swagger - 如何根据 http 方法要求字段?

标签 swagger swagger-2.0

我正在尝试写一个 JSON API使用 swagger,我面临一些问题,因为某些字段是否需要取决于所使用的方法。

这是更新规范中的一行

The resource object MUST contain type and id members.



这是创建规范中的一个

The resource object MUST contain at least a type member.



所以目前我有
Customer: 
    title: Customer
    type: object
    required: ["id", "type"]
    properties:
      id:
        type: string
      type:
        type: string
      attributes:
        type: object
        properties:
          name:
            type: string
          address: 
            type: string

我想做的是required: ["id", "type"]patch方法但只有 required: ["type"]post方法。是否可以在不重新定义另一个客户的情况下做到这一点?

最佳答案

您可以使用以下方法实现这一点:

  Customer: 
    title: Customer
    type: object
    required:
      - type
    properties:
      id:
        type: string
      type:
        type: string
      attributes:
        type: object
        properties:
          name:
            type: string
          address: 
            type: string
  Customer_patch: 
    type: object
    required:
      - id
    allOf:
      - $ref: '#/definitions/Customer'

您必须重新定义另一个客户以放置额外的必填字段,但您可以重用实际的客户定义对象。

关于swagger - 如何根据 http 方法要求字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39343387/

相关文章:

Swagger:从枚举中获取一个或多个值

java - 如何配置 swagger 来处理自定义 Controller 级路径变量注释?

api - openapi中如何定义对象中的字段是唯一的?

c# - swagger、.net core api 中的响应内容类型

go - 如何在 Swagger 中创建前缀路由

spring-boot - Swagger 的 @ApiResponses 和 @ApiResponses

java - 如何在 Rest Api 调用中设置多个响应类型

asp.net-web-api - 从 web-api 返回到 Swagger-ui 的位置响应头

swagger-2.0 - 未发送基本 Auth header (Swagger)

java - 无法使用 <url>/api-docs 访问 swagger 文档