Skip to content

✨ 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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

stickM4N
Copy link

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:

class Gender(IntEnum):
    male = auto()
    female = auto()
    other = auto()
    not_given = auto()

ta = TypeAdapter(Gender)
ta_schema = ta.json_schema()

# This generates the following
assert ta_schema == {
    "enum": [1, 2, 3, 4],
    "title": "Gender",
    "type": "integer"
}

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:

export const Gender= {
    NUMBER_1: 1,
    NUMBER_2: 2,
    NUMBER_3: 3,
    NUMBER_4: 4
} as const;
export type Gender = typeof Gender[keyof typeof Gender];

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 the x-enum-varnames.

With a simple implementation, as intended by pydantic, we could get this done in a blink of an eye.

class CustomJsonSchemaGenerator(GenerateJsonSchema):
    def enum_schema(self, schema: EnumSchema) -> JsonSchemaValue:
        generated_schema = super().enum_schema(schema)
        generated_schema['x-enum-varnames'] = [enum.name for enum in schema['members']]

        return generated_schema

With the output being:

export const Gender= {
    MALE: 1,
    FEMALE: 2,
    OTHER: 3,
    NOT_GIVEN: 4
} as const;
export type Gender = typeof Gender[keyof typeof Gender];

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 of FastAPI-ways for openapi overrideshown in the doc will remain the same and the doc will not require update

@YuriiMotov YuriiMotov changed the title Allow custom JsonSchemaGenerator for OpenAPI generation ✨ Allow custom JsonSchemaGenerator for OpenAPI generation Jul 23, 2025
@YuriiMotov YuriiMotov added the feature New feature or request label Jul 23, 2025
@YuriiMotov
Copy link
Contributor

@stickM4N, thanks for your interest!

This is a duplicate of #12455.
Not sure the author of that PR is still interested in it.
This comment is also applicable to your PR

@stickM4N
Copy link
Author

@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...)
Their approach to this is interacting with FastAPI logic directly, and though i like that approach I opted by just interacting with the utility it gives to form the requests.
It preserved the FastAPI-way to do the schema generation override, reducing the impacts on other parts of the module and still giving the feature.
Probably a better solution is to use the other approach and pass that content directly to App initialization...
I think this is a good feature that con be implemented quickly using the current interface (already prepared for it)

@github-actions github-actions bot removed the waiting label Jul 24, 2025
@YuriiMotov
Copy link
Contributor

I opted by just interacting with the utility it gives to form the requests. It preserved the FastAPI-way to do the schema generation override, reducing the impacts on other parts of the module and still giving the feature.

I agree, this should be enough.

We still need to add test for this feature

Copy link
Contributor

@YuriiMotov YuriiMotov left a 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.

@stickM4N

This comment was marked as resolved.

@stickM4N

This comment was marked as resolved.

@YuriiMotov

This comment was marked as resolved.

@stickM4N

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants