[python] Deserialize Optional[...] response types - #24718
Merged
wing328 merged 1 commit intoAug 18, 2026
Merged
Conversation
…AttributeError The Python generator writes each operation's _response_types_map from the Python type annotation for the response, so a nullable response type is emitted as e.g. "Dict[str, Optional[object]]". ApiClient.__deserialize only understands List[...], Dict[...] and native type names, so Optional[...] fell through to getattr(models, "Optional[object]") and every successful response from such an endpoint raised AttributeError. Unwrap Optional[...] before the existing rules. data is already known to be non-None at that point, so the inner type fully determines the result. Fixes OpenAPITools#24712
Member
|
lgtm. thanks for the fix |
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 #24712
Problem
ApiClient.__deserializeparses onlyList[...],Dict[...]and native type names. The generator fills_response_types_mapfrom the response annotation, so a nullable response emits"Dict[str, Optional[object]]", which falls through togetattr(models, "Optional[object]")and makes every successful response raiseAttributeError.Change
Unwrap
Optional[...]before the existing rules.data is Nonehas already returned above, so the inner type determines the result.Verification
AttributeErrorbefore,{'a': 1}after.pytest tests/: 155 passed. The 12 failures need the live petstore server, same on master.mvn test -Dtest='Python*Test': 102 passed./cc @cbornet @tomplus @krjakbrjak @fa0311
Summary by cubic
Deserializes Python
Optional[...]response types by unwrapping to the inner type, so nullable responses no longer raiseAttributeError. Old:ApiClient.__deserializetreatedOptional[...]as a model and failed. New: it detects and unwrapsOptional[...]before existingList[...]/Dict[...]rules;Nonestill returnsNone. Fixes #24712.Review notes
modules/openapi-generator/src/main/resources/python/api_client.mustache; regenerated sample clients mirror the update.Optional[Pet],Optional[List[str]], andDict[str, Optional[object]]; local suites pass, petstore live-server failures matchmaster.Written for commit 579861f. Summary will update on new commits.