I couldn't see how to do this in the documentation, if you have a base object, but in different situations you want to make properties required, you have to rebuild the object.
| Q | A
| ----------------------------------- | -------
| Bug or feature request? | Feature Request
| Which Swagger/OpenAPI version? | 3.0
| Which Swagger-Editor version? | 3.2.7
| How did you install Swagger-Editor? | Both with in the browser following the (incorrect) instructions on your site and with the swagger offline editor electron app
| Which broswer & version? | Chrome Version 64.0.3282.140
| Which operating system? | MacOs
Basically I would like more reusability of Components
I want to reuse a part of a component and make the properties required in different situations.
in a /create post I will need to require the firstName lastName where as most other calls I will need to require the uuid
So according to the documentation I can overwrite properties, here is what I have tried:
```yaml:
/endpoint
post:
requestBody:
description: Create a Person
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/person'
required:
- firstName
- lastName
properties:
firstName:
type: string
lastName:
type: string
```yaml:
/endpoint
post:
requestBody:
description: Create a Person
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/person'
required:
- firstName
- lastName
example object:
#Components
components:
schemas:
person:
description: Person
#required:
#nothing because in different situations I need different things :(
properties:
uuid:
description: unique object identifier for the person
type: string
format: uuid
firstName:
type: string
lastName:
type: string
favoriteColor:
type: string
doB:
type: string
street:
type: string
phoneNumber:
type: string
If there was a syntax for the required, also as a response object, if I make a field within an required does that indicate that the value of that field in the object will always be present (like uuid for example)
Mostly I find I am writing a creation object and a response body object.
Unfortunately, this is a limitation of JSON Schema - there's no real easy way of doing that.
Related discussions in the OpenAPI Specification repository:
@jennaprice you need to put the $ref inside of an allOf. This limitation will be removed in JSON Schema draft-08, although it's not clear when or how OpenAPI will support newer drafts of JOSN Schema.
here's what your first two examples would look like:
/endpoint
post:
requestBody:
description: Create a Person
required: true
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/person'
required:
- firstName
- lastName
properties:
firstName:
type: string
lastName:
type: string
/endpoint
post:
requestBody:
description: Create a Person
required: true
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/person'
required:
- firstName
- lastName
Closing this; looks like the relevant OpenAPI tickets have this covered.
Most helpful comment
@jennaprice you need to put the
$refinside of anallOf. This limitation will be removed in JSON Schema draft-08, although it's not clear when or how OpenAPI will support newer drafts of JOSN Schema.here's what your first two examples would look like: