我正在尝试写一个 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/