Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • 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)

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.

like image 344
bavaza Avatar asked Dec 19 '25 12:12

bavaza


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.

like image 113
bavaza Avatar answered Dec 22 '25 03:12

bavaza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!