首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >开放API继承的示例数据

开放API继承的示例数据
EN

Stack Overflow用户
提问于 2017-12-04 18:45:18
回答 1查看 1.4K关注 0票数 1

我正在使用OpenAPI 3.0为我正在构建的服务定义API。我遇到了在其他组件中重用模式组件的问题。例如,我有一个Note对象,它包含创建便笺的人的Profile对象。这可以通过使用Profile关键字引用$ref对象来实现预期的效果。问题是当显示示例时,配置文件中没有任何数据,如果我将参考文件放在下面的示例中,它包括Profile的实际Profile块,而不仅仅是Profile组件的示例数据。

我想知道是否有一种方法可以在其他组件中重用组件,也可以重用这些组件上的示例?

Ex:

代码语言:javascript
复制
FullNote:
  allOf:
    - $ref: '#/components/schemas/BaseNote'
    - type: object
      title: A single note response
      required:
      - id
      - dateCreated
      - profile
      properties:
        id:
          type: integer
          format: int32
        dateCreated:
          type: integer
          format: int64
        profile:
          type: object
          $ref: '#/components/schemas/Profile'
      example:
        id: 123456789
        dateCreated: 1509048083045
        profile:
          $ref: '#/components/schemas/Profile'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-04 19:09:06

example关键字(不要与exampleS混淆)不支持$ref。整个示例需要内联指定:

代码语言:javascript
复制
    FullNote:
      allOf:
        - $ref: '#/components/schemas/BaseNote'
        - type: object
          ...
          example:
            id: 123456789
            dateCreated: 1509048083045
            influencer:
              prop1: value1  # <----
              prop2: value2

或者,您也可以使用属性级别的示例-在本例中,像Swagger这样的工具将从属性示例构建模式示例。

代码语言:javascript
复制
    FullNote:
      allOf:
        - $ref: '#/components/schemas/BaseNote'
        - type: object
          ...
          properties:
            id:
              type: integer
              format: int32
              example: 123456789      # <----
            dateCreated:
              type: integer
              format: int64
              example: 1509048083045  # <----
            profile:
              # This property will use examples from the Profile schema
              $ref: '#/components/schemas/Profile'
    Profile:
      type: object
      properties:
        prop1:
          type: string
          example: value1   # <----
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47639926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档