如何扩展OpenAPI中的定义响应?

发布于 2025-02-13 01:29:26 字数 1322 浏览 0 评论 0原文

我想用架构或示例扩展“ 200successdefault”响应。

paths:
  /home:
      ...
      responses:
        200:
          $ref: '#/components/responses/200SuccessDefault'
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/PieChartElement'
              examples:
                PieChart:
                  $ref: '#/components/examples/PieChart_1'

此方法陷入错误,schema示例字段被忽略:

与$ refs一起的兄弟姐妹值被忽略。要将属性添加到$ REF中,请将$ Ref包装到Allof中,或将额外属性移至参考定义(如果适用)中。

我尝试了allof

paths:
  /home:
      responses:
        200:
          allOf:
            - $ref: '#/components/responses/200SuccessDefault'
            - content:
                application/json:
                  schema:
                    type: array
                    items:
                      $ref: '#/components/schemas/PieChartElement'
                  examples:
                    PieChart:
                      $ref: '#/components/examples/PieChart_1'

此方法陷入错误:

不应有其他属性额外的属性:allof
应具有所需的属性'描述'缺失:描述

I want to extend the "200SuccessDefault" response with a schema or example.

paths:
  /home:
      ...
      responses:
        200:
          $ref: '#/components/responses/200SuccessDefault'
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/PieChartElement'
              examples:
                PieChart:
                  $ref: '#/components/examples/PieChart_1'

This approach runs into an error, the schema and examples fields are ignored:

Sibling values alongside $refs are ignored. To add properties to a $ref, wrap the $ref into allOf, or move the extra properties into the referenced definition (if applicable).

I tried allOf:

paths:
  /home:
      responses:
        200:
          allOf:
            - $ref: '#/components/responses/200SuccessDefault'
            - content:
                application/json:
                  schema:
                    type: array
                    items:
                      $ref: '#/components/schemas/PieChartElement'
                  examples:
                    PieChart:
                      $ref: '#/components/examples/PieChart_1'

This approach runs into the error:

should NOT have additional properties additionalProperty: allOf
should have required property 'description' missingProperty: description

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷梦 2025-02-20 01:29:26

无法扩展引用的响应对象。但是,您可以使用共享的模式对象,并在模式中使用 Allof扩展了它。

在Allof内部,您可以放置​​:

  • 您的$ ref
  • 新型扩展默认响应

如果您想举一个示例,则将其放入“ application/json”中。

Openapi的一个例子是:

    "202":
      description: Extended response sample
      content:
        application/json:
          schema:
            allOf:
              - $ref: "#/components/schemas/application"
              - type: object
                properties:
                  anotherProperty:
                    type: string
                    maxLength: 200
                    example: "Property example"
          example: {id: 1234, anotherProperty: "Hello"}

You cannot extend a referenced response object. But, you can use a shared schema object and extend it utilizing allOf within schema.

Inside allOf you can put:

  • your $ref
  • a new type extending your default response

If you want to give an example of an entire extended response (JSON), just put it into "application/json".

An example of OpenAPI would be:

    "202":
      description: Extended response sample
      content:
        application/json:
          schema:
            allOf:
              - $ref: "#/components/schemas/application"
              - type: object
                properties:
                  anotherProperty:
                    type: string
                    maxLength: 200
                    example: "Property example"
          example: {id: 1234, anotherProperty: "Hello"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文