Skip to content

[BUG] generate python-nextgen recursive allOf circular import #16248

Open
@Emptyless

Description

@Emptyless

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions