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
model_json_schema export with Annotated types misses 'required' parameters #8793
Conversation
CodSpeed Performance ReportMerging #8793 will not alter performanceComparing Summary
|
please review First open source contribution 🤞 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great overall, just 2 quick change requests. Thanks!
tests/test_json_schema.py
Outdated
|
||
|
||
def test_json_schema_annotated_with_field() -> None: | ||
"""Check if the ellipsis in the signature is considered as a required field.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""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.""" |
|
||
Model = create_model( | ||
'test_model', | ||
bar=(Annotated[int, Field(description='Bar description')], ...), |
There was a problem hiding this comment.
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")], ...),
There was a problem hiding this comment.
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")], ...),
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 😄
So, with a slight modification to the logic in your PR, we can actually fix many related issues. My proposal is the following:
Which fixes:
Would you be willing to add a test for the second issue as well? Thanks for your contribution! |
if field_info.default is not PydanticUndefined and default_override is not PydanticUndefined:
field_info.default = default_override I think this was the wrong proposal because if a field is initialized with a default = PydanticUndefined, the overide won't work 😄 My modification: if default_override is not PydanticUndefined:
field_info.default = default_override |
Looks great, thanks for the help with the fix! |
Change Summary
When using an Annotation with a FieldInfo and an Ellipsis, the Ellipsis was considered as a default value. Therefore, the field was not considered mandatory.
Related issue number
fix #8780
fix #8634
Checklist
Selected Reviewer: @lig