-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Multi-value Literal isn't compliant with OpenAPI #1350
Comments
This is not really a bug in Pydantic. Pydantic generates valid JSON Schema. OpenAPI uses an old version of JSON Schema that only supports enums. But you can change your model to use an enum instead: https://pydantic-docs.helpmanual.io/usage/types/#enums-and-choices It achieves the same and will generate a JSON Schema with enums, that is valid with OpenAPI. |
Should we change pydantic so literal with multiple choices matches enum? |
@tiangolo I agree, my concern is more about the benefit of the Literal type as a one-liner choice on a Pydantic/FastAPI/Swagger UI context. @samuelcolvin If it breaks too many things, I would suggest a simple note in the documentation to avoid this unexpected behaviour (Literal with one element works well in fastapi context but not with multiple elements) |
Please add a pr to improve the documentation. We should change literals in V2. |
@samuelcolvin actually that makes sense, a |
okay, do you think the change represents a "breaking change" that needs to wait for v2, or a bug fix than can go out in a minor version? |
Sorry for the delay (still figuring out how to handle GitHub email notifications), I think that looks like a "bug fix" or extra feature that could go in a minor version. |
Is there any progress? I have the same problem. |
Not yet, would you like to submit a PR. |
Hi guys, thanks for the research on this topics. For us, this is right now a blocker to go to production (not fulfilling good practices in documentation)... Thanks again for the efforts! |
@JavierMartinezAlc it looks like it is implemented here and/or there |
Multi-value Literal returns
const
in the JSON sub-schema of ananyOf
, which isn't supported by OpenAPI:Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Code to reproduce
output:
Literal with only one value is OpenAPI compliant btw
This issue is linked to: fastapi/fastapi#562, #649 (the simple
enum
would work well)The text was updated successfully, but these errors were encountered: