Reputation: 1598
There's a year old question with a similar title here, but the only answer does not answer the question fully so hoping that things have changed since I'm trying again.
I've a model of a Car
which I would like to use with GET
requests for creating new instances in which I require to have name
.
Additionally, I would like to use this with PUT
requests to update an existing instance. Update requests should not have all properties, the Car
identifier is already specified in the path and only the present properties would be 'merged' into the existing object.
I tried using the following spec:
CarProperties:
type: object
properties:
name:
type: string
title: Car Name
description: A friendly name for this car.
minLength: 1
md:
type: object
hidden: 'true'
description:
type: string
title: Car Description
description: A detailed description of the car.
md:
type: object
hidden: 'true'
UpdateCar:
allOf:
- { $ref: '#/definitions/CarProperties' }
type: object
NewCar:
allOf:
- { $ref: '#/definitions/CarProperties' }
type: object
required: [name]
I expected having name
a required property only in NewCar
, however in both NewCar
and UpdateCar
the property name
is optional.
How can I specify a subset of required properties from a referenced model?
Upvotes: 1
Views: 869
Reputation: 1598
After fiddling for a while it seems like I had bad indentation for the required
field. The following syntax seems to produce what I wanted:
CarProperties:
type: object
properties:
name:
type: string
title: Car Name
description: A friendly name for this car.
minLength: 1
md:
type: object
hidden: 'true'
description:
type: string
title: Car Description
description: A detailed description of the car.
md:
type: object
hidden: 'true'
UpdateCar:
allOf:
- { $ref: '#/definitions/CarProperties' }
- type: object
NewCar:
allOf:
- { $ref: '#/definitions/CarProperties' }
- type: object
required: [name]
Now I have:
NewCar
entity with a required name
field.UpdateCar
entity with an optional name
field.Car
I only need to do it in one place.Upvotes: 3