Skip to content

fix(python): serialize an enum path or query parameter passed as its value - #25540

Open
retrry wants to merge 1 commit into
OpenAPITools:masterfrom
retrry:python-enum-ref-param-value
Open

retrry wants to merge 1 commit into
OpenAPITools:masterfrom
retrry:python-enum-ref-param-value

Conversation

@retrry

@retrry retrry commented Nov 3, 2026 •

Copy link
Copy Markdown

The generated serializer reads .value from an enum reference path or query parameter, so passing the enum's value instead of the enum raises AttributeError: 'str' object has no attribute 'value'. This happens with a nullable enum reference (allOf: [$ref] with nullable: true), which is typed Optional[Any], so @validate_call does not convert a string to the enum before the serializer runs.

ApiClient.param_serialize already runs path and query parameters through sanitize_for_serialization, which returns obj.value for an Enum. Header and form parameters already rely on that, so this change drops the {{#isEnumRef}}.value{{/isEnumRef}} from the path and query lines of python/api.mustache and passes the parameter as given. An enum and its value now serialize the same way.

The Optional[Any] type for a nullable enum reference is unchanged; typing it as the enum needs a codegen change and is out of scope here. python-pydantic-v1 has the same .value on query parameters, but its sanitize_for_serialization has no Enum branch, so it is left out.

Validation: regenerated the samples of all generatorName: python configs; only the .value lines changed. Added testEnumRefQueryParameterAcceptsEnumOrValue to the python Petstore sample, which serializes fake_enum_ref_query_parameter with EnumClass.MINUS_EFG and with "-efg"; it fails on master with the AttributeError above and passes with this change. The python, python-aiohttp and python-httpx2 sample suites pass except tests that need a local Petstore server.

Fixes #16071

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    Regenerated the configs with generatorName: python, the only ones using this template.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Python technical committee: @wing328


Summary by cubic

Fixes the Python generator so enum reference path and query parameters accept both the enum and its raw value. Previously the generated code read .value from the parameter, so passing the value itself raised AttributeError: 'str' object has no attribute 'value' -- an easy mistake with nullable enum references, which are typed Optional[Any].

Path and query parameters are now passed as given; ApiClient.param_serialize already converts enums to their values, matching how header and form parameters work. python-pydantic-v1 is left unchanged because its sanitize_for_serialization has no enum branch.

Added a test to the python Petstore sample that serializes a query parameter both as an enum and as its value.

Written for commit 0a0e4d7. Summary will update on new commits.

View guided diff

…value

The generated serializer read `.value` from an enum reference path or query
parameter, so passing the enum's value instead of the enum raised
AttributeError: 'str' object has no attribute 'value'. A nullable enum
reference (`allOf: [$ref]` with `nullable: true`) is typed `Optional[Any]`,
which invites passing the value. ApiClient.param_serialize already turns an
Enum into its value through sanitize_for_serialization, so the template now
passes the parameter as given.

Fixes OpenAPITools#16071

@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 13 files

View guided diff | Re-trigger cubic

@wing328

wing328 commented Nov 3, 2026

Copy link
Copy Markdown
Member

thanks for the PR

cc @cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][python] Query Param of type String restricted by Enum triggers AttributeError (str has no attribute value)

2 participants