不,不可能在同一个路径项中为同一个操作定义两个不同的主体参数。路径项名称是唯一的,因为它是属性名称(因此是 JSON 键值映射中的“键”),并且 Swagger 规范在给定操作中最多允许一个主体参数。
有几种替代方法可以满足这种需求:
创建两个单独的路径项
例如:
/pets/createFromDescription:
post:
summary: Create a pet from a basic description
operationId: createPetFromDescription
parameters:
- name: petDescription
in: body
required: true
schema:
$ref: "#/definitions/PetDescriptionObject"
responses:
200:
description: OK
/pets/createFromCatalog:
post:
summary: Create a pet from a standard catalog entry
operationId: createPetFromCatalogEntry
parameters:
- name: petCatalogEntry
in: body
required: true
schema:
$ref: "#/definitions/PetCatalogEntry"
responses:
200:
description: OK
使用鉴别器创建子类型
在此处的 Swagger–OpenAPI 2.0 规范中进行了描述。
例子:
/pets:
post:
summary: Create a pet
operationId: createPet
parameters:
- name: petSource
in: body
description: Structured pet information, from which to create the object
required: true
schema:
$ref: "#/definitions/CreatePet_Request"
responses:
200:
description: OK
definitions:
CreatePet_Request:
type: object
properties:
requestVariant:
type: string
required:
- requestVariant
discriminator: requestVariant
PetDescriptionObject:
allOf:
- $ref: "#/definitions/CreatePet_Request"
- type: object
properties:
name:
type: string
description:
type: string
PetCatalogEntry:
allOf:
- $ref: "#/definitions/CreatePet_Request"
- type: object
properties:
SKU:
type: string
Breed:
type: string
Comments:
type: string
这些方法都不是针对请求的媒体类型的。虽然您的请求可能接受多种媒体类型,但如果使用特定媒体类型意味着对请求正文的某些要求,您必须在description
操作或正文参数的属性中记录这些要求。