I have a User
resource:
I want to define a PATCH /users/{uid}
so that the client can update image
, bio
or both.
An example valid request body would be:
{
"image": "filename.jpg",
"bio": "My biography"
}
If the image
property is sent alone the existing bio
property will remain the same on the server and only the image will be updated. If both are sent (as above) both will change.
In short:
an empty request body
{}
is disallowed.{"image": "new.jpg"}
,{"bio": "new bio"
or{"image": "new.jpg", "bio": "new bio"
is allowed.
This is what I have so far. I'm using the anyOf
object with two separate type: object
s within. I've tried this with Swagger hub using the virtserver, but the virtual server always seems to return 200 OK and passes back the example data regardless of whatever is passed, so I have no way to know.
Does my definition do what I intended? If not, what the best practice?
openapi: 3.0.0
...
patch:
summary: update a user
parameters:
- in: path
name: uid
description: user id
schema:
type: string
required: true
requestBody:
description: Update a user's profile
content:
application/json:
schema:
type: object
anyOf:
- type: object
properties:
image:
type: string
- type: object
properties:
bio:
type: string
additionalProperties: false
required: true
responses:
'200':
description: Successfully updated
content:
application/json:
schema:
$ref: '#/components/schemas/User'