Skip to content

Commit 48cba95

Browse files
authored
fixes networknt#509 NPE with oneOf and custom URI Fetcher or Factory (networknt#515)
1 parent 1fbd73f commit 48cba95

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/main/java/com/networknt/schema/utils/JsonNodeUtil.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ public static boolean equalsToSchemaType(JsonNode node, JsonType schemaType, Jso
8484
}
8585

8686
ValidatorState state = (ValidatorState) CollectorContext.getInstance().get(ValidatorState.VALIDATOR_STATE_KEY);
87-
if(JsonType.NULL.equals(nodeType) ){
88-
if(state.isComplexValidator() && JsonNodeUtil.isNodeNullable(parentSchema.getParentSchema().getSchemaNode(), config) || JsonNodeUtil.isNodeNullable(parentSchema.getSchemaNode()) ){
89-
return true;
87+
if(JsonType.NULL.equals(nodeType)) {
88+
if(state.isComplexValidator() && parentSchema != null) {
89+
if( parentSchema.getParentSchema() != null && JsonNodeUtil.isNodeNullable(parentSchema.getParentSchema().getSchemaNode(), config) || JsonNodeUtil.isNodeNullable(parentSchema.getSchemaNode()) ) {
90+
return true;
91+
}
9092
}
9193
}
9294

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.networknt.schema;
2+
import com.fasterxml.jackson.databind.JsonNode;
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.networknt.schema.JsonSchema;
5+
import com.networknt.schema.JsonSchemaFactory;
6+
import com.networknt.schema.SpecVersion;
7+
import com.networknt.schema.ValidationMessage;
8+
import com.networknt.schema.uri.URIFactory;
9+
import com.networknt.schema.uri.URIFetcher;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.io.ByteArrayInputStream;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.net.URI;
16+
import java.nio.charset.StandardCharsets;
17+
import java.util.Set;
18+
19+
import static org.hamcrest.CoreMatchers.is;
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
22+
public class CustomUriTest {
23+
@Test
24+
public void customUri() throws Exception {
25+
/* Given */
26+
final JsonSchemaFactory factory = buildJsonSchemaFactory();
27+
final JsonSchema schema = factory.getSchema(
28+
"{\"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\"type\": \"object\",\"additionalProperties\": false,\"properties\": {\"customAnyOf\": {\"anyOf\": [{\"type\": \"null\"},{\"$ref\": \"custom:date\"}]},\"customOneOf\": {\"oneOf\": [{\"type\": \"null\"},{\"$ref\": \"custom:date\"}]}}}");
29+
final ObjectMapper mapper = new ObjectMapper();
30+
final JsonNode value = mapper.readTree("{\"customAnyOf\": null,\"customOneOf\": null}");
31+
32+
/* When */
33+
final Set<ValidationMessage> errors = schema.validate(value);
34+
35+
/* Then */
36+
assertThat(errors.isEmpty(), is(true));
37+
}
38+
39+
private JsonSchemaFactory buildJsonSchemaFactory() {
40+
return JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909))
41+
.uriFetcher(new CustomUriFetcher(), "custom").uriFactory(new CustomUriFactory(), "custom").build();
42+
}
43+
44+
private static class CustomUriFetcher implements URIFetcher {
45+
private static final String SCHEMA = "{\"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\"$id\":\"custom:date\",\"type\":\"string\",\"format\":\"date\"}";
46+
47+
@Override
48+
public InputStream fetch(final URI uri) throws IOException {
49+
return new ByteArrayInputStream(SCHEMA.getBytes(StandardCharsets.UTF_8));
50+
}
51+
}
52+
53+
private static class CustomUriFactory implements URIFactory {
54+
@Override
55+
public URI create(final String uri) {
56+
return URI.create(uri);
57+
}
58+
59+
@Override
60+
public URI create(final URI baseURI, final String segment) {
61+
return baseURI.resolve(segment);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)