【问题标题】:OpenApi 3 import schemas from external filesOpenApi 3 从外部文件导入模式
【发布时间】:2018-05-04 16:35:29
【问题描述】:

我正在为 Web 服务定义通用模式,并且我想将它们导入规范的组件/模式部分。 我想创建一个跨多个服务通用的规范数据模型,以避免在每个服务定义中重新定义相似的对象。

有没有办法做到这一点? 是否有类似于 XSD 对其导入标签所做的机制?

【问题讨论】:

    标签: rest web-services xsd swagger openapi


    【解决方案1】:

    如果 Pet.yaml 不是一个普通对象而是有几个像这样的对象,则扩展 Helen 对那些从 Google 登陆这里的人的回答:

    components:
      schemas:
        pet:
          type: object
          properties:
            (...)
    

    你可以这样引用它:

    $ref: './common/Pet.yaml#/components/schemas/pet'
    

    【讨论】:

    • 但是如果我想包含一个文档而不是引用一个文档怎么办?它们是有区别的。我不想点击。这个问题也在 XML (XLink) 中得到解决。我希望在 OpenAPI 标准中看到类似的东西。
    • “我不想点击”是什么意思?
    【解决方案2】:

    您可以直接使用绝对或相对 URL $ref 外部 OpenAPI 架构对象:

    responses:
      '200':
        description: OK
        schema:
          $ref: './common/Pet.yaml'
          # or
          # $ref: 'https://api.example.com/schemas/Pet.yaml'
    

    Pet.yaml 包含的地方,例如:

    type: object
    properties:
      id:
        type: integer
        readOnly: true
      petType:
        type: string
      name:
        type: string
    required:
      - id
      - petType
      - name
    

    更多信息请参见Using $ref

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 2018-04-06
      • 2020-12-22
      • 1970-01-01
      相关资源
      最近更新 更多