-
Notifications
You must be signed in to change notification settings - Fork 272
fix(python): emit Pydantic field aliases to fix nested field validation #11357
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
base: main
Are you sure you want to change the base?
fix(python): emit Pydantic field aliases to fix nested field validation #11357
Conversation
🌱 Seed Test SelectorSelect languages to run seed tests for:
How to use: Click the ⋯ menu above → "Edit" → check the boxes you want → click "Update comment". Tests will run automatically and snapshots will be committed to this PR. |
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.
This is the final PR Bugbot will review for you during this billing cycle
Your free Bugbot reviews will reset on January 18
Details
You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
packages/cli/configuration-loader/src/generators-yml/convertGeneratorsConfiguration.ts
Outdated
Show resolved
Hide resolved
13c594b to
ed09d46
Compare
| # the model encodes aliasing: | ||
| # - If the model uses real Pydantic aliases (Field(alias=...)), pass wire keys through unchanged. | ||
| # - If the model encodes aliasing only via FieldMetadata, pre-dealias before validation. | ||
| if inspect.isclass(type_) and issubclass(type_, pydantic.BaseModel): |
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.
Wrong base class in issubclass check for v1-on-v2 context
The alias detection logic checks issubclass(type_, pydantic.BaseModel) but UniversalBaseModel in this file extends pydantic.v1.BaseModel (line 56). In Pydantic v2, these are entirely separate class hierarchies - pydantic.v1.BaseModel is NOT a subclass of pydantic.BaseModel. This means the condition will always be False for any model in the with_pydantic_v1_on_v2 context, causing the alias detection code to never execute. The check needs to use pydantic.v1.BaseModel instead to match the base class actually used by models in this context.
| else convert_and_respect_annotation_metadata(object_=object_, annotation=type_, direction="read") | ||
| ) | ||
| else: | ||
| dealiased_object = convert_and_respect_annotation_metadata(object_=object_, annotation=type_, direction="read") |
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.
Missing allow_population_by_field_name config in v1-on-v2 variant
The UniversalBaseModel.Config class in the with_pydantic_v1_on_v2 variant is missing allow_population_by_field_name = True, which this PR adds to all other variants (both generated seed files and the similar with_pydantic_v1_on_v2/with_aliases variant). This config option is needed to allow Pydantic v1 to accept both wire aliases and Python field names. Combined with the wrong issubclass check bug, when de-aliasing always occurs but models also have pydantic.Field(alias=...) set, validation will fail because Pydantic v1 without this config only accepts the alias.
Description
Linear ticket: FER-8077
A previous fix, #11011 only helped when callers invoked
model_validate()directly, but it did not apply to nested validation paths (e.g. when a parent model is validated viaTypeAdapter), so wire keys likeis_base64_encodedcould still fail with “Field required” inside nested objects. This PR fixes nested decoding by emittingpydantic.Field(alias=...)for aliased fields so Pydantic v2 can resolve wire aliases during compiled nested validation.Changes Made
pydantic.Field(alias=...)is emitted for aliased fieldsUncheckedBaseModel.model_validatePydantic v2 overrideTypeAdapterTesting
Note
Emit
pydantic.Field(alias=...)for aliased fields and update parsing/config so aliases are honored during nested Pydantic v2 validation; remove the v2model_validateoverride.pydantic.Field(alias=...)for aliased fields across generated models (including optional fields, unions, nested objects, and examples).aliasto field definitions.parse_obj_asto detect real Pydantic aliases and only pre-dealias when needed; supports both v1 and v2 paths.UncheckedBaseModel.model_validateoverride for Pydantic v2.populate_by_name/allow_population_by_field_name) inUniversalBaseModel.pydantic.Field(alias=...)and new model config to ensure nested validation works with wire aliases.Written by Cursor Bugbot for commit 5d2873c. This will update automatically on new commits. Configure here.