OpenAPITools / OpenAPITools/openapi-generator

[BUG] [python-flask] [server] oneOf, anyOf Polymorphism does not work

Open
#7,815 0 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Python
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Same as https://github.com/OpenAPITools/openapi-generator/issues/7414, but with python-flask.

oneOf or anyOf elements cause invalid python code generated.

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  [...]
  File "/usr/src/app/openapi_server/controllers/default_controller.py", line 4, in <module>
    from openapi_server.models.one_of_cat_dog import OneOfCatDog  # noqa: E501
ModuleNotFoundError: No module named 'openapi_server.models.one_of_cat_dog'
openapi-generator version

4.3.1

OpenAPI declaration file content or url
openapi: "3.0.2"
info:
  version: 1.0.0
  title: Polymorphism
paths:
  /pets:
    post:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
              discriminator:
                propertyName: pet_type
      responses:
        '200':
          description: created
components:
  schemas:
    # Parent
    Pet:
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
        eats:
          type: string
      discriminator:
        propertyName: pet_type
    # Child 1
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: boolean
    # Child 2
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            climbs:
              type: boolean
Generation Details
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 generate --input-spec /local/openapi.yaml --generator-name python-flask --output /local
Steps to reproduce
  1. Run the above generation
  2. docker build -t openapi_server .
  3. docker run -p 8080:8080 openapi_server
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/6815
https://github.com/OpenAPITools/openapi-generator/issues/5565
https://github.com/OpenAPITools/openapi-generator/pull/7789

Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied openapi.yaml and inspect the generated default_controller.py alongside the generated model files. Reproduce the python-flask generation with the documented Docker command, then trace why the one_of_cat_dog import refers to a missing module. Done means the generated server builds and starts without ModuleNotFoundError for the oneOf/anyOf schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, openapi, python
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.