繁体   English   中英

如何从 OpenAPI 3 YAML 文件生成 Python 字典?

[英]How to generate Python dictionary from OpenAPI 3 YAML file?

是否有可以读取 OpenAPI 3 YAML 文件并将其转换为 Python 字典的 Python 3 包或代码片段?

这是 OpenAPI 3 YAML 文件中的内容示例

openapi: 3.0.0
servers:
    url: https://localhost/api
paths:
  /user/{userId}:
    get:
      summary: Get a user by ID
      parameters:
        - in: path
          name: userId
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
        name:
          $ref: '#/components/schemas/UserFullName'
        age:
          type: integer
    UserFullName:
      type: object
      properties:
        firstName:
          type: string
        lastName:
          type: string

我希望包/代码生成一个看起来像这样的 Python 字典

{
  "/user/{userId}": {
    "get": {
      "200": {
        "id": "integer",
        "name": {
          "firstName": "string",
          "lastName": "string"
        }
        "age" "integer"
      }
    }
  }
}

- - - 编辑 - - -

我已经尝试过 PyYaml,但是,它没有返回我所期望的。

我期待 '$ref' 键值对充当这样的字符串插值:

{
   "id":0,
   "name":{
      "firstName":"string",
      "lastName":"string"
   },
   "age":0
}

相反,PyYaml 正在返回:

{
  "id":0,
  "name": {
    "$ref": "#/components/schemas/UserFullName"
  },
  "age":0
}

是的,有一个名为pyyaml的包。 要了解更多信息,请参阅这篇文章How to Read YAML File to Dict in Python

暂无
暂无

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

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