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

Fixing #164 #179

Merged
merged 5 commits into from
May 18, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ static Map<String, Object> extend(Map<String, Object> additional, Map<String, Ob
return additional;
}
Map<String, Object> rawObj = new HashMap<>();
original.keySet().stream().forEach(name -> rawObj.put(name, original.get(name)));
additional.keySet().stream().forEach(name -> rawObj.put(name, additional.get(name)));
original.forEach(rawObj::put);
additional.forEach(rawObj::put);
return rawObj;
}

Expand Down
31 changes: 28 additions & 3 deletions core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,46 @@ public static Schema load(final JSONObject schemaJson, final SchemaClient httpCl
* {@code null}.
*/
public SchemaLoader(SchemaLoaderBuilder builder) {
Object effectiveRootSchemaJson = builder.rootSchemaJson == null
? builder.schemaJson
: builder.rootSchemaJson;
SpecificationVersion specVersion = extractSchemaKeywordValue(effectiveRootSchemaJson)
.map(SpecificationVersion::getByMetaSchemaUrl)
.orElse(builder.specVersion);
this.config = new LoaderConfig(builder.httpClient,
builder.formatValidators,
builder.specVersion,
specVersion,
builder.useDefaults,
builder.nullableSupport,
builder.regexpFactory);
this.ls = new LoadingState(config,
builder.pointerSchemas,
builder.rootSchemaJson == null ? builder.schemaJson : builder.rootSchemaJson,
effectiveRootSchemaJson,
builder.schemaJson,
builder.id,
builder.pointerToCurrentObj);
this.exclusiveLimitHandler = ExclusiveLimitHandler.ofSpecVersion(config.specVersion);
}

private static Optional<String> extractSchemaKeywordValue(Object effectiveRootSchemaJson) {

if (effectiveRootSchemaJson instanceof Map) {
Map<String, Object> schemaObj = (Map<String, Object>) effectiveRootSchemaJson;
Object schemaValue = schemaObj.get("$schema");
if (schemaValue != null) {
return Optional.of((String) schemaValue);
}
}
if (effectiveRootSchemaJson instanceof JsonObject) {
JsonObject schemaObj = (JsonObject) effectiveRootSchemaJson;
Object schemaValue = schemaObj.get("$schema");
if (schemaValue != null) {
return Optional.of((String) schemaValue);
}
}
return Optional.empty();
}

SchemaLoader(LoadingState ls) {
this.ls = ls;
this.config = ls.config;
Expand Down Expand Up @@ -474,7 +499,7 @@ Schema.Builder loadForType(JsonValue type) {
}

private boolean schemaHasAnyOf(Collection<String> propNames) {
return propNames.stream().filter(ls.schemaJson()::containsKey).findAny().isPresent();
return propNames.stream().anyMatch(ls.schemaJson()::containsKey);
}

Schema.Builder<?> loadChild(JsonValue childJson) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import static java.util.Collections.emptyMap;
import static org.everit.json.schema.TestSupport.asStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;

import org.everit.json.schema.NumberSchema;
import org.everit.json.schema.ReferenceSchema;
import org.everit.json.schema.ResourceLoader;
import org.everit.json.schema.Schema;
Expand All @@ -18,11 +20,9 @@

public class ReferenceLookupTest {

private static Map<String, Object> rootSchemaJson;
private static final Map<String, Object> rootSchemaJson = ResourceLoader.DEFAULT.readObj("ref-lookup-tests.json").toMap();

{
rootSchemaJson = ResourceLoader.DEFAULT.readObj("ref-lookup-tests.json").toMap();
}
private static final String v4Subschema = ResourceLoader.DEFAULT.readObj("v4-referred-subschema.json").toString();

private SchemaClient httpClient;

Expand Down Expand Up @@ -77,4 +77,11 @@ public void withParentScope() {
assertEquals("ok", actual.getDescription());
}

@Test
public void schemaVersionChange() {
when(httpClient.get("http://localhost/child-ref")).thenReturn(asStream(v4Subschema));
NumberSchema actual = (NumberSchema) performLookup("#/properties/definitionInRemote");
assertTrue(actual.isExclusiveMinimum());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"child": {
"$ref": "child-ref"
}
},
"definitionInRemote": {
"$ref": "http://localhost/child-ref#/definitions/SubSchema"
}
},
"definitions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-04/schema",
"definitions": {
"SubSchema": {
"minimum": 2,
"exclusiveMinimum": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-04/schema",
"definitions": {
"parameter": {
"mininum": 2,
"exclusiveMinimum": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"parameter": {
"type": "array",
"items": {
"$ref": "http://localhost:1234/v4-schema.json#/definitions/parameter"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"parameter": [
{
"name": "test",
"in": "query",
"type" : "string"
}
]
}