Swagger-editor: Make the properties with the objects able to be required in certain situations

Created on 6 Feb 2018  路  4Comments  路  Source: swagger-api/swagger-editor

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

Expected Behavior

Basically I would like more reusability of Components

Current Behavior

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

Possible Solution

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)

Context

Mostly I find I am writing a creation object and a response body object.

Most helpful comment

@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

All 4 comments

Unfortunately, this is a limitation of JSON Schema - there's no real easy way of doing that.

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

variable picture variable  路  4Comments

gerbsen picture gerbsen  路  3Comments

ljerka picture ljerka  路  5Comments

gnieutin picture gnieutin  路  4Comments

confuser picture confuser  路  6Comments