简体   繁体   English

来自 OpenAPI yaml 文件的 FastAPI 架构

[英]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.我正在寻找一种使用现有 OpenAPI YAML 架构文件初始化FastAPI服务器的方法。

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.文档确实描述了如何修改自动生成的模式,但我正在寻找类似Connexion 的东西,它可以即时生成验证器。

  • 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)可以在此处找到从 OpenAPI 架构文件生成 FastAPI 项目的工具,但在我的情况下不起作用(该项目仍处于实验阶段)

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: openapi.yaml 中匹配的对象模式:

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

ModelName is not mandatory. ModelName不是必需的。

After resolving the issue with fastapi-code-generator , I've opted to use it.在使用fastapi-code-generator解决问题后,我选择使用它。

For future readers, who use Python 3.7 , the issue was a missing import in the generated models.py file:对于使用Python 3.7 的未来读者,问题是生成的models.py文件中缺少导入:

from __future__ import annotations

Adding it at the top of models.py resolved the issue.models.py的顶部添加它解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM