fix(core): preserve $defs array/primitive types in schema_to_pydantic_model#7211
Open
Mr-Neutr0n wants to merge 1 commit intomicrosoft:mainfrom
Open
fix(core): preserve $defs array/primitive types in schema_to_pydantic_model#7211Mr-Neutr0n wants to merge 1 commit intomicrosoft:mainfrom
Mr-Neutr0n wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…_model
When `$defs` contains non-object type definitions (arrays, primitives),
the schema_to_pydantic_model function was incorrectly creating empty
BaseModel classes instead of preserving the original type information.
This caused schemas with type aliases like:
`type TaskData = list[str]`
to be converted from:
`{"type": "array", "items": {"type": "string"}}`
to:
`{"type": "object", "properties": {}}`
The fix adds a separate `_type_cache` to store non-object type definitions
and a `_schema_to_python_type` method to properly convert them to Python
types. The `get_ref` method now checks both caches.
Fixes microsoft#7203
Signed-off-by: Harikrishna KP <harikp2002@gmail.com>
Author
|
@microsoft-github-policy-service agree |
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.
Summary
Fixes #7203
When
$defscontains non-object type definitions (arrays, primitives), theschema_to_pydantic_modelfunction was incorrectly creating empty BaseModel classes instead of preserving the original type information.Before (broken):
After (fixed):
Changes
_type_cachedictionary to store non-object type definitions separately from model cache_schema_to_python_type()method to convert non-object schemas (arrays, primitives) to Python types_process_definitions()to detect non-object types and route them appropriatelyget_ref()to check type cache before model cacheTest Plan
test_defs_array_type_preserved- reproduces exact issue from schema_to_pydantic_model loses $defs type information (array → object) #7203test_defs_primitive_type_preserved- tests primitive types in $defstest_defs_array_with_constraints- tests array constraints (minItems/maxItems)