diff --git a/src/main/java/com/networknt/schema/AllOfValidator.java b/src/main/java/com/networknt/schema/AllOfValidator.java index dc2091c13..b45198244 100644 --- a/src/main/java/com/networknt/schema/AllOfValidator.java +++ b/src/main/java/com/networknt/schema/AllOfValidator.java @@ -35,6 +35,14 @@ public class AllOfValidator extends BaseJsonValidator { public AllOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext); + if (!schemaNode.isArray()) { + JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig()); + throw new JsonSchemaException(message().instanceNode(schemaNode) + .instanceLocation(schemaLocation.getFragment()) + .messageKey("type") + .arguments(nodeType.toString(), "array") + .build()); + } int size = schemaNode.size(); for (int i = 0; i < size; i++) { this.schemas.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i), diff --git a/src/main/java/com/networknt/schema/AnyOfValidator.java b/src/main/java/com/networknt/schema/AnyOfValidator.java index 931c4e33b..5dac253ac 100644 --- a/src/main/java/com/networknt/schema/AnyOfValidator.java +++ b/src/main/java/com/networknt/schema/AnyOfValidator.java @@ -37,6 +37,14 @@ public class AnyOfValidator extends BaseJsonValidator { public AnyOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF, validationContext); + if (!schemaNode.isArray()) { + JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig()); + throw new JsonSchemaException(message().instanceNode(schemaNode) + .instanceLocation(schemaLocation.getFragment()) + .messageKey("type") + .arguments(nodeType.toString(), "array") + .build()); + } int size = schemaNode.size(); for (int i = 0; i < size; i++) { this.schemas.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i), diff --git a/src/main/java/com/networknt/schema/OneOfValidator.java b/src/main/java/com/networknt/schema/OneOfValidator.java index 3ba564a44..1a49d986f 100644 --- a/src/main/java/com/networknt/schema/OneOfValidator.java +++ b/src/main/java/com/networknt/schema/OneOfValidator.java @@ -36,6 +36,14 @@ public class OneOfValidator extends BaseJsonValidator { public OneOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ONE_OF, validationContext); + if (!schemaNode.isArray()) { + JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig()); + throw new JsonSchemaException(message().instanceNode(schemaNode) + .instanceLocation(schemaLocation.getFragment()) + .messageKey("type") + .arguments(nodeType.toString(), "array") + .build()); + } int size = schemaNode.size(); for (int i = 0; i < size; i++) { JsonNode childNode = schemaNode.get(i); diff --git a/src/test/java/com/networknt/schema/AllOfValidatorTest.java b/src/test/java/com/networknt/schema/AllOfValidatorTest.java new file mode 100644 index 000000000..97d047665 --- /dev/null +++ b/src/test/java/com/networknt/schema/AllOfValidatorTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.networknt.schema; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +import com.networknt.schema.SpecVersion.VersionFlag; + +public class AllOfValidatorTest { + @Test + void invalidTypeShouldThrowJsonSchemaException() { + String schemaData = "{\r\n" + + " \"$defs\": {\r\n" + + " \"User\": true\r\n" + + " },\r\n" + + " \"allOf\": {\r\n" + + " \"$ref\": \"#/defs/User\"\r\n" + + " }\r\n" + + "}"; + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012); + JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData)); + assertEquals("type", ex.getValidationMessage().getMessageKey()); + } +} diff --git a/src/test/java/com/networknt/schema/AnyOfValidatorTest.java b/src/test/java/com/networknt/schema/AnyOfValidatorTest.java new file mode 100644 index 000000000..25ee04bef --- /dev/null +++ b/src/test/java/com/networknt/schema/AnyOfValidatorTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.networknt.schema; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +import com.networknt.schema.SpecVersion.VersionFlag; + +public class AnyOfValidatorTest { + @Test + void invalidTypeShouldThrowJsonSchemaException() { + String schemaData = "{\r\n" + + " \"$defs\": {\r\n" + + " \"User\": true\r\n" + + " },\r\n" + + " \"anyOf\": {\r\n" + + " \"$ref\": \"#/defs/User\"\r\n" + + " }\r\n" + + "}"; + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012); + JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData)); + assertEquals("type", ex.getValidationMessage().getMessageKey()); + } +} diff --git a/src/test/java/com/networknt/schema/OneOfValidatorTest.java b/src/test/java/com/networknt/schema/OneOfValidatorTest.java index f83d7f9ae..151632d92 100644 --- a/src/test/java/com/networknt/schema/OneOfValidatorTest.java +++ b/src/test/java/com/networknt/schema/OneOfValidatorTest.java @@ -17,6 +17,7 @@ package com.networknt.schema; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.List; import java.util.Set; @@ -123,4 +124,19 @@ void oneOfZero() { assertEquals("$.test", assertions.get(3).getInstanceLocation().toString()); assertEquals("$.oneOf[2].additionalProperties.type", assertions.get(3).getEvaluationPath().toString()); } + + @Test + void invalidTypeShouldThrowJsonSchemaException() { + String schemaData = "{\r\n" + + " \"$defs\": {\r\n" + + " \"User\": true\r\n" + + " },\r\n" + + " \"oneOf\": {\r\n" + + " \"$ref\": \"#/defs/User\"\r\n" + + " }\r\n" + + "}"; + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012); + JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData)); + assertEquals("type", ex.getValidationMessage().getMessageKey()); + } }