oneOf validation fails happy path case
Given the following schema:
openapi: 3.0.0
info:
title: Validation
description: >-
version: 1.0.0
servers:
- url: 'http://test.com'
description: QATE
paths:
/test:
get:
summary: placeholder summary
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/test'
components:
schemas:
test:
oneOf:
- $ref: '#/components/schemas/typeA'
- $ref: '#/components/schemas/typeB'
typeA:
type: object
properties:
typeAValueA:
type: string
typeB:
type: object
properties:
typeBValueB:
type: string
and the test response
{
"typeAValueA": "good"
}
We expect the validation to pass.
The validation fails with the following message:
Instance failed to match exactly one schema (matched 2 out of 2)
We are using:
.withLevelResolver(LevelResolverFactory.withAdditionalPropertiesIgnored())
When we execute the same validation directly against the same JSON schema validator library used by this tool we get the expected success result.
The JSON schema we use in this case is:
{
"oneOf":
[
{
"type": "object",
"additionalProperties": false,
"properties":
{
"typeAValueA":
{
"type": "string"
}
}
},
{
"type": "object",
"additionalProperties": false,
"properties":
{
"typeBValueB":
{
"type": "string"
}
}
}
],
"$schema": "http://json-schema.org/draft-04/schema#"
}
The inclusion of additionalProperties, in this case, is irrelevant due to the match of the response payload of the test response to the typeAValueA field and type.
Comments (3)
-
-
reporter Confirmed. You are correct.
additionalProperties: false
Corrects the issue.
-
reporter - changed status to resolved
Confirmed that proposed solution resolves the issue. Evaluated the JSON schema evaluator without additionalProperties: false and experienced the same issue. Solution is documented in reply to original ticket.
- Log in to comment
Thanks for raising this.
I think I can see what’s happening. Your schema does not set
additionalProperties: false
explicitly, and because you have set the additional properties validation to “ignored” the validator does not automatically inject these for you.Also, none of your properties are marked as required in the schema.
Because of that, both
typeA
andtypeB
schemas will actually match any input (e.g. they have no required properties and allow any additional properties to be present). Because of that theoneOf
validation result is actually correct.Some things you could try:
additionalProperties: false
to yourtypeA
andtypeB
schemas so that they do not accept arbitrary propertiesLet me know if that helps.