Skip to content

Commit e32a541

Browse files
committed
fixes #273 normalize the meta schema uri
1 parent 88a75df commit e32a541

File tree

5 files changed

+70
-31
lines changed

5 files changed

+70
-31
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static PatternFormat pattern(String name, String regex) {
6060
}
6161

6262
private static class V4 {
63-
private static String URI = "http://json-schema.org/draft-04/schema#";
63+
private static String URI = "https://json-schema.org/draft-04/schema";
6464
private static final String ID = "id";
6565

6666
public static final List<Format> BUILTIN_FORMATS = new ArrayList<Format>(JsonMetaSchema.COMMON_BUILTIN_FORMATS);
@@ -89,7 +89,7 @@ public static JsonMetaSchema getInstance() {
8989
}
9090

9191
private static class V6 {
92-
private static String URI = "http://json-schema.org/draft-06/schema#";
92+
private static String URI = "https://json-schema.org/draft-06/schema";
9393
// Draft 6 uses "$id"
9494
private static final String ID = "$id";
9595

@@ -119,7 +119,7 @@ public static JsonMetaSchema getInstance() {
119119
}
120120

121121
private static class V7 {
122-
private static String URI = "http://json-schema.org/draft-07/schema#";
122+
private static String URI = "https://json-schema.org/draft-07/schema";
123123
private static final String ID = "$id";
124124

125125
public static final List<Format> BUILTIN_FORMATS = new ArrayList<Format>(JsonMetaSchema.COMMON_BUILTIN_FORMATS);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
import java.io.IOException;
2727
import java.io.InputStream;
28+
import java.net.MalformedURLException;
2829
import java.net.URI;
30+
import java.net.URISyntaxException;
31+
import java.net.URL;
2932
import java.util.Collection;
3033
import java.util.HashMap;
3134
import java.util.Map;
@@ -275,7 +278,7 @@ protected ValidationContext createValidationContext(final JsonNode schemaNode) {
275278

276279
private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
277280
final JsonNode uriNode = schemaNode.get("$schema");
278-
final String uri = uriNode == null || uriNode.isNull() ? defaultMetaSchemaURI : uriNode.textValue();
281+
final String uri = uriNode == null || uriNode.isNull() ? defaultMetaSchemaURI : normalizeMetaSchemaUri(uriNode.textValue());
279282
final JsonMetaSchema jsonMetaSchema = jsonMetaSchemas.get(uri);
280283
if (jsonMetaSchema == null) {
281284
throw new JsonSchemaException("Unknown Metaschema: " + uri);
@@ -396,4 +399,14 @@ private boolean idMatchesSourceUri(final JsonMetaSchema metaSchema, final JsonNo
396399
}
397400
return result;
398401
}
402+
403+
static protected String normalizeMetaSchemaUri(String u) {
404+
try {
405+
URI uri = new URI(u);
406+
URI newUri = new URI("https", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null, null);
407+
return newUri.toString();
408+
} catch (URISyntaxException e) {
409+
throw new JsonSchemaException("Wrong MetaSchema URI: " + u);
410+
}
411+
}
399412
}

src/test/java/com/networknt/schema/CollectorContextTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public String getErrorMessageDescription() {
150150

151151
private void setupSchema() throws Exception {
152152
final JsonMetaSchema metaSchema = getJsonMetaSchema(
153-
"https://github.com/networknt/json-schema-validator/tests/schemas/example01#");
153+
"https://github.com/networknt/json-schema-validator/tests/schemas/example01");
154154
final JsonSchemaFactory schemaFactory = JsonSchemaFactory
155155
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(metaSchema)
156156
.build();
@@ -160,7 +160,7 @@ private void setupSchema() throws Exception {
160160

161161
private String getSchemaString() {
162162
return "{"
163-
+ "\"$schema\": \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\","
163+
+ "\"$schema\": \"https://github.com/networknt/json-schema-validator/tests/schemas/example01\","
164164
+ "\"title\" : \"Sample test schema\",\n"
165165
+ "\"description\" : \"Sample schema definition\","
166166
+ "\"type\" : \"object\","
@@ -186,7 +186,7 @@ private String getSchemaString() {
186186

187187
private String getSchemaStringMultipleProperties() {
188188
return "{"
189-
+ "\"$schema\": \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\","
189+
+ "\"$schema\": \"https://github.com/networknt/json-schema-validator/tests/schemas/example01\","
190190
+ "\"title\" : \"Sample test schema\","
191191
+ "\"description\" : \"Sample schema definition\","
192192
+ "\"type\" : \"object\","

src/test/java/com/networknt/schema/CustomMetaSchemaTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ private List<String> readStringList(JsonNode node) {
107107
public void customMetaSchemaWithIgnoredKeyword() throws JsonProcessingException, IOException {
108108
ObjectMapper objectMapper = new ObjectMapper();
109109
final JsonMetaSchema metaSchema = JsonMetaSchema
110-
.builder("https://github.com/networknt/json-schema-validator/tests/schemas/example01#", JsonMetaSchema.getV4())
110+
.builder("https://github.com/networknt/json-schema-validator/tests/schemas/example01", JsonMetaSchema.getV4())
111111
// Generated UI uses enumNames to render Labels for enum values
112112
.addKeyword(new EnumNamesKeyword())
113113
.build();
114114

115115
final JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).addMetaSchema(metaSchema).build();
116116
final JsonSchema schema = validatorFactory.getSchema("{\n" +
117117
" \"$schema\":\n" +
118-
" \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\",\n" +
118+
" \"https://github.com/networknt/json-schema-validator/tests/schemas/example01\",\n" +
119119
" \"enum\": [\"foo\", \"bar\"],\n" +
120120
" \"enumNames\": [\"Foo !\", \"Bar !\"]\n" +
121121
"}");

src/test/java/com/networknt/schema/UnknownMetaSchemaTest.java

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,71 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.JsonNode;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import org.junit.Assert;
67
import org.junit.Test;
78

89
import java.util.Set;
910

1011
public class UnknownMetaSchemaTest {
1112

12-
private String schema =
13-
"{\n" +
14-
" \"$schema\": \"https://json-schema.org/draft-07/schema\",\n" +
15-
" \"title\": \"thingModel\",\n" +
16-
" \"description\": \"description of thing\",\n" +
17-
" \"type\": \"object\",\n" +
18-
" \"properties\": {\n" +
19-
" \"data\": {\n" +
20-
" \"type\": \"integer\"\n" +
21-
" }\n" +
22-
" },\n" +
23-
" \"required\": [\n" +
24-
" \"data\"\n" +
25-
" ]\n" +
26-
"}";
27-
28-
private String json =
29-
"{\n" +
30-
" \"data\": 1\n" +
31-
"}";
13+
private String schema1 = "{\"$schema\":\"http://json-schema.org/draft-07/schema\",\"title\":\"thingModel\",\"description\":\"description of thing\",\"type\":\"object\",\"properties\":{\"data\":{\"type\":\"integer\"},\"required\":[\"data\"]}}";
14+
private String schema2 = "{\"$schema\":\"https://json-schema.org/draft-07/schema\",\"title\":\"thingModel\",\"description\":\"description of thing\",\"type\":\"object\",\"properties\":{\"data\":{\"type\":\"integer\"},\"required\":[\"data\"]}}";
15+
private String schema3 = "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"title\":\"thingModel\",\"description\":\"description of thing\",\"type\":\"object\",\"properties\":{\"data\":{\"type\":\"integer\"},\"required\":[\"data\"]}}";
3216

17+
private String json = "{\"data\":1}";
3318

3419
@Test
35-
public void test() throws JsonProcessingException {
20+
public void testSchema1() throws JsonProcessingException {
3621
ObjectMapper mapper = new ObjectMapper();
3722
JsonNode jsonNode = mapper.readTree(this.json);
3823

3924
JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build();
40-
JsonSchema jsonSchema = factory.getSchema(schema);
25+
JsonSchema jsonSchema = factory.getSchema(schema1);
4126

4227
Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
4328
for(ValidationMessage error:errors) {
4429
System.out.println(error.getMessage());
4530
}
4631
}
32+
33+
@Test
34+
public void testSchema2() throws JsonProcessingException {
35+
ObjectMapper mapper = new ObjectMapper();
36+
JsonNode jsonNode = mapper.readTree(this.json);
37+
38+
JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build();
39+
JsonSchema jsonSchema = factory.getSchema(schema2);
40+
41+
Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
42+
for(ValidationMessage error:errors) {
43+
System.out.println(error.getMessage());
44+
}
45+
}
46+
@Test
47+
public void testSchema3() throws JsonProcessingException {
48+
ObjectMapper mapper = new ObjectMapper();
49+
JsonNode jsonNode = mapper.readTree(this.json);
50+
51+
JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build();
52+
JsonSchema jsonSchema = factory.getSchema(schema3);
53+
54+
Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
55+
for(ValidationMessage error:errors) {
56+
System.out.println(error.getMessage());
57+
}
58+
}
59+
60+
@Test
61+
public void testNormalize() throws JsonSchemaException {
62+
String uri01 = "http://json-schema.org/draft-07/schema";
63+
String uri02 = "http://json-schema.org/draft-07/schema#";
64+
String uri03 = "http://json-schema.org/draft-07/schema?key=value";
65+
String uri04 = "http://json-schema.org/draft-07/schema?key=value&key2=value2";
66+
String expected = "https://json-schema.org/draft-07/schema";
67+
Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri01));
68+
Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri02));
69+
Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri03));
70+
Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri04));
71+
72+
}
4773
}

0 commit comments

Comments
 (0)