Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2020

Conversation

packyg
Copy link
Contributor

@packyg packyg commented Dec 5, 2020

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

    def __str__(self) -> str:
        return str(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

class SomeEnum(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
Contributor Author

packyg commented Dec 5, 2020

CC @dbanty

@codecov
Copy link

codecov bot commented Dec 5, 2020

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.

@@ -4,3 +4,6 @@ class {{ enum.reference.class_name }}(str, Enum):
{% for key, value in enum.values.items() %}
{{ key }} = "{{ value }}"
{% endfor %}

def __str__(self) -> str:
return str(self.value)
Copy link
Contributor Author

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

@dbanty dbanty added this to the 0.7.1 milestone Dec 6, 2020
@dbanty dbanty merged commit dee661a into openapi-generators:main Dec 6, 2020
@dbanty
Copy link
Collaborator

dbanty commented Dec 6, 2020

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 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.

2 participants