| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
When using Annotated[T, Field(default)] without an explicit parameter default (= value), pydantic 2.12+ changed FieldInfo.from_annotated_attribute() to overwrite the Field's default with PydanticUndefined, incorrectly marking fields as required in the JSON schema. This fix checks if a Field with a default exists in the Annotated metadata and uses FieldInfo.from_annotation() to preserve that default, while still using from_annotated_attribute() for the standard case where parameter defaults take precedence. The fix maintains backward compatibility with pydantic 2.11 and earlier while ensuring correct behavior with 2.12+.
Oh perfect, I'd be keen to get that in then! Do you have time to fix the merge conflicts? |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
This PR fixes a compatibility issue with pydantic 2.12+ where fields using Annotated[T, Field(default)] without explicit parameter defaults were incorrectly marked as required in JSON schemas.
Motivation and Context
Pydantic 2.12.0 introduced a breaking change in how FieldInfo.from_annotated_attribute() handles defaults. When passing PydanticUndefined as the second parameter, it now overwrites any default value specified inside Field() within an Annotated type. This causes fields that should be optional (with defaults from Field()) to be incorrectly marked as required in the generated JSON schema.
The issue affects function parameters like:
With pydantic 2.12+, this parameter would be marked as required in the JSON schema, even though Field(1) specifies a default value.
How Has This Been Tested?
The solution detects when an Annotated type contains a FieldInfo with a default and uses FieldInfo.from_annotation() to preserve that default, while continuing to use from_annotated_attribute() for standard cases where parameter defaults should take precedence.
Breaking Changes
None. This is a bug fix that maintains backward compatibility with pydantic 2.11 while adding forward compatibility with pydantic 2.12+.
Types of changes
Checklist
Additional context
This fix prepares the codebase for pydantic 2.12+, which will be included in dependency updates. The change is minimal and focused on preserving the intended behavior: defaults specified in Field() within Annotated types should be respected in the JSON schema.
The implementation checks for the presence of a FieldInfo with a non-undefined default in the Annotated metadata and chooses the appropriate FieldInfo construction method accordingly.