Skip to content

Commit c72cf3c

Browse files
committed
initial working fix for #233
* wiring SchemaLocation into SchemaBuilder * adding `JSONPointer#getRefTokens()` * tracking the location in both `Schema#location` and `Schema#schemaLocation` (unfortunately the latter is protected so needs to be maintained) * adding `SchemaLocation#parseURI()` and using it in `ReferenceLookup`
1 parent 2457c61 commit c72cf3c

18 files changed

+144
-44
lines changed

core/src/main/java/org/everit/json/schema/JSONPointer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ private Object readByIndexToken(Object current, String indexToken) throws JSONPo
285285
}
286286
}
287287

288+
List<String> getRefTokens() {
289+
return refTokens;
290+
}
291+
288292
/**
289293
* Returns a string representing the JSONPointer path value using string
290294
* representation

core/src/main/java/org/everit/json/schema/Schema.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ public Builder<S> id(String id) {
6363
*/
6464
@Deprecated
6565
public Builder<S> schemaLocation(String schemaLocation) {
66-
// this.schemaLocation = schemaLocation;
67-
// return this;
68-
throw new UnsupportedOperationException();
66+
return schemaLocation(SchemaLocation.parseURI(schemaLocation));
6967
}
7068

7169
public Builder<S> schemaLocation(SchemaLocation location) {
72-
throw new UnsupportedOperationException();
70+
this.schemaLocation = location;
71+
return this;
7372
}
7473

