-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Enum as separate model #1173
Comments
Use |
@ViktorPegy Did you find a solution that didn't require you to update This seems like an issue for anyone using a codegen tool to generate client code. By default, the codegen tool will duplicate all enum definitions, which definitely isn't desirable. @samuelcolvin Are there any plans to add a setting that generates enums as separate models by default? |
No plans at present (beyond this issue).
Currently you do need to implement a That way the schema for all models inheriting from Long term if there's a switch or utility function, it'll boil down to the same method for modifying the schema, so your stop gap could also become the long term fix. In terms of a fix, I'd prefer to either:
|
@samuelcolvin Thanks for the thoughtful response — all of that sounds reasonable. At a high level, my understanding is that we can achieve the desired behavior by doing the following:
It looks like Is there a trick to letting us insert the enum definition as part of With regard to the long-term fixes you proposed, I feel like it should be desirable to always output enums as separate models. I know you mentioned that some people might not like that, but what are the circumstances where it'd be better to have duplicate enum definitions over a single enum definition that gets referenced everywhere? If we were to go down this path, the new behavior should remain backwards compatible since the generated schema still follows the Open API spec for reusable enums, and the fix should be pretty straightforward — looks like we just need a few updates to I'd be happy to take a whack at the implementation as I expect this will be useful for anyone using a codegen tool. |
It looks like this issue has been addressed already via #1432 and can be closed. |
@mostaphaRoudsari you're right ! Thanks |
Sorry to comment on an old ticket, but is there any way to force the old (pre v1.6) behaviour on recent pydantic releases? |
Question
Hello. I use the FastAPI framework. Can I extract the enum field as a separate component(with '$ref' in models schema)? I need it because I use the OpenAPI client generator and it generates several enums for each model.
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Models example:
The text was updated successfully, but these errors were encountered: