Comments (3)
Hi,
We have a discriminator (generated via FastAPI/Pydantic Literal')
It generates the following snippets in the OpenAPI spec, however the speakeasy cli fails with:
ERROR validation error: [line 280] discriminator mapping ref #/components/schemas/Distribution must have a property named style of type string or enum ERROR failed to generate SDKs for python ✖
"Distribution": {
"properties": {
"style": {
"const": "distribution",
"title": "Style",
"default": "distribution"
}
},
"type": "object",
"title": "Distribution",
"description": "Pydantic class for when we want the distribution on a particular date."
}
"statistic": {
"oneOf": [
{
"$ref": "#/components/schemas/Mean"
},
{
"$ref": "#/components/schemas/Quantile"
},
{
"$ref": "#/components/schemas/Distribution"
}
],
"title": "Statistic",
"discriminator": {
"propertyName": "style",
"mapping": {
"distribution": "#/components/schemas/Distribution",
"mean": "#/components/schemas/Mean",
"quantile": "#/components/schemas/Quantile"
}
}
}
It seems it is failing on the missing "type"
tag on "Distribution", however the swagger editor is able to determine it correctly.
Is this related to implying types from const/default attributes not being supported?
from speakeasy.
Hi @fergusdixon thanks for the issue report.
So looking through the schemas you provided above the issue you are seeing is because your style
schema doesn't have a type
attribute like type: string
.
We rely on strong typing in your OpenAPI spec as much as possible to ensure we are generating the right types, so when a type
isn't present we treat it as a generic any
type which in this case fails our validation for discriminators because we are try to ensure they are strings.
While json-schema does allow for loose typing like this and assumptions on the type of the schema being made on heuristics around what other properties are present in the schema, we do little of that ourselves except where we can be pretty confident we have the type right, for example if you have a properties
field or an items
field in your schema we can be pretty sure its either an object
or array
you intended for the type
of the schema. But for things like deducing the type
is a string
from const
or default
values that requires as to parse those values as json and try and deduce the type but that is error prone especially if the string values might contain a date or number for example.
So we just don't assume type in these situations and required it to be explicitly defined.
Hope that provides some context around why this is failing
from speakeasy.
Hey, thanks for the clarification and definitely makes sense. Frustrating that the FastAPI/Pydantic Literal types don't include this. I think we can find a workaround using Field(json_schema_extra={"type": "string"}
on the Literal fields to force that verbose typing
from speakeasy.
Related Issues (20)
- Incorrect JSON Format for PHP Serialization
- [SPE-2660] Enforce authentication as required when there are multiple required security schemes
- [SPE-2676] Automatically apply careful diff suppression to APIs which return sometimes return optional attributes but with ForceReplace semantics HOT 1
- [SPE-2678] Improve type coalescing for openapi oneOf subtypes with a discriminator defined HOT 1
- [SPE-2237] Terraform DataSources that hide Pagination Details HOT 1
- [SPE-2691] Nix Package manager support
- [SPE-2702] Ergonomics Improvements
- [SPE-2718] Escape JSON in Descriptions for SDK Docs HOT 1
- [SPE-2720] Support defaults and consts for parameters HOT 3
- csharp: Error: sdk.cs.stmpl:112:32: HOT 3
- [SPE-2828] [bug] python generator writes invalid dataclass if using params with and without default value HOT 2
- [SPE-2784] Support parametrised server urls in Terraform
- [SPE-2793] Default values set in the OAS are not set in Terraform Plans
- [SPE-2286] Java - v2, support x-speakeasy-pagination
- Question and Issue related to terraform generation HOT 4
- Runtime error generating SDK for security schemes HOT 1
- [SPE-2823] Support Set Attributes in Terraform HOT 2
- [SPE-2833] Set Module Path in Python SDK
- [SPE-2836] Terraform generation should preferentially work with application/json content types
- [SPE-2857] Custom Plan Validators Support in Terraform HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from speakeasy.