【问题标题】:Reusable component outside of components/schema?组件/模式之外的可重用组件?
【发布时间】:2018-05-08 15:28:50
【问题描述】:

components/schema之外还有其他地方可以放置可重用组件吗?

我有一个“部分”(因为没有更好的术语)想在其他地方$ref,但我不想成为“模型”:

一些例子:

NotFoundError:
  type: object
  properties:
    code:
      type: integer
      format: int32
      example: 404
    message:
      type: string
      example: "Resource Not Found"
ID-Array:
  type: array
  items:
    $ref: '#/components/schemas/ID'
  writeOnly: true
ID:
  type: integer
  format: int32
  example: 1
ID-ReadOnly:
  allOf:
    - $ref: '#/components/schemas/ID'
    - readOnly: true
ID-WriteOnly:
  allOf:
    - $ref: '#/components/schemas/ID'
    - writeOnly: true

【问题讨论】:

    标签: swagger openapi


    【解决方案1】:

    一种可能的解决方案是将部分放在某个extension key 下,比如x-partials,并相应地更改$refs。 $ref 不关心路径是什么,只要目标是预期格式即可。

    x-partials:
      ID:
        type: integer
        format: int32
        example: 1
    
    components:
      schemas:
        ID-Array:
          type: array
          items:
            $ref: '#/x-partials/ID'
          writeOnly: true
        ID-ReadOnly:
          allOf:
            - $ref: '#/x-partials/ID'
            - readOnly: true
        ID-WriteOnly:
          allOf:
            - $ref: '#/x-partials/ID'
            - writeOnly: true
    

    这样,例如,部分将不会显示在 Swagger UI 的“模型”部分中。

    【讨论】:

      猜你喜欢
      • 2014-10-30
      • 2014-01-27
      • 1970-01-01
      • 2013-07-18
      • 2020-10-13
      • 2018-05-18
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多