anyOf Support
While anyOf contains a list of schemas, like oneOf, a value may match one or more of the schemas in the list, while with oneOf the value may match just one of the schemas in the list. This results in subtle differences in how anyOf support has been implemented, which are documented here.
Test generation
When generating tests for anyOf, one test is generated per schema, and if the schemas are JSON objects, one test at the end is generated from all the schemas in the list.
For example, consider the following schema:
paths:
/example:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
data:
anyOf:
- type: object
properties:
name:
type: string
- type: object
properties:
age:
type: integer
The following test requests would be generated:
- a test with
datacontaining anameproperty of type string - a test with
datacontaining anageproperty of type integer - a test with
datacontaining bothname(string) andage(integer) properties
The last at the end is not generated when generating tests for oneOf.
Object generation
When generating a JSON object from a schema that contains anyOf, 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 testing.
Validation
Consider the same schema as above.
The following values would match the anyOf, because they conform to at least one of the schemas in the anyOf list:
{
"data": {
"name": "Alice"
}
}
{
"data": {
"age": 30
}
}
{
"data": {
"name": "Alice",
"age": 30
}
}
However, the following value would not match the anyOf, because it contains an undeclared key gender:
{
"data": {
"name": "Alice",
"age": 30,
"gender": "female"
}
}
Note: if a key in an object exists in multiple available schemas in an anyOf, but matches none of the types, it won’t match the anyOf.
Consider the following schema:
type: object
properties:
data:
anyOf:
- type: object
properties:
value:
type: string
- type: object
properties:
value:
type: integer
The following value would not match the anyOf, because the value key exists in both schemas, but matches neither type:
{
"data": {
"value": true
}
}