Skip to content

Commit 4fb2eba

Browse files
fixes #532 Invalid (non-string) $schema produces NullPointerException (#533)
1 parent 1b43f0d commit 4fb2eba

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
### Changed
1212

13+
- fixes #532 Invalid (non-string) $schema produces NullPointerException
14+
1315
## 1.0.67 - 2022-03-05
1416

1517
### Changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public Builder addUrnFactory(URNFactory urnFactory) {
139139
this.urnFactory = urnFactory;
140140
return this;
141141
}
142-
142+
143143
public Builder forceHttps(boolean forceHttps) {
144144
this.forceHttps = forceHttps;
145145
return this;
@@ -290,6 +290,9 @@ protected ValidationContext createValidationContext(final JsonNode schemaNode) {
290290

291291
private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
292292
final JsonNode uriNode = schemaNode.get("$schema");
293+
if (uriNode != null && !uriNode.isNull() && !uriNode.isTextual()) {
294+
throw new JsonSchemaException("Unknown MetaSchema: " + uriNode.toString());
295+
}
293296
final String uri = uriNode == null || uriNode.isNull() ? defaultMetaSchemaURI : normalizeMetaSchemaUri(uriNode.textValue(), forceHttps, removeEmptyFragmentSuffix);
294297
final JsonMetaSchema jsonMetaSchema = jsonMetaSchemas.get(uri);
295298
if (jsonMetaSchema == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.networknt.schema;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
public class Issue532Test {
8+
@Test
9+
public void failure() {
10+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
11+
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> {
12+
factory.getSchema("{ \"$schema\": true }");
13+
});
14+
assertEquals("Unknown MetaSchema: true", ex.getMessage());
15+
}
16+
}

0 commit comments

Comments
 (0)