Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the properties with the objects able to be required in certain situations #1664

Closed
jennaprice opened this issue Feb 6, 2018 · 4 comments

Comments

@jennaprice
Copy link

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:

/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

AND

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

@webron
Copy link
Contributor

webron commented Feb 15, 2018

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

@hkosova
Copy link
Contributor

hkosova commented Feb 16, 2018

@handrews
Copy link

@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

@shockey
Copy link
Contributor

shockey commented Mar 15, 2018

Closing this; looks like the relevant OpenAPI tickets have this covered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants