Re-using model with different required properties
In your example, you can use a single model for both GET and POST/PUT, with properties only used in the GET response marked as readOnly
. From the spec:
readOnly
Declares the property as "read only". This means that it MAY be sent as part of a response but MUST NOT be sent as part of the request. Properties marked as readOnly being true SHOULD NOT be in the required list of the defined schema. Default value is false.
The spec would look like:
get:
responses:
200:
description: Return a cat
schema:
$ref: "#/definitions/Cat"
put:
parameters:
- name: cat
in: body
required: true
schema:
$ref: "#/definitions/Cat"
responses:
204:
description: Cat edited
definitions:
Cat:
properties:
id:
type: integer
readOnly: true
name:
type: string
breed:
type: string
required:
- name
- breed
This means you must PUT the name
and breed
:
{
"name": "Puss in Boots",
"breed": "whatever"
}
and GET /cats/{id}
must return the name
and breed
and may also return the id
:
{
"name": "Puss in Boots",
"breed": "whatever",
"id": 5
}
I've had a similar problem. Turned out that my indentation was wrong. The following syntax should work:
PutCat:
allOf:
- $ref: "#/definitions/Cat"
- type: object
required: [name]