Fix Sequence ignoring discriminator #9980
Merged
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.
Change Summary
This is a fix for bug #9872. The annotation
Sequenceignoresdiscriminatormetadata in certain cases. The generated schema has not correctpython_schemawhere aunionis presented rather than expectedtagged-union.json_schemais normal.The bug occurs in
GenerateSchema._sequence_schema. In thejson-or-pythonschema created, the underling reference ofitem_type_schemais the same, therefore themetadatadict is also shared. When callingapply_discriminatorson the model schema, becausejson_schemais walked first, it has correct conversion fromuniontotagged-union. But the walk will pop the discriminator frommetadata. Therefore whenpython_schemais walked,metadatais already empty, resulting inunion, nottagged-unionschema.I suspect there are more of these kinds of reference sharing problems in other parts of schema generation process. 🤔
Note the author mentioned that the bug disappears when adding
model_validatorto both models. This is because the procedure for applying discriminators is different. It isGenerateSchema._apply_discriminator_to_unionthat generatestagged-unionin this case.Related issue number
fix #9872
Checklist