Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

[BUG][python] Generated code references classes that are not declared yet #276

@BaptisteSaves

Description

@BaptisteSaves

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
Description

When using the generated client from the following yaml, I got an error in file paginated_result_tax_collection_dto.py that the class is not defined.

NameError: name 'ResultsTupleInput' is not defined

If we check the file paginated_result_tax_collection_dto.py , we can see the ResultTuple class is declared after its use

XXX

PaginatedResultTaxCollectionDtoDictInput = typing.TypedDict(
    'PaginatedResultTaxCollectionDtoDictInput',
    {
        "count": int,
        "results": typing.Union[
            ResultsTupleInput,
            ResultsTuple
        ],
    }
)
XXX

class ResultsTuple(
    typing.Tuple[
        my_object_dto.MyObjectDtoDict,
        ...
    ]
):

    def __new__(cls, arg: typing.Union[ResultsTupleInput, ResultsTuple], configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None):
        return Results.validate(arg, configuration=configuration)
openapi-json-schema-generator version

3.3.0

OpenAPI declaration file content or url

The original yaml was generated by FastAPI

{
  "openapi": "3.1.0",
  "info": {
    "title": "my-service",
    "version": "0.1.0"
  },
  "paths": {
    "/api/hello": {
      "get": {
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PaginatedResult_MyObjectDto_"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "PaginatedResult_MyObjectDto_": {
        "properties": {
          "count": {
            "type": "integer",
            "title": "Count"
          },
          "results": {
            "items": {
              "$ref": "#/components/schemas/MyObjectDto"
            },
            "type": "array",
            "title": "Results"
          }
        },
        "additionalProperties": false,
        "type": "object",
        "required": [
          "count",
          "results"
        ],
        "title": "PaginatedResult[MyObjectDto]"
      },
      "MyObjectDto": {
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid",
            "title": "id"
          }
        },
        "additionalProperties": false,
        "type": "object",
        "title": "MyObjectDto"
      }
    }
  }
}
Generation Details

Generated through docker:

docker run --rm -v ./:/local openapijsonschematools/openapi-json-schema-generator-cli:3.3.0 generate -i /local/my-service.yml -g python -o /local/out

Steps to reproduce

Generate the client, import it and try to call the api, the code does not start

Suggest a fix

The class is actually defined, it seems the order is the problem

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions