Skip to main content

oneOf Support

oneOf contains a list of schemas. A value may match one of the items in this list, and each item will be tested as described below.

Test generation

When generating tests for oneOf, one test is generated per schema.

For example, consider the following schema:

paths:
/example:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
data:
oneOf:
- type: object
properties:
name:
type: string
- type: object
properties:
age:
type: integer

If there are no examples of this, or if the specmatic config contains schemaResiliencyTests: all (read more about boundary condition testing), the following test requests would be generated:

  1. a test with data containing a name property of type string
  2. a test with data containing an age property of type integer

Object generation

When generating a JSON object from a schema that contains oneOf, one schema is selected at random and an object is generated from it based on that schema. This applies to example generation as well as response generation during mocking.

Validation

Consider the same schema as above.

The following values would match the oneOf, because they conform to at one of the schemas in the oneOf list:

{
"data": {
"name": "Alice"
}
}
{
"data": {
"age": 30
}
}

However, the following value would not match the oneOf, because it contains an undeclared key gender:

{
"data": {
"name": "Alice",
"gender": "female"
}
}