我有一个路径,它使用每个http方法具有几乎相同属性的复杂模型.问题是我想为PUT和POST的请求定义一些必需的属性,而GET响应中不需要属性(因为服务器总是返回所有属性,并且在文档的其他地方提到它).
我创建了一个简单的cat API来演示我尝试过的东西.我们的想法是,对于GET响应,响应模型没有标记为必需的任何内容,但PUT的请求必须具有猫的名称.
swagger: "2.0"
info:
title: "Cat API"
version: 1.0.0
paths:
/cats/{id}:
parameters:
- name: id
in: path
required: true
type: integer
get:
responses:
200:
description: Return a cat
schema:
$ref: "#/definitions/GetCat"
put:
parameters:
- name: cat
in: body
required: true
schema:
$ref: "#/definitions/PutCat"
responses:
204:
description: Cat edited
definitions:
Cat:
type: object
properties:
name:
type: string
GetCat:
allOf:
- $ref: "#/definitions/Cat"
properties:
id:
type: integer
PutCat:
type: object
required:
- name
properties:
$ref: "#/definitions/Cat/properties"
Run Code Online (Sandbox Code Playgroud)
Swagger编辑说这是一个有效的规范,但是name …
我正在用yaml写出我的招摇定义.假设我的定义看起来像这样.
paths:
/payloads:
post:
summary: create a payload
...
parameters:
- in: body
name: payload
description: New payload
required: true
schema:
$ref: "#/definitions/payload"
put:
summary: update a payload
...
parameters:
- in: body
name: payload
description: Updated existing payload
required: true
schema:
$ref: "#/definitions/payload"
...
definitions:
payload:
properties:
id:
type: string
someProperty:
type: string
...
Run Code Online (Sandbox Code Playgroud)
有没有办法可以指示PUT操作需要有效负载的id属性,并且POST操作是可选的(或者根本不应该出现)?