1

In Swagger, is it possible to reference a parent object's properties when defining what properties of a child object are required?

For example, given the following base class:

definitions:
  Pet:
    type: object
    properties:
      name:
        type: string
      owner:
        type: string

I would like to have two child classes: one for creating a Pet (where all properties are required in the payload) and another for updating a Pet (where none are required). I tried achieving this by doing:

  CreatePetRequest:
    allOf:
      - $ref: '#/definitions/Pet'
    required:
      - name
      - owner

  UpdatePetRequest:
    allOf:
      - $ref: '#/definitions/Pet'

However, this doesn't work. Instead, an exception is raised:

swagger_spec_validator.common.SwaggerValidationError: Required list has properties not defined: ['name', 'owner']

How can I achieve this? Is it even possible with Swagger?

ventolin
  • 2,971
  • 3
  • 21
  • 25
  • Possible duplicate of [Re-using model with different required properties](http://stackoverflow.com/questions/40839706/re-using-model-with-different-required-properties) – Helen Jan 09 '17 at 13:55

1 Answers1

0

I see the exact same issue with a particular version of swagger-spec-validator pip package, in my case it was the version 2.0.4 which fixed the issue. Install it using pip install swagger-spec-validator==2.0.4 or pin the package with version number in requirements.txt

Rahul Shaw
  • 539
  • 6
  • 11
  • Thanks for the tip. I was indeed on 2.0.3 when I tried this. I'll give it a go with 2.1.0 (the newest) later on and hopefully have more luck. – ventolin Nov 07 '17 at 11:43