Skip to content

Commit 5bb9b44

Browse files
authored
Merge pull request #275 from francesc79/bug/cyclic-dep
#258 fixed cyclic dependencies
2 parents 2ef94a1 + f0a60ef commit 5bb9b44

19 files changed

+161
-22
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public AdditionalPropertiesValidator(String schemaPath, JsonNode schemaNode, Jso
4040
additionalPropertiesSchema = null;
4141
} else if (schemaNode.isObject()) {
4242
allowAdditionalProperties = true;
43-
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
43+
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema)
44+
.initialize();
4445
} else {
4546
allowAdditionalProperties = false;
4647
additionalPropertiesSchema = null;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
3131
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
3232
int size = schemaNode.size();
3333
for (int i = 0; i < size; i++) {
34-
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema));
34+
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema)
35+
.initialize());
3536
}
3637
}
3738

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
3131
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF, validationContext);
3232
int size = schemaNode.size();
3333
for (int i = 0; i < size; i++) {
34-
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema));
34+
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema)
35+
.initialize());
3536
}
3637
}
3738

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public class ContainsValidator extends BaseJsonValidator implements JsonValidato
3131
public ContainsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3232
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.CONTAINS, validationContext);
3333
if (schemaNode.isObject() || schemaNode.isBoolean()) {
34-
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
34+
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema)
35+
.initialize();
3536
}
3637

3738
parseErrorCode(getValidatorType().getErrorCodeKey());

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public DependenciesValidator(String schemaPath, JsonNode schemaNode, JsonSchema
4444
depsProps.add(pvalue.get(i).asText());
4545
}
4646
} else if (pvalue.isObject() || pvalue.isBoolean()) {
47-
schemaDeps.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUri(), pvalue, parentSchema));
47+
schemaDeps.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUri(), pvalue, parentSchema)
48+
.initialize());
4849
}
4950
}
5051

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ public IfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSche
3737
for (final String keyword : KEYWORDS) {
3838
final JsonNode node = schemaNode.get(keyword);
3939
if (keyword.equals("if")) {
40-
ifSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
40+
ifSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema)
41+
.initialize();
4142
} else if (keyword.equals("then") && node != null) {
42-
thenSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
43+
thenSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema)
44+
.initialize();
4345
} else if (keyword.equals("else") && node != null) {
44-
elseSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
46+
elseSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema)
47+
.initialize();
4548
}
4649
}
4750
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,22 @@ public class ItemsValidator extends BaseJsonValidator implements JsonValidator {
3434
public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3535
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ITEMS, validationContext);
3636
if (schemaNode.isObject() || schemaNode.isBoolean()) {
37-
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
37+
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema)
38+
.initialize();
3839
} else {
3940
tupleSchema = new ArrayList<JsonSchema>();
4041
for (JsonNode s : schemaNode) {
41-
tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), s, parentSchema));
42+
tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), s, parentSchema)
43+
.initialize());
4244
}
4345

