Skip to content

Commit c41df27

Browse files
authored
Introduce forceHttps flag in JsonSchemaFactory.Builder (#400)
* Provide a reproduce case for gh-314 * Introduce `forceHttps` flag in `JsonSchemaFactory.Builder`
1 parent d907f55 commit c41df27

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static class Builder {
4747
private URNFactory urnFactory;
4848
private final Map<String, JsonMetaSchema> jsonMetaSchemas = new HashMap<String, JsonMetaSchema>();
4949
private final Map<String, String> uriMap = new HashMap<String, String>();
50+
private boolean forceHttps = true;
5051

5152

5253
public Builder() {
@@ -139,7 +140,10 @@ public Builder addUrnFactory(URNFactory urnFactory) {
139140
return this;
140141
}
141142

142-
143+
public Builder forceHttps(boolean forceHttps) {
144+
this.forceHttps = forceHttps;
145+
return this;
146+
}
143147

144148
public JsonSchemaFactory build() {
145149
// create builtin keywords with (custom) formats.
@@ -150,7 +154,8 @@ public JsonSchemaFactory build() {
150154
new URISchemeFetcher(uriFetcherMap),
151155
urnFactory,
152156
jsonMetaSchemas,
153-
uriMap
157+
uriMap,
158+
forceHttps
154159
);
155160
}
156161
}
@@ -163,6 +168,7 @@ public JsonSchemaFactory build() {
163168
private final Map<String, JsonMetaSchema> jsonMetaSchemas;
164169
private final Map<String, String> uriMap;
165170
private final ConcurrentMap<URI, JsonSchema> uriSchemaCache = new ConcurrentHashMap<URI, JsonSchema>();
171+
private final boolean forceHttps;
166172

167173

168174
private JsonSchemaFactory(
@@ -172,7 +178,8 @@ private JsonSchemaFactory(
172178
final URISchemeFetcher uriFetcher,
173179
final URNFactory urnFactory,
174180
final Map<String, JsonMetaSchema> jsonMetaSchemas,
175-
final Map<String, String> uriMap) {
181+
final Map<String, String> uriMap,
182+
final boolean forceHttps) {
176183
if (mapper == null) {
177184
throw new IllegalArgumentException("ObjectMapper must not be null");
178185
} else if (defaultMetaSchemaURI == null || defaultMetaSchemaURI.trim().isEmpty()) {
@@ -195,6 +202,7 @@ private JsonSchemaFactory(
195202
this.urnFactory = urnFactory;
196203
this.jsonMetaSchemas = jsonMetaSchemas;
197204
this.uriMap = uriMap;
205+
this.forceHttps = forceHttps;
198206
}
199207

200208
/**
@@ -273,7 +281,7 @@ protected ValidationContext createValidationContext(final JsonNode schemaNode) {
273281

274282
private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
275283
final JsonNode uriNode = schemaNode.get("$schema");
276-
final String uri = uriNode == null || uriNode.isNull() ? defaultMetaSchemaURI : normalizeMetaSchemaUri(uriNode.textValue());
284+
final String uri = uriNode == null || uriNode.isNull() ? defaultMetaSchemaURI : normalizeMetaSchemaUri(uriNode.textValue(), forceHttps);
277285
final JsonMetaSchema jsonMetaSchema = jsonMetaSchemas.get(uri);
278286
if (jsonMetaSchema == null) {
279287
throw new JsonSchemaException("Unknown MetaSchema: " + uri);
@@ -395,10 +403,11 @@ private boolean idMatchesSourceUri(final JsonMetaSchema metaSchema, final JsonNo
395403
return result;
396404
}
397405

398-
static protected String normalizeMetaSchemaUri(String u) {
406+
static protected String normalizeMetaSchemaUri(String u, boolean forceHttps) {
399407
try {
400408
URI uri = new URI(u);
401-
URI newUri = new URI("https", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null, null);
409+
String scheme = forceHttps ? "https" : uri.getScheme();
410+
URI newUri = new URI(scheme, uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null, null);
402411
return newUri.toString();
403412
} catch (URISyntaxException e) {
404413
throw new JsonSchemaException("Wrong MetaSchema URI: " + u);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static SpecVersion.VersionFlag detect(JsonNode jsonNode) {
3939
if (!jsonNode.has(SCHEMA_TAG))
4040
throw new JsonSchemaException("Schema tag not present");
4141

42-
String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(jsonNode.get(SCHEMA_TAG).asText());
42+
String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(jsonNode.get(SCHEMA_TAG).asText(), true);
4343
if (schemaUri.equals(JsonMetaSchema.getV4().getUri()))
4444
return SpecVersion.VersionFlag.V4;
4545
else if (schemaUri.equals(JsonMetaSchema.getV6().getUri()))
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.networknt.schema;
2+
3+
import java.io.InputStream;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
public class Issue314Test {
8+
private static final JsonSchemaFactory FACTORY =
9+
JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7))
10+
.addMetaSchema(
11+
JsonMetaSchema.builder(
12+
"http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0",
13+
JsonMetaSchema.getV7())
14+
.build())
15+
.forceHttps(false)
16+
.build();
17+
18+
@Test
19+
public void testNormalizeHttpOnly() {
20+
String schemaPath = "/schema/issue314-v7.json";
21+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
22+
JsonSchema schema = FACTORY.getSchema(schemaInputStream);
23+
24+
Assert.assertNotNull(schema);
25+
}
26+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public void testNormalize() throws JsonSchemaException {
6464
String uri03 = "http://json-schema.org/draft-07/schema?key=value";
6565
String uri04 = "http://json-schema.org/draft-07/schema?key=value&key2=value2";
6666
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));
67+
Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri01, true));
68+
Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri02, true));
69+
Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri03, true));
70+
Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri04, true));
7171

7272
}
7373
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
3+
"type": "object"
4+
}

0 commit comments

Comments
 (0)