Skip to content

Commit 524dc47

Browse files
author
Francesco Zanutto
committed
#258 fixed cyclic dependencies
1 parent e1ce67d commit 524dc47

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
@@ -28,6 +28,8 @@
2828
import java.util.Collection;
2929
import java.util.HashMap;
3030
import java.util.Map;
31+
import java.util.concurrent.ConcurrentHashMap;
32+
import java.util.concurrent.ConcurrentMap;
3133

3234
public class JsonSchemaFactory {
3335
private static final Logger logger = LoggerFactory
@@ -138,6 +140,7 @@ public JsonSchemaFactory build() {
138140
private final URISchemeFetcher uriFetcher;
139141
private final Map<String, JsonMetaSchema> jsonMetaSchemas;
140142
private final Map<String, String> uriMap;
143+
private final ConcurrentMap<URI, JsonSchema> uriSchemaCache = new ConcurrentHashMap<URI, JsonSchema>();
141144

142145
private JsonSchemaFactory(
143146
final ObjectMapper mapper,
@@ -248,7 +251,8 @@ public static Builder builder(final JsonSchemaFactory blueprint) {
248251
protected JsonSchema newJsonSchema(final URI schemaUri, final JsonNode schemaNode, final SchemaValidatorsConfig config) {
249252
final ValidationContext validationContext = createValidationContext(schemaNode);
250253
validationContext.setConfig(config);
251-
JsonSchema jsonSchema = new JsonSchema(validationContext, schemaUri, schemaNode);
254+
JsonSchema jsonSchema = new JsonSchema(validationContext, schemaUri, schemaNode)
255+
.initialize();
252256
return jsonSchema;
253257
}
254258

@@ -315,16 +319,27 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co
315319
logger.error("Failed to create URI.", e);
316320
throw new JsonSchemaException(e);
317321
}
322+
323+
if (uriSchemaCache.containsKey(mappedUri))
324+
return uriSchemaCache.get(mappedUri);
325+
318326
try {
319327
inputStream = this.uriFetcher.fetch(mappedUri);
320328
final JsonNode schemaNode = mapper.readTree(inputStream);
321329
final JsonMetaSchema jsonMetaSchema = findMetaSchemaForSchema(schemaNode);
322330

331+
JsonSchema jsonSchema;
323332
if (idMatchesSourceUri(jsonMetaSchema, schemaNode, schemaUri)) {
324-
return new JsonSchema(new ValidationContext(this.uriFactory, jsonMetaSchema, this, config), mappedUri, schemaNode, true /*retrieved via id, resolving will not change anything*/);
333+
jsonSchema = new JsonSchema(new ValidationContext(this.uriFactory, jsonMetaSchema, this, config), mappedUri, schemaNode, true /*retrieved via id, resolving will not change anything*/);
334+
} else {
335+
final ValidationContext validationContext = createValidationContext(schemaNode);
336+
validationContext.setConfig(config);
337+
jsonSchema = new JsonSchema(validationContext, mappedUri, schemaNode);
325338
}
339+
uriSchemaCache.put(mappedUri, jsonSchema);
340+
jsonSchema.initialize();
326341

327-
return newJsonSchema(mappedUri, schemaNode, config);
342+
return jsonSchema;
328343
} finally {
329344
if (inputStream != null) {
330345
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)