4446
JsonNode addItemNode = getParentSchema().getSchemaNode().get(PROPERTY_ADDITIONAL_ITEMS);
4547
if (addItemNode != null) {
4648
if (addItemNode.isBoolean()) {
4749
additionalItems = addItemNode.asBoolean();
4850
} else if (addItemNode.isObject()) {
49-
additionalSchema = new JsonSchema(validationContext, parentSchema.getCurrentUri(), addItemNode);
51+
additionalSchema = new JsonSchema(validationContext, parentSchema.getCurrentUri(), addItemNode)
52+
.initialize();
5053
}
5154
}
5255
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public class JsonSchema extends BaseJsonValidator {
3434
private static final Pattern intPattern = Pattern.compile("^[0-9]+$");
35-
protected final Map<String, JsonValidator> validators;
35+
protected Map<String, JsonValidator> validators;
3636
private final String idKeyword;
3737
private final ValidationContext validationContext;
3838

@@ -70,7 +70,11 @@ private JsonSchema(ValidationContext validationContext, String schemaPath, URI c
7070
this.config = validationContext.getConfig();
7171
this.idKeyword = validationContext.getMetaSchema().getIdKeyword();
7272
this.currentUri = this.combineCurrentUriWithIds(currentUri, schemaNode);
73-
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
73+
}
74+
75+
JsonSchema initialize() {
76+
this.validators = Collections.unmodifiableMap(this.read(getSchemaNode()));
77+
return this;
7478
}
7579

7680
private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) {

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.Collection;
3030
import java.util.HashMap;
3131
import java.util.Map;
32+
import java.util.concurrent.ConcurrentHashMap;
33+
import java.util.concurrent.ConcurrentMap;
3234

3335
public class JsonSchemaFactory {
3436
private static final Logger logger = LoggerFactory
@@ -147,6 +149,7 @@ public JsonSchemaFactory build() {
147149
private final URNFactory urnFactory;
148150
private final Map<String, JsonMetaSchema> jsonMetaSchemas;
149151
private final Map<String, String> uriMap;
152+
private final ConcurrentMap<URI, JsonSchema> uriSchemaCache = new ConcurrentHashMap<URI, JsonSchema>();
150153

151154

152155
private JsonSchemaFactory(
@@ -260,7 +263,8 @@ public static Builder builder(final JsonSchemaFactory blueprint) {
260263
protected JsonSchema newJsonSchema(final URI schemaUri, final JsonNode schemaNode, final SchemaValidatorsConfig config) {
261264
final ValidationContext validationContext = createValidationContext(schemaNode);
262265
validationContext.setConfig(config);
263-
JsonSchema jsonSchema = new JsonSchema(validationContext, schemaUri, schemaNode);
266+
JsonSchema jsonSchema = new JsonSchema(validationContext, schemaUri, schemaNode)
267+
.initialize();
264268
return jsonSchema;
265269
}
266270

@@ -327,16 +331,27 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co
327331
logger.error("Failed to create URI.", e);
328332
throw new JsonSchemaException(e);
329333
}
334+
335+
if (uriSchemaCache.containsKey(mappedUri))
336+
return uriSchemaCache.get(mappedUri);
337+
330338
try {
331339
inputStream = this.uriFetcher.fetch(mappedUri);
332340
final JsonNode schemaNode = mapper.readTree(inputStream);
333341
final JsonMetaSchema jsonMetaSchema = findMetaSchemaForSchema(schemaNode);
334342

343+
JsonSchema jsonSchema;
335344
if (idMatchesSourceUri(jsonMetaSchema, schemaNode, schemaUri)) {
336-
return new JsonSchema(new ValidationContext(this.uriFactory, this.urnFactory, jsonMetaSchema, this, config), mappedUri, schemaNode, true /*retrieved via id, resolving will not change anything*/);
345+
jsonSchema = new JsonSchema(new ValidationContext(this.uriFactory, this.urnFactory, jsonMetaSchema, this, config), mappedUri, schemaNode, true /*retrieved via id, resolving will not change anything*/);
346+
} else {
347+
final ValidationContext validationContext = createValidationContext(schemaNode);
348+
validationContext.setConfig(config);
349+
jsonSchema = new JsonSchema(validationContext, mappedUri, schemaNode);
337350
}
351+
uriSchemaCache.put(mappedUri, jsonSchema);
352+
jsonSchema.initialize();
338353

339-
return newJsonSchema(mappedUri, schemaNode, config);
354+
return jsonSchema;
340355
} finally {
341356
if (inputStream != null) {
342357
inputStream.close();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public class NotValidator extends BaseJsonValidator implements JsonValidator {
3030

3131
public NotValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3232
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.NOT, validationContext);
33-
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
33+
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema)
34+
.initialize();
3435

3536
parseErrorCode(getValidatorType().getErrorCodeKey());
3637
}

0 commit comments

Comments
 (0)