Skip to content

Commit 954d68b

Browse files
authored
Merge pull request #137 from rhwood/url-map-performance
fix: avoid performance penalty of using URL in map or set
2 parents d615f95 + f2ebbec commit 954d68b

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.fasterxml.jackson.databind.JsonNode;
3030
import com.fasterxml.jackson.databind.ObjectMapper;
3131
import com.networknt.schema.url.StandardURLFetcher;
32+
import com.networknt.schema.url.URLFactory;
3233
import com.networknt.schema.url.URLFetcher;
3334

3435
public class JsonSchemaFactory {
@@ -41,7 +42,7 @@ public static class Builder {
4142
private URLFetcher urlFetcher;
4243
private String defaultMetaSchemaURI;
4344
private Map<String, JsonMetaSchema> jsonMetaSchemas = new HashMap<String, JsonMetaSchema>();
44-
private Map<URL, URL> urlMap = new HashMap<URL, URL>();
45+
private Map<String, String> urlMap = new HashMap<String, String>();
4546

4647
public Builder objectMapper(ObjectMapper objectMapper) {
4748
this.objectMapper = objectMapper;
@@ -70,7 +71,7 @@ public Builder addMetaSchemas(Collection<? extends JsonMetaSchema> jsonMetaSchem
7071
return this;
7172
}
7273

73-
public Builder addUrlMappings(Map<URL, URL> map) {
74+
public Builder addUrlMappings(Map<String, String> map) {
7475
this.urlMap.putAll(map);
7576
return this;
7677
}
@@ -91,9 +92,9 @@ public JsonSchemaFactory build() {
9192
private final URLFetcher urlFetcher;
9293
private final String defaultMetaSchemaURI;
9394
private final Map<String, JsonMetaSchema> jsonMetaSchemas;
94-
private final Map<URL, URL> urlMap;
95+
private final Map<String, String> urlMap;
9596

96-
private JsonSchemaFactory(ObjectMapper mapper, URLFetcher urlFetcher, String defaultMetaSchemaURI, Map<String, JsonMetaSchema> jsonMetaSchemas, Map<URL, URL> urlMap) {
97+
private JsonSchemaFactory(ObjectMapper mapper, URLFetcher urlFetcher, String defaultMetaSchemaURI, Map<String, JsonMetaSchema> jsonMetaSchemas, Map<String, String> urlMap) {
9798
if (mapper == null) {
9899
throw new IllegalArgumentException("ObjectMapper must not be null");
99100
}
@@ -204,9 +205,9 @@ public JsonSchema getSchema(InputStream schemaStream) {
204205
public JsonSchema getSchema(URL schemaURL, SchemaValidatorsConfig config) {
205206
try {
206207
InputStream inputStream = null;
207-
Map<URL, URL> map = (config != null) ? config.getUrlMappings() : new HashMap<URL, URL>(urlMap);
208+
Map<String, String> map = (config != null) ? config.getUrlMappings() : new HashMap<String, String>(urlMap);
208209
map.putAll(urlMap);
209-
URL mappedURL = map.getOrDefault(schemaURL, schemaURL);
210+
URL mappedURL = URLFactory.toURL(map.getOrDefault(schemaURL.toString(), schemaURL.toString()));
210211
try {
211212
inputStream = urlFetcher.fetch(mappedURL);
212213
JsonNode schemaNode = mapper.readTree(inputStream);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21-
import java.net.URL;
2221

2322
public class SchemaValidatorsConfig {
2423
/**
@@ -43,7 +42,7 @@ public class SchemaValidatorsConfig {
4342
* validation of schemas that refer to public URLs. This is merged with any mappings the {@link JsonSchemaFactory}
4443
* may have been built with.
4544
*/
46-
private Map<URL, URL> urlMappings = new HashMap<URL, URL>();
45+
private Map<String, String> urlMappings = new HashMap<String, String>();
4746

4847
public boolean isTypeLoose() {
4948
return typeLoose;
@@ -53,11 +52,12 @@ public void setTypeLoose(boolean typeLoose) {
5352
this.typeLoose = typeLoose;
5453
}
5554

56-
public Map<URL, URL> getUrlMappings() {
57-
return new HashMap<URL, URL>(urlMappings);
55+
public Map<String, String> getUrlMappings() {
56+
// return a copy of the mappings
57+
return new HashMap<String, String>(urlMappings);
5858
}
5959

60-
public void setUrlMappings(Map<URL, URL> urlMappings) {
60+
public void setUrlMappings(Map<String, String> urlMappings) {
6161
this.urlMappings = urlMappings;
6262
}
6363

@@ -83,6 +83,6 @@ public SchemaValidatorsConfig() {
8383

8484
private void loadDefaultConfig() {
8585
this.typeLoose = true;
86-
this.urlMappings = new HashMap<URL, URL>();
86+
this.urlMappings = new HashMap<String, String>();
8787
}
8888
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ public void testMappingsForRef() throws IOException {
141141
assertEquals(0, schema.validate(mapper.readTree("[]")).size());
142142
}
143143

144-
private Map<URL, URL> getUrlMappingsFromUrl(URL url) throws MalformedURLException, IOException {
145-
HashMap<URL, URL> map = new HashMap<URL, URL>();
144+
private Map<String, String> getUrlMappingsFromUrl(URL url) throws MalformedURLException, IOException {
145+
HashMap<String, String> map = new HashMap<String, String>();
146146
for (JsonNode mapping : mapper.readTree(url)) {
147-
map.put(URLFactory.toURL(mapping.get("publicURL").asText()),
148-
URLFactory.toURL(mapping.get("localURL").asText()));
147+
map.put(mapping.get("publicURL").asText(),
148+
mapping.get("localURL").asText());
149149
}
150150
return map;
151151
}

0 commit comments

Comments
 (0)