Skip to content
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

[BUG] Python not serializing enums correctly #8377

Closed
5 of 6 tasks
cgfarmer4 opened this issue Jan 8, 2021 · 14 comments · Fixed by #8387
Closed
5 of 6 tasks

[BUG] Python not serializing enums correctly #8377

cgfarmer4 opened this issue Jan 8, 2021 · 14 comments · Fixed by #8387

Comments

@cgfarmer4
Copy link
Contributor

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

#7058

Suggest a fix
@auto-labeler
Copy link

auto-labeler bot commented Jan 8, 2021

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@spacether
Copy link
Contributor

spacether commented Jan 8, 2021

So I see this working in our tests here:
https://github.com/OpenAPITools/openapi-generator/blob/master/samples/openapi3/client/petstore/python/tests_manual/test_fake_api.py#L133
What version of openapi-generator are you using?
Have you tried passing in InitialProducts([Products('transactions')])?

@spacether
Copy link
Contributor

Values are strictly typed in the python generator. A string will not be accepted, you have to pass in an instance of Products.

@cgfarmer4
Copy link
Contributor Author

cgfarmer4 commented Jan 8, 2021

@spacether I understand the strictly typed issue. The problem is with the JSON it creates.

What version of openapi-generator are you using?

openapitools/openapi-generator-cli:v5.0.0

Have you tried passing in InitialProducts([Products('transactions')])?

The request body is as follows:

    request = CreateRequest(
        initial_products=[Products('transactions')]
    )

InitialProducts is not a model, its simply a request body parameter name.

@spacether
Copy link
Contributor

Can you provide the endoint definition also?

@cgfarmer4
Copy link
Contributor Author

SandboxPublicTokenCreateRequest:
  type: object
  properties:
    initial_products:
      type: array
      items:
        $ref: '#/components/schemas/Products'
            
/sandbox/public_token/create:
  post:
    requestBody:
      required: true
      description: ""
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SandboxPublicTokenCreateRequest'
            
Products:
  title: Products
  enum:
  - transactions
  type: string

@spacether
Copy link
Contributor

Thanks!

@spacether
Copy link
Contributor

spacether commented Jan 8, 2021

@cgfarmer4 can you try moving your initial_products definition into a component and $refing it?
EDIT: never mind I misunderstood your example. It is already refed.

@cgfarmer4
Copy link
Contributor Author

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

@spacether
Copy link
Contributor

All merged

@cgfarmer4
Copy link
Contributor Author

@spacether do you expect this to land in 5.1? Looks like I'll get a new docker build tomorrow so Ill pull latest but hoping to settle on a version in the next month or so.

@spacether
Copy link
Contributor

spacether commented Jan 10, 2021

The update is live in master branch now.
For a versioned release, this should land in 5.0.1. How do you use versions of openapi generator?

@cgfarmer4
Copy link
Contributor Author

Trying to stabilize on 5.0.0 as we have been using the betas to develop over the past few months. I'll make an exception for python and pull latest from docker CLI. Thanks again for jumping on this promptly!

@spacether
Copy link
Contributor

You're welcome. Thank you for submitting the issue. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants