-
Notifications
You must be signed in to change notification settings - Fork 330
Closed
Description
It seems that when the anyOf is an array item, the validation is correct for the first item but for the remaining items, it validates against the first schema of the anyOf instead of using the discriminator.
To reproduce:
- schema.json
{
"type": "array",
"items": {
"anyOf": [
{
"$ref": "#/components/schemas/Kitchen"
},
{
"$ref": "#/components/schemas/BedRoom"
}
]
},
"components": {
"schemas": {
"Room": {
"type": "object",
"properties": {
"@type": {
"type": "string"
}
},
"required": [
"@type"
],
"discriminator": {
"propertyName": "@type"
}
},
"BedRoom": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Room"
},
{
"type": "object",
"properties": {
"numberOfBeds": {
"type": "integer"
}
},
"required": [
"numberOfBeds"
]
}
]
},
"Kitchen": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Room"
},
{
"type": "object",
"properties": {
"hasMicrowaveOven": {
"type": "boolean"
}
},
"required": [
"hasMicrowaveOven"
]
}
]
}
}
}
}
- input.json
[
{
"@type": "Kitchen",
"hasMicrowaveOven": true
},
{
"@type": "BedRoom",
"numberOfBeds": 4
}
]
- test case
@Test
void test() throws Exception {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
InputStream schemaStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("schema.json");
InputStream jsonStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("input.json");
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setOpenAPI3StyleDiscriminators(true);
JsonSchema schema = factory.getSchema(schemaStream, config);
JsonNode jsonNode = new ObjectMapper().readTree(jsonStream);
Set<ValidationMessage> errors = schema.validate(jsonNode);
assertEquals(errors.size(), 0);
}
Note: if we reverse order of the kitchen and the bedroom in the json input, then the test passes.
graemeberry-esgglobal