fix(python): serialize an enum path or query parameter passed as its value
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:
Regenerated the configs with./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exitgeneratorName: 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.