Skip to content

Commit 2f0a7c3

Browse files
committed
implementation of ReferenceSchema#describePropertiesTo()
1 parent 89d8aa1 commit 2f0a7c3

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package org.everit.json.schema;
1717

18+
import org.everit.json.schema.internal.JSONPrinter;
19+
1820
import java.util.Objects;
1921

22+
import static java.util.Objects.requireNonNull;
23+
2024
/**
2125
* This class is used by {@link org.everit.json.schema.loader.SchemaLoader} to resolve JSON pointers
2226
* during the construction of the schema. This class has been made mutable to permit the loading of
@@ -31,6 +35,11 @@ public static class Builder extends Schema.Builder<ReferenceSchema> {
3135

3236
private ReferenceSchema retval;
3337

38+
/**
39+
* The value of {@code "$ref"}
40+
*/
41+
private String refValue = "";
42+
3443
/**
3544
* This method caches its result, so multiple invocations will return referentially the same
3645
* {@link ReferenceSchema} instance.
@@ -43,6 +52,10 @@ public ReferenceSchema build() {
4352
return retval;
4453
}
4554

55+
public Builder refValue(String refValue) {
56+
this.refValue = refValue;
57+
return this;
58+
}
4659
}
4760

4861
public static Builder builder() {
@@ -51,8 +64,11 @@ public static Builder builder() {
5164

5265
private Schema referredSchema;
5366

67+
private final String refValue;
68+
5469
public ReferenceSchema(final Builder builder) {
5570
super(builder);
71+
this.refValue = requireNonNull(builder.refValue, "refValue cannot be null");
5672
}
5773

5874
@Override
@@ -95,6 +111,7 @@ public boolean equals(Object o) {
95111
if (o instanceof ReferenceSchema) {
96112
ReferenceSchema that = (ReferenceSchema) o;
97113
return that.canEqual(this) &&
114+
Objects.equals(refValue, that.refValue) &&
98115
Objects.equals(referredSchema, that.referredSchema) &&
99116
super.equals(that);
100117
} else {
@@ -104,11 +121,16 @@ public boolean equals(Object o) {
104121

105122
@Override
106123
public int hashCode() {
107-
return Objects.hash(super.hashCode(), referredSchema);
124+
return Objects.hash(super.hashCode(), referredSchema, refValue);
108125
}
109126

110127
@Override
111128
protected boolean canEqual(Object other) {
112129
return other instanceof ReferenceSchema;
113130
}
131+
132+
@Override void describePropertiesTo(JSONPrinter writer) {
133+
writer.key("$ref");
134+
writer.value(refValue);
135+
}
114136
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,11 @@ public SchemaLoaderBuilder addFormatValidator(FormatValidator formatValidator) {
8484
}
8585

8686
/**
87-
* @param formatName
88-
* the name which will be used in the schema JSON files to refer to this {@code formatValidator}
89-
* @param formatValidator
90-
* the object performing the validation for schemas which use the {@code formatName} format
87+
* @param formatName the name which will be used in the schema JSON files to refer to this {@code formatValidator}
88+
* @param formatValidator the object performing the validation for schemas which use the {@code formatName} format
9189
* @return {@code this}
9290
* @deprecated instead it is better to override {@link FormatValidator#formatName()}
93-
* and use {@link #addFormatValidator(FormatValidator)}
91+
* and use {@link #addFormatValidator(FormatValidator)}
9492
*/
9593
@Deprecated
9694
public SchemaLoaderBuilder addFormatValidator(final String formatName,
@@ -299,8 +297,7 @@ private void addDependency(final Builder builder, final String ifPresent, final
299297
typeMultiplexer(deps)
300298
.ifObject().then(obj -> {
301299
builder.schemaDependency(ifPresent, loadChild(obj).build());
302-
})
303-
.ifIs(JSONArray.class).then(propNames -> {
300+
}).ifIs(JSONArray.class).then(propNames -> {
304301
IntStream.range(0, propNames.length())
305302
.mapToObj(i -> propNames.getString(i))
306303
.forEach(dependency -> builder.propertyDependency(ifPresent, dependency));
@@ -560,7 +557,8 @@ private Schema.Builder<?> lookupReference(final String relPointerString, final J
560557
JSONPointer pointer = isExternal
561558
? JSONPointer.forURL(httpClient, absPointerString)
562559
: JSONPointer.forDocument(rootSchemaJson, absPointerString);
563-
ReferenceSchema.Builder refBuilder = ReferenceSchema.builder();
560+
ReferenceSchema.Builder refBuilder = ReferenceSchema.builder()
561+
.refValue(relPointerString);
564562
pointerSchemas.put(absPointerString, refBuilder);
565563
QueryResult result = pointer.query();
566564
JSONObject resultObject = extend(withoutRef(ctx), result.getQueryResult());

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
import nl.jqno.equalsverifier.EqualsVerifier;
1919
import nl.jqno.equalsverifier.Warning;
2020
import org.everit.json.schema.ReferenceSchema.Builder;
21+
import org.everit.json.schema.loader.SchemaLoader;
22+
import org.json.JSONObject;
2123
import org.junit.Assert;
2224
import org.junit.Test;
2325

26+
import static org.junit.Assert.assertTrue;
27+
2428
public class ReferenceSchemaTest {
2529

2630
@Test
@@ -45,4 +49,14 @@ public void equalsVerifier() {
4549
.suppress(Warning.STRICT_INHERITANCE)
4650
.verify();
4751
}
52+
53+
@Test
54+
public void toStringTest() {
55+
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/ref.json");
56+
String actual = SchemaLoader.load(rawSchemaJson).toString();
57+
System.out.println(actual);
58+
assertTrue(ObjectComparator.deepEquals(rawSchemaJson.query("/properties"),
59+
new JSONObject(actual).query("/properties")));
60+
}
61+
4862
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"properties": {
3+
"prop": {
4+
"$ref" : "#/definitions/Ref"
5+
}
6+
},
7+
"definitions" : {
8+
"Ref" : {
9+
"type" : "number"
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)