Skip to content

Commit

Permalink
Implementation of #436 (#437)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Wiesner <Florian Wiesnerflorian.wiesner@oracle.com>
  • Loading branch information
FWiesner and Florian Wiesner authored Aug 23, 2021
1 parent 9a8680c commit 5ec08c7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/networknt/schema/AllOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
registerAndMergeDiscriminator(currentDiscriminatorContext, discriminator, parentSchema, at);
// now we have to check whether we have hit the right target
final String discriminatorPropertyName = discriminator.get("propertyName").asText();
final String discriminatorPropertyValue = node.get(discriminatorPropertyName).textValue();
final JsonNode discriminatorNode = node.get(discriminatorPropertyName);
final String discriminatorPropertyValue = discriminatorNode == null
? null
: discriminatorNode.textValue();

final JsonSchema jsonSchema = parentSchema;
checkDiscriminatorMatch(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/AnyOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
if (schema.getValidators().containsKey(typeValidatorName)) {
TypeValidator typeValidator = ((TypeValidator) schema.getValidators().get(typeValidatorName));
//If schema has type validator and node type doesn't match with schemaType then ignore it
//For union type, it is must to call TypeValidator
//For union type, it is a must to call TypeValidator
if (typeValidator.getSchemaType() != JsonType.UNION && !typeValidator.equalsToSchemaType(node)) {
allErrors.add(buildValidationMessage(at, typeValidator.getSchemaType().toString()));
continue;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/networknt/schema/BaseJsonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ protected static void checkDiscriminatorMatch(final ValidationContext.Discrimina
final ObjectNode discriminator,
final String discriminatorPropertyValue,
final JsonSchema jsonSchema) {
if (discriminatorPropertyValue == null) {
currentDiscriminatorContext.markMatch();
return;
}

final JsonNode discriminatorMapping = discriminator.get("mapping");
if (null == discriminatorMapping) {
checkForImplicitDiscriminatorMappingMatch(currentDiscriminatorContext,
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/networknt/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ public Set<ValidationMessage> validate(JsonNode jsonNode, JsonNode rootNode, Str
}

final String discriminatorPropertyName = discriminatorToUse.get("propertyName").asText();
final String discriminatorPropertyValue = jsonNode.get(discriminatorPropertyName).asText();
final JsonNode discriminatorNode = jsonNode.get(discriminatorPropertyName);
final String discriminatorPropertyValue = discriminatorNode == null ? null : discriminatorNode.asText();
checkDiscriminatorMatch(discriminatorContext,
discriminatorToUse,
discriminatorPropertyValue,
Expand Down
69 changes: 69 additions & 0 deletions src/test/resources/openapi3/discriminator.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,75 @@
"@type": "Bathroom"
},
"valid": false
},
{
"description": "discriminator missing - falling back to default validation with failure as @type is mandatory",
"data": {
},
"valid": false
}
]
},
{
"description": "absent discriminator value as discriminator marked as non-mandatory (which is against OAS3 recommendations)",
"schema": {
"anyOf": [
{
"$ref": "#/components/schemas/Room"
},
{
"$ref": "#/components/schemas/BedRoom"
}
],
"components": {
"schemas": {
"Room": {
"type": "object",
"properties": {
"intOrStringType": {
"type": "integer"
}
},
"discriminator": {
"propertyName": "@type",
"mapping": {
"bed": "#/components/schemas/BedRoom"
}
}
},
"BedRoom": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Room"
},
{
"type": "object",
"properties": {
"intOrStringType": {
"type": "string"
}
}
}
]
}
}
}
},
"tests": [
{
"description": "discriminator missing - falling back to default anyOf behavior",
"data": {
"intOrStringType": 4
},
"valid": true
},
{
"description": "discriminator missing - with constructed schema conflict",
"data": {
"intOrStringType": "cannot match due to allOf"
},
"valid": false
}
]
},
Expand Down

0 comments on commit 5ec08c7

Please sign in to comment.