question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Python not serializing enums correctly

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I have an endpoint that accepts an enum as part of its request body. Whenever I pass Products.allowed_values[('value',)]["TRANSACTIONS"] as the value I get the failure:

E               name.exceptions.ApiTypeError: Invalid type for variable '0'. Required value type is Products and passed type was str at ['initial_products'][0]

Python model serialization fails when type is not Products('transactions').

openapi-generator version

Docker CLI v5

OpenAPI declaration file content or url

    initial_products:
      type: array
      items:
        $ref: '#/components/schemas/Products'
        
    Products:
      title: Products
      enum:
      - transactions
      type: string
Generation Details
docker run --rm -v /mydrive:/local openapitools/openapi-generator-cli:v5.0.0 generate -g python -i local/api.yml -o local/build/generated-python \
		-p packageName=name 
Steps to reproduce

Pass in Products('transactions') to the endpoint and get JSON as {value: 'transactions'}. Should be just 'transactions'.

Pass in Products.allowed_values[('value',)]["TRANSACTIONS"] and get the failure mentioned above.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/7058

Suggest a fix

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
spacethercommented, Jan 8, 2021

All merged

1reaction
cgfarmer4commented, Jan 8, 2021

Thanks so much for jumping in to fix here @spacether. Will keep an eye on the merge.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serialising an Enum member to JSON - Stack Overflow
The correct answer depends on what you intend to do with the serialized version. If you are going to unserialize back into Python, ......
Read more >
enum.IntEnum is not compatible with JSON serialisation
Serialization isn't the only issue - you have to know how to deserialize as well. I use JSON extensively in Mailman's REST API....
Read more >
Python type hinting - circular imports - enum serialization bug
With this class definition, all classes are properly imported, type hints are able to resolve and objects can be created. So it does...
Read more >
Stop wasting time with enums in C# | Volare Software
Enums in C# can make you code easier to read: But enums don't cross in and out ... only needed if you serialized...
Read more >
TypeScript string enums, and when and how to use them
No matter how much you know about TypeScript enums, this guide covers all ... String enums are serializable over transfer protocols and are ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found