Skip to content

Can optional properties be made required when "allOf" and "discriminator" are used? #1870

@jeff9finger

Description

@jeff9finger

swagger-api/swagger-editor#1212 seem to imply that the following is legal in Open API v2 and v3.
(This is a little different than the example in the git issue because the one there is not technically correct)

  Foo:
    type: object
    properties:
      req2:
        type: string
      req3:
        type: string
      opt1:
        type: string
      opt2:
        type: string

  Bar:
    allOf:
      - $ref: '#/definitions/Foo'
      - type: object
        required:
          - req1
          - req2
          - req3
        properties:
          req1:
            type: string

But I am wondering if anyone knows whether adding a discriminator into the mix changes the behavior.

  FooType:
    type: string
    enum:
    - a
    - b
    - c

  Foo:
    type: object
    properties:
      id:
        type: string
  
  Bar:
    allOf:
      - $ref: '#/definitions/Foo'
      - type: object
        discriminator: foo_type
        required:
          - foo_type
        properties:
          foo_type:
            $ref: '#/definitions/FooType'
          optional1:
            type: string
  
  FooBar:
    allOf:
      - $ref: '#/definitions/Bar'
      - type: object
        required:
          - optional1
        properties:
          optional2:
            type: string

#1428 provides a lot of good information, but does not seem to explain this particular scenario - FooBar declares a property in the "required" list that exists in one of the other objects.

Is the above spec valid? Would it set "optional1" as required in FooBar and not required in Bar?

Thanks for any clarification.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions