Multi-value Literal returns const in the JSON sub-schema of an anyOf, which isn't supported by OpenAPI:
anyOf – the subschemas must be OpenAPI schemas and not standard JSON Schemas.
Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":
pydantic version: 1.4
pydantic compiled: False
install path: /Users/[...]/venv/lib/python3.7/site-packages/pydantic
python version: 3.7.4 (default, Sep 7 2019, 18:27:02) [Clang 10.0.1 (clang-1001.0.46.4)]
platform: Darwin-18.7.0-x86_64-i386-64bit
optional deps. installed: ['typing-extensions']
from pydantic import BaseModel
from typing_extensions import Literal
class FooBar(BaseModel):
values: Literal["value_1", "value_2"]
print(FooBar.schema_json(indent=2))
output:
{
"title": "FooBar",
"type": "object",
"properties": {
"values": {
"title": "Values",
"anyOf": [
{
"const": "value_1",
"type": "string"
},
{
"const": "value_2",
"type": "string"
}
]
}
},
"required": [
"values"
]
}
Literal with only one value is OpenAPI compliant btw
This issue is linked to: https://github.com/tiangolo/fastapi/issues/562, https://github.com/samuelcolvin/pydantic/pull/649 (the simple enum would work well)
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.
Should we change pydantic so literal with multiple choices matches enum?
@samuelcolvin actually that makes sense, a Literal generates a JSON Schema const, and it could only have a single value. So, yes, a multi-valued Literal should create a JSON Schema equivalent to an enum.
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)...
So would you mind to provide me a small hint on where to start in the code, I will take a look and let see if I can do a PR 😊
Thanks again for the efforts!
Most helpful comment
Is there any progress? I have the same problem.