Skip to content
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

model_json_schema export with Annotated types misses 'required' parameters #8793

Merged
merged 3 commits into from Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions pydantic/fields.py
Expand Up @@ -372,6 +372,8 @@ class MyModel(pydantic.BaseModel):
if _typing_extra.is_annotated(annotation):
first_arg, *extra_args = typing_extensions.get_args(annotation)
field_infos = [a for a in extra_args if isinstance(a, FieldInfo)]
if default is Ellipsis:
default = PydanticUndefined
field_info = FieldInfo.merge_field_infos(*field_infos, annotation=first_arg, default=default)
metadata: list[Any] = []
for a in extra_args:
Expand Down
39 changes: 22 additions & 17 deletions tests/test_json_schema.py
Expand Up @@ -6,14 +6,7 @@
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from enum import Enum, IntEnum
from ipaddress import (
IPv4Address,
IPv4Interface,
IPv4Network,
IPv6Address,
IPv6Interface,
IPv6Network,
)
from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
from pathlib import Path
from typing import (
Any,
Expand Down Expand Up @@ -75,15 +68,7 @@
model_json_schema,
models_json_schema,
)
from pydantic.networks import (
AnyUrl,
EmailStr,
IPvAnyAddress,
IPvAnyInterface,
IPvAnyNetwork,
MultiHostUrl,
NameEmail,
)
from pydantic.networks import AnyUrl, EmailStr, IPvAnyAddress, IPvAnyInterface, IPvAnyNetwork, MultiHostUrl, NameEmail
from pydantic.type_adapter import TypeAdapter
from pydantic.types import (
UUID1,
Expand Down Expand Up @@ -5947,3 +5932,23 @@ class Model(BaseModel):
c: ModelC

assert Model.model_json_schema()


def test_json_schema_annotated_with_field() -> None:
"""Check if the ellipsis in the signature is considered as a required field."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Check if the ellipsis in the signature is considered as a required field."""
"""Ensure field specified with Annotated in create_model call is still marked as required."""


from pydantic import create_model

Model = create_model(
'test_model',
bar=(Annotated[int, Field(description='Bar description')], ...),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you include this example as well? baz=(Annotated[int, Field(..., description="Baz description")], ...),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe with this model:

Model = create_model(
    'test_model',
    foo=(int, ...),
    bar=(Annotated[int, Field(description="Bar description")], ...),
    baz=(Annotated[int, Field(..., description="Baz description")], ...),
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 😄

)

assert Model.model_json_schema() == {
'properties': {
'bar': {'description': 'Bar description', 'title': 'Bar', 'type': 'integer'},
},
'required': ['bar'],
'title': 'test_model',
'type': 'object',
}