Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 of 6 tasks
BaptisteSaves opened this issue Nov 7, 2023 · 1 comment · Fixed by #277
Closed
5 of 6 tasks

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

BaptisteSaves opened this issue Nov 7, 2023 · 1 comment · Fixed by #277

Comments

@BaptisteSaves
Copy link

BaptisteSaves commented Nov 7, 2023

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

@spacether spacether linked a pull request Nov 7, 2023 that will close this issue
3 tasks
@spacether
Copy link
Contributor

spacether commented Nov 7, 2023

@BaptisteSaves thank you for reporting this
I fixed it by ensuring that schemas are written after their dependent schemas and enforcing that when $refs are used in deeper inline schemas.
Just now I released the fix in v3.3.1 in docker hub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants