FastAPI schema from an OpenAPI yaml file

Viewed 2575

I'm looking for a way to initialize a FastAPI server, with an existing OpenAPI YAML schema file.

The docs do describe how to modify the auto-generated schema, but I'm looking for something like Connexion, which can generate the validators on-the-fly.

  • a related question can be found here
  • a tool to generate a FastAPI project from an OpenAPI schema file can be found here, but did not work in my case (the project is still experimental)

Edit

I've traced down the offending property to this one:

class MyParameters(BaseModel):
    ...
    ModelName: Optional[ModelName] = Field(None, description='')
    ...

The matching object schema in openapi.yaml:

MyParameters:
    type: object
    properties:
        ...
        ModelName:
            type: string
            enum: [XYZ]
            description: ""
        ...

ModelName is not mandatory.

1 Answers

After resolving the issue with fastapi-code-generator, I've opted to use it.

For future readers, who use Python 3.7, the issue was a missing import in the generated models.py file:

from __future__ import annotations

Adding it at the top of models.py resolved the issue.

Related