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 client: response parsing fails for array of anyOf items #6546

Closed
4 of 5 tasks
hleb-albau opened this issue Jun 4, 2020 · 8 comments
Closed
4 of 5 tasks
Labels
Client: Python Inline Schema Handling Schema contains a complex schema in items/additionalProperties/allOf/oneOf/anyOf Issue: Bug

Comments

@hleb-albau
Copy link

Bug Report Checklist

  • [] Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used? latest master
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output? error during result parsing from server
  • [Optional] Bounty to sponsor the fix (example)
Description

If openapi definition contains model of type array with anyOf items description, generated Python client can't parse request from server. Generated model has openapi_types with next type list[AnyOfFirstSecondThird]. During response parsing client takes one by one list items and try to de-serialize to non existing type klass AnyOfFirstSecondThird. Parsing fails at klass = getattr(openapi_client.models, klass) line with next error - module 'openapi_client.models' has no attribute 'AnyOfFirstSecondThird'

openapi-generator version

latest master, 5.0.0-SNAPSHOT

OpenAPI declaration file content or url
    ContainerList:
       type: array
       items:
         anyOf:
           - $ref: "#/components/schemas/FirstResultType"
           - $ref: "#/components/schemas/SecondResultType"
           - $ref: "#/components/schemas/ThirdResultType"
Command line used for generation
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i /local/docs/openapi.yml -g python -o /local/clients/python
Steps to reproduce
Related issues/PRs
Suggest a fix
@auto-labeler
Copy link

auto-labeler bot commented Jun 4, 2020

👍 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 Jun 4, 2020

@hleb-albau have you tried using the python-experimental generator?
The python generator does not fully implement composed schemas but the python-experimentalgenerator does.
You can see tests of the anyOf class gmFruit here
That class is defined here
and is defined as:

    gmFruit:
       properties:
          color:
             type: string
       anyOf:
          - $ref: '#/components/schemas/apple'
          - $ref: '#/components/schemas/banana'
       additionalProperties: false

@hleb-albau
Copy link
Author

hleb-albau commented Jun 4, 2020

@spacether
I tried to use python-experimental and got same results, but with slight different error - KeyError sys.modules[model.any_of_first_second_third']. And of course, model package does not contain any_of_first_second_third.
Also, the problem not directly related to case tested by gmFruit , but rather combination of array and it's type definition.

Btw, I tried online swagger editor and generate another version on client. Seems it is generated by swagger-generator. Using that client I get lucky with response parsing. But in the middle of de-serialization anyOf types were lost and replaces by raw dict.

I should prepare some full example, need some time.

@spacether
Copy link
Contributor

spacether commented Jun 4, 2020

Ah, I was missing that this was an array model. I though that it was an object type model. Can you provide the referenced anyOf models?

  • FirstResultType
  • SeconResultType
  • ThirdResultType

We don't yet have code coverage of this use case in python-experimental.

@spacether
Copy link
Contributor

spacether commented Jun 4, 2020

In the short term, you could make an anyOf model which contains your anyOf definition. Then in your array model, just $ref to your new anyof model. That should work in python-experimental.

We also had a PR that just landed which creates arrayModels in python-experimental so

  • you will no longer get the import error
  • but your example may or may not work, not sure if our array model PR fixes your use case

@spacether
Copy link
Contributor

spacether commented Mar 26, 2021

This issue's root cause is that inline composed schemas are not handled by generators at this time.
Our tooling assumes that composed schemas are defined in components.
A more general solution is to add composed schema CodegenProperties and have generators use that inline definition.
These issues have the same root cause:

In this proof of concept PR I show how we can add composed schema info to:
CodegenModel/CodegenProperty/CodegenParameter/CodegenResponse and then we can define these composed schemas at any level in generated code.
#8325
See this working in ComposedOneOfDifferentTypes here and see tests of it here

@spacether spacether added the Inline Schema Handling Schema contains a complex schema in items/additionalProperties/allOf/oneOf/anyOf label Mar 30, 2021
@spacether
Copy link
Contributor

spacether commented Oct 22, 2022

python-experimental became the default python client in v6.2.0
If you upgrade your python client to that version, this feature will work for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client: Python Inline Schema Handling Schema contains a complex schema in items/additionalProperties/allOf/oneOf/anyOf Issue: Bug
Projects
None yet
Development

No branches or pull requests

2 participants