Semaphor
Semaphor

Reputation: 1040

Why are the OpenAPI 3 oneOf example objects not always valid against both schemas

The specification for the oneOf keyword in https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#oneof has an example schema as well as three example objects. The request body is oneOf the Dog or Cat schema:

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

And the three JSON objects:

Object 1

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

Object 2

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

Object 3

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

They say, the first object is valid against one of the schemas (I assume they mean the Dog schema), the second one is not valid against both schemas and the third is valid against both schemas. This means the second and third object are not correct request bodies.

But why are not all three objects valid against both schemas? None of the keywords for objects mentioned here http://json-schema.org/draft/2020-12/json-schema-validation.html#name-validation-keywords-for-obj are used. And the types of the properties match for all the objects.

I played with this validator https://www.jsonschemavalidator.net/ and wrote also a simple schema with an object with some properties and even an empty json validates against the schema.

Upvotes: 0

Views: 2397

Answers (1)

Semaphor
Semaphor

Reputation: 1040

It seems that my assumptions are correct and all objects are valid against the oneOf schema. I found an issue reporting this: https://github.com/swagger-api/swagger.io/issues/253.

Upvotes: 0

Related Questions