Skip to content

Bugfix: Implement __str__ for enum props to fix query string - #259

Merged
dbanty merged 2 commits into
openapi-generators:mainfrom
benchling:fix-enums-as-query-params
Dec 6, 2020
Merged

Bugfix: Implement __str__ for enum props to fix query string#259
dbanty merged 2 commits into
openapi-generators:mainfrom
benchling:fix-enums-as-query-params

Conversation

@packyg

Copy link
Copy Markdown
Contributor

Update both string and int enums to have a __str__ method that return just the string representation of the value:

def__str__(self) ->str:
returnstr(self.value)

This fixes a bug introduced in #241 (my bad 😬 ). As I noted on that PR, request bodies will be fine as json.dumps will convert the enums correctly. However it does not work for query string params - urllib.parse.urlencode (which httpx uses here)
just does str(val), which returns "EnumClass.key".

So

classSomeEnum(str, Enum):
val="a_value"some_endpoint.sync(client=api_client, some_query_string=SomeEnum.val)

would result in a query string of ?someQueryString=SomeEnum.val instead of ?someQueryString=a_value

@packyg

Copy link
Copy Markdown
ContributorAuthor

CC @dbanty

@codecov

codecovBot commented Dec 5, 2020

Copy link
Copy Markdown

Codecov Report

Merging #259 (73eed97) into main (4556d9a) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@ Coverage Diff @@## main #259 +/- ##
=========================================
Coverage 100.00% 100.00% =========================================
Files 46 46 Lines 1302 1302 =========================================
Hits 1302 1302 

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4556d9a...73eed97. Read the comment docs.

{% endfor %}

def __str__(self) -> str:
return str(self.value)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I wanted to just do return self.value, but mypy couldn't figure out the type

@dbantydbanty added this to the 0.7.1 milestone Dec 6, 2020
@dbanty
dbanty merged commit dee661a into openapi-generators:mainDec 6, 2020
@dbanty

Copy link
Copy Markdown
Collaborator

Thanks for the fix! 🐛

dbanty added a commit that referenced this pull request Dec 8, 2020
@eli-bl
eli-bl deleted the fix-enums-as-query-params branch November 22, 2024 19:42
Sign up for freeto 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.

2 participants

@packyg@dbanty