fix: resolve Pydantic field aliases in nested expression field queries#1278
Open
roman-right wants to merge 5 commits intomainfrom
Open
fix: resolve Pydantic field aliases in nested expression field queries#1278roman-right wants to merge 5 commits intomainfrom
roman-right wants to merge 5 commits intomainfrom
Conversation
Fixes #937, #945. ExpressionField now carries optional type metadata about the nested Pydantic model. When accessing sub-fields via __getattr__, it looks up the field alias from the model's field info instead of using the Python attribute name directly. This works at arbitrary nesting depth. Added test models and 5 new tests in TestAliasResolution.
for more information, see https://pre-commit.ci
…__getattr__ - Remove mid-code import of get_model_fields (already imported at module level) - Remove bare try/except that silently swallowed all exceptions
for more information, see https://pre-commit.ci
184884a to
0f5e25b
Compare
…h resolution Introduce FieldResolution dataclass (frozen, immutable) that tracks: - model_class: nested BaseModel for alias resolution during field access - is_link: whether path crosses a Link/BackLink boundary ExpressionField now resolves aliases through Link types and carries is_link metadata. resolve_query_paths() replaces the legacy convert_ids() hack with metadata-driven DBRef translation (_id <-> ) at query time. Architecture: - ExpressionField._resolve_field() returns FieldResolution (was _resolve_nested_model) - __getattr__ propagates is_link from parent to child expressions - resolve_query_paths() uses is_link flag for translation - convert_ids() kept as deprecated shim 23 new unit tests for FieldResolution and resolve_query_paths. Fixes #937, #945
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #937, #945
Problem
When using Pydantic Field aliases on nested BaseModel fields, find queries used the Python attribute name instead of the alias. For example,
Doc.nested.unit_classproducednested.unit_classin the MongoDB query instead ofnested.unitClass.This also affected Link fields with aliases (#945).
Fix
ExpressionFieldnow carries optional type information about the nested model. When accessing a sub-attribute, it looks up the alias in the nested model's fields and resolves it. This works at arbitrary nesting depth.Changes:
ExpressionField.__new__accepts an optionalmodel_classparameterExpressionField.__getattr__resolves aliases from the nested model's fieldsinit_document_fieldsandinit_view_fieldspass the resolved nested model type toExpressionFieldTests
Added tests for: