Skip to content

[python] Deserialize Optional[...] response types - #24718

Merged
wing328 merged 1 commit into
OpenAPITools:masterfrom
HDPark95:fix/issue-24712-python-optional-deserialize
Aug 18, 2026
Merged

[python] Deserialize Optional[...] response types#24718
wing328 merged 1 commit into
OpenAPITools:masterfrom
HDPark95:fix/issue-24712-python-optional-deserialize

Conversation

@HDPark95

@HDPark95 HDPark95 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #24712

Problem

ApiClient.__deserialize parses only List[...], Dict[...] and native type names. The generator fills _response_types_map from the response annotation, so a nullable response emits "Dict[str, Optional[object]]", which falls through to getattr(models, "Optional[object]") and makes every successful response raise AttributeError.

Change

Unwrap Optional[...] before the existing rules. data is None has already returned above, so the inner type determines the result.

Verification

  • Issue repro spec: AttributeError before, {'a': 1} after.
  • 4 tests added to the python sample; 3 fail on master.
  • 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 raise AttributeError. Old: ApiClient.__deserialize treated Optional[...] as a model and failed. New: it detects and unwraps Optional[...] before existing List[...]/Dict[...] rules; None still returns None. Fixes #24712.

Review notes

  • Changes are in modules/openapi-generator/src/main/resources/python/api_client.mustache; regenerated sample clients mirror the update.
  • Adds tests for Optional[Pet], Optional[List[str]], and Dict[str, Optional[object]]; local suites pass, petstore live-server failures match master.
  • No migration actions; generated Python clients now correctly deserialize nullable responses.

Written for commit 579861f. Summary will update on new commits.

Review in cubic

…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

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 10 files

Re-trigger cubic

@wing328

wing328 commented Aug 18, 2026

Copy link
Copy Markdown
Member

lgtm. thanks for the fix

@wing328
wing328 merged commit 0842190 into OpenAPITools:master Aug 18, 2026
56 checks passed
@wing328 wing328 added this to the 7.25.0 milestone Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][PYTHON] Nullable response type emits a _response_types_map string __deserialize cannot parse — AttributeError on every successful response

2 participants