swagger.io icon indicating copy to clipboard operation
swagger.io copied to clipboard

Examples of oneOf keyword are incorrect

Open adambordas opened this issue 5 years ago • 4 comments

The documentation that describes the oneOf keyword has wrong examples in it.

The specification looks like this:

  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
      responses:
        '200':
          description: Updated
components:
  schemas:
    Dog:
      type: object
      properties:
        bark:
          type: boolean
        breed:
          type: string
          enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:
      type: object
      properties:
        hunts:
          type: boolean
        age:
          type: integer

There are three examples:

  1. The following JSON object is valid against one of the schemas, so the response body is correct:

{
  "bark": true,
  "breed": "Dingo" 
}

Correction: The JSON object is valid for NOT ONLY one of the schemas. It indeed has the two properties of Dog, but it is also valid aginst Cat, as Cat has no required properties defined AND it also allows additionalProperties. (additionalProperties defaults to true according to https://swagger.io/specification/). So the validation will fail.

  1. The following JSON object is not valid against both schemas, so the response body is incorrect:

{
  "bark": true,
  "hunts": true
}

Correction: The JSON object is VALID against both schemas because neither Dog nor Cat defines required properties and both allow additional properties by default. So the validation will fail.

  1. The following JSON object is valid against both schemas, so the response body is incorrect – it should be valid against only one of the schemas, since we are using the oneOf keyword.

{
  "bark": true,
  "hunts": true,
  "breed": "Husky",
  "age": 3 		
}

Correction: The statement above is true, but the example is misleading. It is not valid for both schemas because it contains both schemas' properties. It is valid against both schemas because both Dog and Cat allow additional properties by default.

adambordas avatar Sep 20 '19 09:09 adambordas

Hi @slapers, you are correct. The Cat and Dog schemas need required properties in order for the oneOf schema to work properly.

https://github.com/swagger-api/swagger.io/issues/279#issuecomment-627305230

moxley avatar May 13 '20 20:05 moxley

Hi, this was recently reported to OpenAPI asking for clarification (https://github.com/OAI/OpenAPI-Specification/issues/3477). Does this project accept PRs?

iglosiggio avatar Dec 22 '23 20:12 iglosiggio

This had been confusing me... I don't see this repo containing that page, so I assume the community is not able to correct that.

vm-001 avatar Jan 26 '24 06:01 vm-001

See somehow related #348

zorba128 avatar May 15 '24 09:05 zorba128