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

Schema property required at level of a path action #730

Closed
ajfisher opened this issue Jul 11, 2016 · 3 comments
Closed

Schema property required at level of a path action #730

ajfisher opened this issue Jul 11, 2016 · 3 comments

Comments

@ajfisher
Copy link

I have a scenario where when I create an entity using a POST I need to have several required fields. However when I update it those fields may no longer be required (as I will have already used them) and as such they become optional.

My question then is that given the schema for the object is the same and I want to use a reference, how can I set properties of that referred schema to be required at the point of the action for the path.

EG (snippet)

swagger: '2.0'
info:
  version: "01"
host: localhost
basePath: /api/v1
paths:
  /user:
    post:
      description: Creates a new `User` entity, duplicates are not allowed
      parameters:
        - name: request
          in: body
          required: true
          schema:
            type: object
            required:
              - user // this makes the user object required and inherits the reqs for the User reference
            properties:
              user:
                $ref: '#/definitions/User'
    put:
      description: Updates a `User` given the appropriate `uuid`
      parameters:
        - name: uuid
          in: header
          description: UUID of the `User` to request
          required: true
          type: string
        - name: request
          in: body
          schema:
            $ref: '#/definitions/User'
definitions:
  User:
    type: object
    description: | 
      A `User` object describes a user entity.
    properties:
      email:
        type: string
        description: Must be email valid
      first_name:
        type: string
      last_name:
        type: string
      created_at:
        type: string
        format: date-time

What I want to be able to do is say in here:

        - name: request
          in: body
          required: true
          schema:
            type: object
            required:
              - user.email
              - user.firstname
            properties:
              user:
                $ref: '#/definitions/User'

For example so that the required aspect of this is set to the properties of the referenced object

Hopefully that makes sense and there's a way to do this?

@wparad
Copy link

wparad commented Jul 11, 2016

See issue #425

@ePaul
Copy link
Contributor

ePaul commented Jul 28, 2016

I guess, currently you could do something like this:

        - name: request
          in: body
          required: true
          schema:
            type: object
            properties:
              user:
                allOf:
                  - $ref: '#/definitions/User'
                  - type: object
                    required:
                      - email
                      - firstname

I'm not sure about the tooling support, it might be that you would have to repeat the required properties from the user object here.

@webron
Copy link
Member

webron commented Mar 3, 2017

@ePaul is right - that's the way to do it.

@webron webron closed this as completed Mar 3, 2017
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

4 participants