Repository navigation
Conversation
…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
Member
|
thanks for the PR cc @cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10) |
This branch has not been deployed
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.
The generated serializer reads
.valuefrom an enum reference path or query parameter, so passing the enum's value instead of the enum raisesAttributeError: 'str' object has no attribute 'value'. This happens with a nullable enum reference (allOf: [$ref]withnullable: true), which is typedOptional[Any], so@validate_calldoes not convert a string to the enum before the serializer runs.ApiClient.param_serializealready runs path and query parameters throughsanitize_for_serialization, which returnsobj.valuefor anEnum. Header and form parameters already rely on that, so this change drops the{{#isEnumRef}}.value{{/isEnumRef}}from the path and query lines ofpython/api.mustacheand 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-v1has the same.valueon query parameters, but itssanitize_for_serializationhas noEnumbranch, so it is left out.Validation: regenerated the samples of all
generatorName: pythonconfigs; only the.valuelines changed. AddedtestEnumRefQueryParameterAcceptsEnumOrValueto thepythonPetstore sample, which serializesfake_enum_ref_query_parameterwithEnumClass.MINUS_EFGand with"-efg"; it fails on master with theAttributeErrorabove and passes with this change. Thepython,python-aiohttpandpython-httpx2sample suites pass except tests that need a local Petstore server.Fixes #16071
PR checklist
generatorName: python, the only ones using this template.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
.valuefrom the parameter, so passing the value itself raisedAttributeError: 'str' object has no attribute 'value'-- an easy mistake with nullable enum references, which are typedOptional[Any].Path and query parameters are now passed as given;
ApiClient.param_serializealready converts enums to their values, matching how header and form parameters work.python-pydantic-v1is left unchanged because itssanitize_for_serializationhas no enum branch.Added a test to the
pythonPetstore sample that serializes a query parameter both as an enum and as its value.Written for commit 0a0e4d7. Summary will update on new commits.