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 🤞 |
sydney-runkle
left a comment
There was a problem hiding this comment.
Looks great overall, just 2 quick change requests. Thanks!
|
|
||
|
|
||
| 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.
| """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.
Could you include this example as well? baz=(Annotated[int, Field(..., description="Baz description")], ...),
There was a problem hiding this comment.
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")], ...),
)|
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_overrideI 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