Skip to content

Commit fbf11c3

Browse files
authored
Check type validator is of correct class (#1003)
1 parent 61323ba commit fbf11c3

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/main/java/com/networknt/schema/JsonSchema.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,9 @@ private List<JsonValidator> read(JsonNode schemaNode) {
474474
} else if ("required".equals(pname)) {
475475
this.requiredValidator = validator;
476476
} else if ("type".equals(pname)) {
477-
this.typeValidator = (TypeValidator) validator;
477+
if (validator instanceof TypeValidator) {
478+
this.typeValidator = (TypeValidator) validator;
479+
}
478480
}
479481
}
480482

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.networknt.schema;
18+
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import com.fasterxml.jackson.core.JsonProcessingException;
24+
import com.fasterxml.jackson.databind.JsonMappingException;
25+
import com.fasterxml.jackson.databind.JsonNode;
26+
import com.networknt.schema.SpecVersion.VersionFlag;
27+
import com.networknt.schema.serialization.JsonMapperFactory;
28+
29+
public class Issue994Test {
30+
@Test
31+
void test() throws JsonMappingException, JsonProcessingException {
32+
String schemaData = "{\r\n"
33+
+ " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\r\n"
34+
+ " \"type\": \"object\",\r\n"
35+
+ " \"properties\": {\r\n"
36+
+ " \"textValue\": {\r\n"
37+
+ " \"type\": [\r\n"
38+
+ " \"string\",\r\n"
39+
+ " \"null\"\r\n"
40+
+ " ],\r\n"
41+
+ " \"isMandatory\": true\r\n"
42+
+ " }\r\n"
43+
+ " }\r\n"
44+
+ "}";
45+
JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012()).vocabularies(vocabularies -> {
46+
vocabularies.remove(Vocabulary.V202012_VALIDATION.getIri());
47+
}).build();
48+
JsonNode schemaNode = JsonMapperFactory.getInstance().readTree(schemaData);
49+
JsonSchema schema = JsonSchemaFactory
50+
.getInstance(VersionFlag.V202012, builder -> builder.metaSchema(metaSchema)).getSchema(schemaNode);
51+
String inputData = "{\r\n"
52+
+ " \"textValue\": \"hello\"\r\n"
53+
+ "}";
54+
assertTrue(schema.validate(inputData, InputFormat.JSON, OutputFormat.BOOLEAN));
55+
}
56+
}

0 commit comments

Comments
 (0)