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 issue where loading the v6 schema overwrites all the custom validators #153

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -73,7 +74,7 @@ static URI extractChildId(URI parentScopeId, Object childJson, String idKeyword)
SchemaLoader.SchemaLoaderBuilder initChildLoader() {
SchemaLoader.SchemaLoaderBuilder rval = SchemaLoader.builder()
.httpClient(this.config.httpClient)
.formatValidators(this.config.formatValidators)
.formatValidators(new HashMap<>(this.config.formatValidators))
.resolutionScope(id)
.schemaJson(schemaJson)
.rootSchemaJson(rootSchemaJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ public SchemaLoaderBuilder addFormatValidator(String formatName,

public SchemaLoaderBuilder draftV6Support() {
this.specVersion = DRAFT_6;
this.formatValidators = new HashMap<>(DRAFT_6.defaultFormatValidators());
DRAFT_6.defaultFormatValidators().forEach((formatName, validator) -> {
addFormatValidator(formatName, validator);
});
return this;
}

Expand Down
34 changes: 29 additions & 5 deletions tests/src/test/java/org/everit/json/schema/IssueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,33 @@ private static JSONObject fileAsJson(File file) {
}
}

static class EvenCharNumValidator implements FormatValidator {
@Override
public Optional<String> validate(final String subject) {
if (subject.length() % 2 == 0) {
return Optional.empty();
} else {
return Optional.of(String.format("the length of srtring [%s] is odd", subject));
}
}

@Override
public String formatName() {
return "evenlength";
}
}

private Schema loadSchema() {
Optional<File> schemaFile = fileByName("schema.json");
try {
if (schemaFile.isPresent()) {
JSONObject schemaObj = fileAsJson(schemaFile.get());
return SchemaLoader.load(schemaObj);
return SchemaLoader.builder()
.addFormatValidator(new IssueTest.EvenCharNumValidator())
.schemaJson(schemaObj)
.build()
.load()
.build();
}
throw new RuntimeException(issueDir.getCanonicalPath() + "/schema.json is not found");
} catch (IOException e) {
Expand All @@ -100,10 +121,13 @@ private void stopJetty() {
public void test() {
Assume.assumeFalse("issue dir starts with 'x' - ignoring", issueDir.getName().startsWith("x"));
fileByName("remotes").ifPresent(this::initJetty);
Schema schema = loadSchema();
fileByName("subject-valid.json").ifPresent(file -> validate(file, schema, true));
fileByName("subject-invalid.json").ifPresent(file -> validate(file, schema, false));
stopJetty();
try {
Schema schema = loadSchema();
fileByName("subject-valid.json").ifPresent(file -> validate(file, schema, true));
fileByName("subject-invalid.json").ifPresent(file -> validate(file, schema, false));
} finally {
stopJetty();
}
}

private Validator createValidator() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 10,
"minLength": 3,
"format": "evenlength"
}
},
"required": [
"id"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "http://localhost:1234/customformat-schema.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "everitorg"
}