OpenAPITools / OpenAPITools/openapi-generator

[BUG] [python-flask] Polymorphism causes imports to non-existent models

Open
#4,421 2 comments 0 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

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?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When attempting to use polymorphism as described here in the specification controllers will contain import statements to models which have not been generated

openapi-generator version

Tested with 4.1.3, as well as snapshots of 4.2.1 and 5.0.0. Issue exists in all tested versions.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Python polymorphism bug
  version: 1.0.0
paths:
  "/foo":
    post:
      responses:
        "200":
          description: "Success"
          content:
            application/json:
              schema:
                anyOf:
                  - $ref: "#/components/schemas/Bar"
                  - $ref: "#/components/schemas/Baz"
components:
  schemas:
    Foo:
      type: object
      properties:
        prop1:
          type: string
    Bar:
      allOf:
        - $ref: "#/components/schemas/Foo"
        - type: object
          properties:
            prop2:
              type: string
    Baz:
      allOf:
        - $ref: "#/components/schemas/Foo"
        - type: object
          properties:
            prop3:
              type: string
Command line used for generation

java -jar openapi-generator-cli-4.1.3.jar generate -g python-flask -i openapi.yml

Steps to reproduce
  1. Copy above yaml to openapi.yml
  2. Run command from above

openapi_server/controllers/default_controller.py now contains the line
from openapi_server.models.any_of_bar_baz import AnyOfBarBaz. AnyOfBarBaz is not a model which has been generated.

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 reproduced command and inspect openapi_server/controllers/default_controller.py, especially the generated import of AnyOfBarBaz. Trace how the anyOf schema is mapped to model imports, then verify the generated output no longer imports a model that was not generated using the supplied OpenAPI document.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.