Skip to content

Commit

Permalink
fixes #509 NPE with oneOf and custom URI Fetcher or Factory (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu authored Feb 25, 2022
1 parent 1fbd73f commit 48cba95
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/networknt/schema/utils/JsonNodeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ public static boolean equalsToSchemaType(JsonNode node, JsonType schemaType, Jso
}

ValidatorState state = (ValidatorState) CollectorContext.getInstance().get(ValidatorState.VALIDATOR_STATE_KEY);
if(JsonType.NULL.equals(nodeType) ){
if(state.isComplexValidator() && JsonNodeUtil.isNodeNullable(parentSchema.getParentSchema().getSchemaNode(), config) || JsonNodeUtil.isNodeNullable(parentSchema.getSchemaNode()) ){
return true;
if(JsonType.NULL.equals(nodeType)) {
if(state.isComplexValidator() && parentSchema != null) {
if( parentSchema.getParentSchema() != null && JsonNodeUtil.isNodeNullable(parentSchema.getParentSchema().getSchemaNode(), config) || JsonNodeUtil.isNodeNullable(parentSchema.getSchemaNode()) ) {
return true;
}
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/test/java/com/networknt/schema/CustomUriTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import com.networknt.schema.uri.URIFactory;
import com.networknt.schema.uri.URIFetcher;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class CustomUriTest {
@Test
public void customUri() throws Exception {
/* Given */
final JsonSchemaFactory factory = buildJsonSchemaFactory();
final JsonSchema schema = factory.getSchema(
"{\"$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\"}]}}}");
final ObjectMapper mapper = new ObjectMapper();
final JsonNode value = mapper.readTree("{\"customAnyOf\": null,\"customOneOf\": null}");

/* When */
final Set<ValidationMessage> errors = schema.validate(value);

/* Then */
assertThat(errors.isEmpty(), is(true));
}

private JsonSchemaFactory buildJsonSchemaFactory() {
return JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909))
.uriFetcher(new CustomUriFetcher(), "custom").uriFactory(new CustomUriFactory(), "custom").build();
}

private static class CustomUriFetcher implements URIFetcher {
private static final String SCHEMA = "{\"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\"$id\":\"custom:date\",\"type\":\"string\",\"format\":\"date\"}";

@Override
public InputStream fetch(final URI uri) throws IOException {
return new ByteArrayInputStream(SCHEMA.getBytes(StandardCharsets.UTF_8));
}
}

private static class CustomUriFactory implements URIFactory {
@Override
public URI create(final String uri) {
return URI.create(uri);
}

@Override
public URI create(final URI baseURI, final String segment) {
return baseURI.resolve(segment);
}
}
}

0 comments on commit 48cba95

Please sign in to comment.