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
Pydantic changes json schema representation when extra keys are provided #3896
Comments
bug deep in |
I believe this is the same issue we are seeing after upgrading If so, we see the identical problem, and it is causing major headaches as the output of It seems like this is a bug (as @samuelcolvin noted), although in the seemingly related #2592 that OP seems to indicate it's expected. For clarity, here's a quick runnable bugjar using
from dataclasses import field
import json
from enum import Enum, unique
from pydantic.dataclasses import dataclass
@unique
class UserRole(Enum):
MANAGER = "manager"
TECHNICIAN = "technician"
VIEWER = "viewer"
@dataclass(frozen=True)
class User():
"""
the following DocTest PASSES:
but the AllOf occurence looks to be a bug
>>> print(json.dumps(User.__pydantic_model__.schema(), indent=4))
{
"title": "User",
"type": "object",
"properties": {
"role": {
"description": "custom descr of UserRole field",
"allOf": [
{
"$ref": "#/definitions/UserRole"
}
]
},
"email": {
"title": "Email",
"type": "string"
}
},
"required": [
"role",
"email"
],
"definitions": {
"UserRole": {
"title": "UserRole",
"description": "An enumeration.",
"enum": [
"manager",
"technician",
"viewer"
]
}
}
}
"""
role: UserRole = field(
metadata=dict(
description="custom descr of UserRole field",
)
)
email: str |
@samuelcolvin we're upgrading from a very old version Testing several recent releases it appears introduced between
our update was driven by a necessary update to Thank you! |
Unless I'm misunderstanding something, this isn't actually a bug, but it is intended behavior — OpenAPI<3.1 doesn't allow sibling keys next to Given that, and the fact that that pushing down into the single-item If you want to request we make it an option to generate JSON schema without pushing down |
Checks
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Creating a nested class as such:
will result in the following schema generated:
However, when an extra kwarg is assigned to the nested object foo:
This will result in the following schema:
The main issue with this, and find problematic, is that foo (NestedNestedClass) has been changed from a simple reference, to something that is
allOf
of something, which is list with a single element with the original reference?The text was updated successfully, but these errors were encountered: