-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
✨ Allow custom JsonSchemaGenerator
for OpenAPI generation
#13918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
JsonSchemaGenerator
for OpenAPI generation
@stickM4N, thanks for your interest! This is a duplicate of #12455. |
@YuriiMotov Thanks for the quick feedback. When I was doing research about this, I saw that PR (and also an old branch still in the repo, probably deprecated...) |
I agree, this should be enough. We still need to add test for this feature |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is quite useful feature that was requested many times.
This will allow solving (overcome) multiple issues with schema generation:
- adding nice names for ENUM parameters (described in this PR)
- fix schema for
Literal["something"]
described in this PR - fix schema for optional Query parameters described in those PRs: #12375, #9873
- change sorting order, described in this discussion and others
- I believe there are many other use-cases where it might be useful
In the future we may want to switch to using our own customized implementation of GenerateJsonSchema
, but for now it would be nice to provide the way for users to workaround some of mentioned (and other) issues.
I'm not sure whether we need to add docs for this feature. I think we can skip it for now and add it later.
Intro
OpenAPI generators have grom a lot by themselves and despite all comply with base OpenAPI specs, many have implemented extra features that would be good to have when getting the schema from
FastAPI
.Example use case:
The inconvenience here comes because OpenAPI specs does not hace naming for
IntEnum
class attributes and most be created by generators without any naming convention...TypeScript generated code with
openapi-generator-cli
would look like this and is completely abstract:These generators have implemented extra definitions to provide these attributes naming association. This varies depending on the generator but if we keep on with the one we are using already (
openapi-generator-cli
) that would be thex-enum-varnames
.With a simple implementation, as intended by
pydantic
, we could get this done in a blink of an eye.With the output being:
Conclusion
FastAPI
already has the interface to make this, it just lacks the parameter of the generator to be used. So, adding that to its implementation would be all what is required to have this feature, as shown in the code changes. The rest ofFastAPI
-ways for openapi overrideshown in the doc will remain the same and the doc will not require update