7574
public Builder<S> defaultValue(Object defaultValue) {
@@ -107,7 +106,10 @@ public Builder<S> unprocessedProperties(Map<String, Object> unprocessedPropertie
107106

108107
private final String id;
109108

110-
protected final SchemaLocation schemaLocation;
109+
@Deprecated
110+
protected final String schemaLocation;
111+
112+
private final SchemaLocation location;
111113

112114
private final Object defaultValue;
113115

@@ -129,7 +131,8 @@ protected Schema(Builder<?> builder) {
129131
this.title = builder.title;
130132
this.description = builder.description;
131133
this.id = builder.id;
132-
this.schemaLocation = builder.schemaLocation;
134+
this.schemaLocation = builder.schemaLocation == null ? null : builder.schemaLocation.toString();
135+
this.location = builder.schemaLocation;
133136
this.defaultValue = builder.defaultValue;
134137
this.nullable = builder.nullable;
135138
this.readOnly = builder.readOnly;
@@ -234,14 +237,11 @@ public String getId() {
234237
}
235238

236239
public String getSchemaLocation() {
237-
if (schemaLocation == null) {
238-
return null;
239-
}
240-
return schemaLocation.toString();
240+
return schemaLocation;
241241
}
242242

243243
public SchemaLocation getLocation() {
244-
return schemaLocation;
244+
return location;
245245
}
246246

247247
public Object getDefaultValue() {

core/src/main/java/org/everit/json/schema/SchemaLocation.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,54 @@
11
package org.everit.json.schema;
22

33
import static java.util.Collections.emptyList;
4+
import static java.util.Objects.requireNonNull;
45

56
import java.net.URI;
7+
import java.net.URISyntaxException;
68
import java.util.ArrayList;
79
import java.util.List;
10+
import java.util.Objects;
811

912
public class SchemaLocation {
1013

1114
public static final SchemaLocation empty() {
1215
return new SchemaLocation(null, emptyList());
1316
}
1417

15-
private URI rootDocumentURI;
18+
public static final SchemaLocation parseURI(String uri) {
19+
try {
20+
int hashmarkIdx = uri.indexOf("#");
21+
if (hashmarkIdx > -1) {
22+
String rootDocumentURI;
23+
String rawPointer;
24+
if (hashmarkIdx == uri.length() - 1) {
25+
rootDocumentURI = uri;
26+
rawPointer = "";
27+
} else {
28+
rootDocumentURI = uri.substring(0, hashmarkIdx);
29+
rawPointer = uri.substring(hashmarkIdx + 1);
30+
}
31+
URI documentURI = "".equals(rootDocumentURI) ? null : new URI(rootDocumentURI);
32+
return new SchemaLocation(documentURI, new JSONPointer(rawPointer).getRefTokens());
33+
} else {
34+
return new SchemaLocation(new URI(uri), emptyList());
35+
}
36+
} catch (URISyntaxException e) {
37+
throw new RuntimeException(e);
38+
}
39+
}
1640

17-
private List<String> pointerToLocation;
41+
private final URI rootDocumentURI;
42+
43+
private final List<String> pointerToLocation;
1844

1945
public SchemaLocation(URI rootDocumentURI, List<String> pointerToLocation) {
2046
this.rootDocumentURI = rootDocumentURI;
21-
this.pointerToLocation = pointerToLocation;
47+
this.pointerToLocation = new ArrayList<>(requireNonNull(pointerToLocation, "pointerToLocation cannot be null"));
2248
}
2349

2450
public SchemaLocation(List<String> pointerToLocation) {
51+
rootDocumentURI = null;
2552
this.pointerToLocation = pointerToLocation;
2653
}
2754

@@ -32,6 +59,20 @@ public SchemaLocation addPointerSegment(String key) {
3259
return new SchemaLocation(rootDocumentURI, newPointer);
3360
}
3461

62+
@Override public boolean equals(Object o) {
63+
if (this == o)
64+
return true;
65+
if (!(o instanceof SchemaLocation))
66+
return false;
67+
SchemaLocation that = (SchemaLocation) o;
68+
return Objects.equals(rootDocumentURI, that.rootDocumentURI) &&
69+
pointerToLocation.equals(that.pointerToLocation);
70+
}
71+
72+
@Override public int hashCode() {
73+
return Objects.hash(rootDocumentURI, pointerToLocation);
74+
}
75+
3576
@Override
3677
public String toString() {
3778
StringBuilder buffer = new StringBuilder();

core/src/main/java/org/everit/json/schema/loader/ReferenceLookup.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ Schema.Builder<?> lookup(String relPointerString, JsonObject ctx) {
161161
JsonPointerEvaluator.QueryResult result = pointer.query();
162162

163163
SchemaLoader childLoader = ls.initChildLoader()
164-
// .pointerToCurrentObj(asList(absPointerString))
165-
.pointerToCurrentObj(SchemaLocation.empty())
164+
.pointerToCurrentObj(SchemaLocation.parseURI(absPointerString))
166165
.resolutionScope(!isInternal ? withoutFragment(absPointerString) : ls.id)
167166
.schemaJson(result.getQueryResult())
168167
.rootSchemaJson(result.getContainingDocument()).build();

core/src/test/java/org/everit/json/schema/ArraySchemaTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void uniqueObjectValues() {
179179
public void equalsVerifier() {
180180
EqualsVerifier.forClass(ArraySchema.class)
181181
.withRedefinedSuperclass()
182-
.withIgnoredFields("schemaLocation")
182+
.withIgnoredFields("schemaLocation", "location")
183183
.suppress(Warning.STRICT_INHERITANCE)
184184
.verify();
185185
}

core/src/test/java/org/everit/json/schema/CombinedSchemaTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void reportCauses() {
118118
public void equalsVerifier() {
119119
EqualsVerifier.forClass(CombinedSchema.class)
120120
.withRedefinedSuperclass()
121-
.withIgnoredFields("schemaLocation")
121+
.withIgnoredFields("schemaLocation", "location")
122122
.suppress(Warning.STRICT_INHERITANCE)
123123
.verify();
124124
}

core/src/test/java/org/everit/json/schema/EmptySchemaTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.everit.json.schema;
1717

18-
import nl.jqno.equalsverifier.EqualsVerifier;
19-
import nl.jqno.equalsverifier.Warning;
2018
import org.json.JSONObject;
2119
import org.junit.Assert;
2220
import org.junit.Test;
2321

22+
import nl.jqno.equalsverifier.EqualsVerifier;
23+
import nl.jqno.equalsverifier.Warning;
24+
2425
public class EmptySchemaTest {
2526

2627
@Test
@@ -74,7 +75,7 @@ public void testAllGenericProps() {
7475
public void equalsVerifier() {
7576
EqualsVerifier.forClass(EmptySchema.class)
7677
.withRedefinedSuperclass()
77-
.withIgnoredFields("schemaLocation")
78+
.withIgnoredFields("schemaLocation", "location")
7879
.suppress(Warning.STRICT_INHERITANCE)
7980
.verify();
8081
}

core/src/test/java/org/everit/json/schema/EnumSchemaTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void objectInArrayMatches() {
7777
possibleValues.add(arr);
7878

7979
EnumSchema subject = subject().build();
80-
80+
8181
subject.validate(new JSONArray("[{\"a\":true}]"));
8282
}
8383

@@ -101,7 +101,7 @@ public void toStringTest() {
101101
public void equalsVerifier() {
102102
EqualsVerifier.forClass(EnumSchema.class)
103103
.withRedefinedSuperclass()
104-
.withIgnoredFields("schemaLocation")
104+
.withIgnoredFields("schemaLocation", "location")
105105
.suppress(Warning.STRICT_INHERITANCE)
106106
.verify();
107107
}

core/src/test/java/org/everit/json/schema/NotSchemaTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616
package org.everit.json.schema;
1717

18-
import nl.jqno.equalsverifier.EqualsVerifier;
19-
import nl.jqno.equalsverifier.Warning;
20-
import org.junit.Test;
21-
2218
import static org.everit.json.schema.TestSupport.buildWithLocation;
2319
import static org.junit.Assert.assertEquals;
2420

21+
import org.junit.Test;
22+
23+
import nl.jqno.equalsverifier.EqualsVerifier;
24+
import nl.jqno.equalsverifier.Warning;
25+
2526
public class NotSchemaTest {
2627

2728
@Test
@@ -42,7 +43,7 @@ public void success() {
4243
public void equalsVerifier() {
4344
EqualsVerifier.forClass(NotSchema.class)
4445
.withRedefinedSuperclass()
45-
.withIgnoredFields("schemaLocation")
46+
.withIgnoredFields("schemaLocation", "location")
4647
.suppress(Warning.STRICT_INHERITANCE)
4748
.verify();
4849
}

core/src/test/java/org/everit/json/schema/NullSchemaTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package org.everit.json.schema;
1717

18-
import nl.jqno.equalsverifier.EqualsVerifier;
19-
import nl.jqno.equalsverifier.Warning;
18+
import static org.junit.Assert.assertEquals;
19+
2020
import org.json.JSONObject;
2121
import org.junit.Test;
2222

23-
import static org.junit.Assert.assertEquals;
23+
import nl.jqno.equalsverifier.EqualsVerifier;
24+
import nl.jqno.equalsverifier.Warning;
2425

2526
public class NullSchemaTest {
2627

@@ -42,7 +43,7 @@ public void success() {
4243
public void equalsVerifier() {
4344
EqualsVerifier.forClass(NullSchema.class)
4445
.withRedefinedSuperclass()
45-
.withIgnoredFields("schemaLocation")
46+
.withIgnoredFields("schemaLocation", "location")
4647
.suppress(Warning.STRICT_INHERITANCE)
4748
.verify();
4849
}

0 commit comments

Comments
 (0)