OpenAPITools / OpenAPITools/openapi-generator

[BUG] generate python-nextgen recursive allOf circular import

Open
#16,248 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
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)?
  • 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

When using a recursive reference, the python-nextgen generator creates a client which has a circular import.

openapi-generator version

6.4, 6.5, 6.6, 7.0-beta

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
  "info": {
    "title": "python-allof-circular-import-bug",
    "description": "python-allof-circular-import-bug",
    "version": "0.1.0"
  },
  "servers": [
    {
      "url": "/",
      "description": "Default server URL"
    }
  ],
  "paths": {
    "/": {
      "get": {
        "summary": "/",
        "description": "/",
        "operationId": "root",
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "SchemaOne": {
        "required": [
          "field_one",
          "field_two"
        ],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/SchemaTwo"
          },
          {
            "type": "object",
            "properties": {
              "field_one": {
                "type": "string"
              },
              "field_two": {
                "type": "string"
              }
            }
          }
        ]
      },
      "SchemaTwo": {
        "required": [
          "field_three"
        ],
        "type": "object",
        "properties": {
          "field_three": {
            "type": "string"
          }
        },
        "description": "",
        "discriminator": {
          "propertyName": "field_three"
        },
        "anyOf": [
          {
            "$ref": "#/components/schemas/SchemaOne"
          }
        ]
      }
    }
  }
}
Generation Details
Steps to reproduce

run java -jar openapi-generator-cli.jar generate --config openapi-config.json --input-spec openapi.json --generator-name python-nextgen --global-property skipFormModel=false

Related issues/PRs
Suggest a fix

My proposal would be to import the models only for type checking purposes:

# model_anyof.mustache
...
{{#vendorExtensions.x-py-model-imports}}
if typing.TYPE_CHECKING:
    {{{.}}}
{{/vendorExtensions.x-py-model-imports}}
...

instead of

# model_anyof.mustache
...
{{#vendorExtensions.x-py-model-imports}}
{{{.}}}
{{/vendorExtensions.x-py-model-imports}}

And change the type check to be name based:

...
if type(v).__name__ != '{{{dataType}}}':
            error_messages.append(f"Error! Input type `{type(v)}` is not `{{{dataType}}}`")
...

instead of

...
if type(v) is not {{{dataType}}}:
            error_messages.append(f"Error! Input type `{type(v)}` is not `{{{dataType}}}`")
...

I've tested this successfully locally but looking for feedback if this is the right direction before creating a PR to resolve the issue.

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 provided OpenAPI declaration and reproduce the issue using the Java CLI command with the python-nextgen generator. Inspect model_anyof.mustache and the generated model imports and type checks. Done means the recursive allOf/anyOf specification generates a Python client without circular imports while retaining the expected validation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, python
Domain
api, 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.