Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for StackOverFlowErrors occurring in case of circular $ref validators #417

Merged
merged 1 commit into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/com/networknt/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class JsonSchema extends BaseJsonValidator {
private final String idKeyword;
private final ValidationContext validationContext;
private WalkListenerRunner keywordWalkListenerRunner;
private boolean validatorsLoaded = false;

/**
* This is the current uri of this schema. This uri could refer to the uri of this schema's file
Expand Down Expand Up @@ -423,8 +424,11 @@ public Map<String, JsonValidator> getValidators() {
* instances of the validators.</p>
*/
public void initializeValidators() {
for (final JsonValidator validator : getValidators().values()) {
validator.preloadJsonSchema();
if (!validatorsLoaded) {
validatorsLoaded = true;
for (final JsonValidator validator : getValidators().values()) {
validator.preloadJsonSchema();
}
}
}
}
9 changes: 9 additions & 0 deletions src/test/java/com/networknt/schema/Issue406Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

public class Issue406Test {
protected static final String INVALID_$REF_SCHEMA = "{\"$ref\":\"urn:unresolved\"}";
protected static final String CIRCULAR_$REF_SCHEMA = "{\"$ref\":\"#/nestedSchema\","
+ "\"nestedSchema\":{\"$ref\":\"#/nestedSchema\"}}";

@Test
public void testPreloadingNotHappening() {
Expand All @@ -31,4 +33,11 @@ public void run() {
}
});
}

@Test
public void testPreloadingHappeningForCircularDependency() {
final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
final JsonSchema schema = factory.getSchema(CIRCULAR_$REF_SCHEMA);
schema.initializeValidators();
}
}