bavaza
bavaza

Reputation: 11057

FastAPI schema from an OpenAPI yaml file

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.

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.

Upvotes: 3

Views: 7380

Answers (1)

bavaza
bavaza

Reputation: 11057

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.

Upvotes: 4

Related Questions