fix: skip redundant isinstance dispatch for homogeneous model unions - #1487
Open
AndersBennedsgaard wants to merge 1 commit into
Conversation
When a property is a union of schemas that are all object/model types (e.g. `oneOf: [ModelA, ModelB]`), `union_property.py.jinja`'s `transform` macro generated a per-member `isinstance` dispatch in `to_dict()`, even though every branch does the exact same thing (`.to_dict()`). The check never affects the outcome, since every generated model class converts identically. Detect when every union member is model-shaped and skip the per-member dispatch: - required: unconditional `dest = source.to_dict()`, no isinstance at all - optional: `else: dest = source.to_dict()` after the existing Unset guard - optional request bodies (`skip_unset=True`, no Unset guard emitted yet): guard with `if not isinstance(source, Unset)` instead of the previous single-member `isinstance(source, ModelX)` check Mixed unions (model + primitive/enum/None/Any members) are unaffected and keep their existing per-member dispatch, since those branches genuinely differ in behavior. Discussion: openapi-generators#1486
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.
When a property is a union of schemas that are all object/model types (e.g.
oneOf: [ModelA, ModelB]),union_property.py.jinja'stransformmacro generated a per-memberisinstancedispatch into_dict(), even though every branch does the exact same thing (.to_dict()). The check never affects the outcome, since every generated model class converts identically.Detect when every union member is model-shaped and skip the per-member dispatch:
dest = source.to_dict(), no isinstance at allelse: dest = source.to_dict()after the existing Unset guardskip_unset=True, no Unset guard emitted yet): guard withif not isinstance(source, Unset)instead of the previous single-memberisinstance(source, ModelX)checkMixed unions (model + primitive/enum/None/Any members) are unaffected and keep their existing per-member dispatch, since those branches genuinely differ in behavior.
pdm run checkrun succesfully.Discussion: #1486
Full disclosure: I used Claude Sonnet to help with the development. Feel free to point our any AI slop, happy to fix it