Skip to content

Commit b5a408a

Browse files
committed
changing readOnly and writeOnly from boolean to Boolean, and supporting them in Schema#toString()
1 parent db698c4 commit b5a408a

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public abstract static class Builder<S extends Schema> {
3333

3434
private Boolean nullable = null;
3535

36-
private boolean readOnly = false;
36+
private Boolean readOnly = null;
3737

38-
private boolean writeOnly = false;
38+
private Boolean writeOnly = null;
3939

4040
public Builder<S> title(String title) {
4141
this.title = title;
@@ -67,12 +67,12 @@ public Builder<S> nullable(Boolean nullable) {
6767
return this;
6868
}
6969

70-
public Builder<S> readOnly(boolean readOnly) {
70+
public Builder<S> readOnly(Boolean readOnly) {
7171
this.readOnly = readOnly;
7272
return this;
7373
}
7474

75-
public Builder<S> writeOnly(boolean writeOnly) {
75+
public Builder<S> writeOnly(Boolean writeOnly) {
7676
this.writeOnly = writeOnly;
7777
return this;
7878
}
@@ -93,9 +93,9 @@ public Builder<S> writeOnly(boolean writeOnly) {
9393

9494
private final Boolean nullable;
9595

96-
private final boolean readOnly;
96+
private final Boolean readOnly;
9797

98-
private final boolean writeOnly;
98+
private final Boolean writeOnly;
9999

100100
/**
101101
* Constructor.
@@ -186,8 +186,8 @@ public boolean equals(Object o) {
186186
Objects.equals(description, schema.description) &&
187187
Objects.equals(id, schema.id) &&
188188
Objects.equals(nullable, schema.nullable) &&
189-
readOnly == schema.readOnly &&
190-
writeOnly == schema.writeOnly;
189+
Objects.equals(readOnly, schema.readOnly) &&
190+
Objects.equals(writeOnly, schema.writeOnly);
191191
} else {
192192
return false;
193193
}
@@ -252,6 +252,8 @@ public void describeTo(JSONPrinter writer) {
252252
writer.ifPresent("id", id);
253253
writer.ifPresent("default", defaultValue);
254254
writer.ifPresent("nullable", nullable);
255+
writer.ifPresent("readOnly", readOnly);
256+
writer.ifPresent("writeOnly", writeOnly);
255257
describePropertiesTo(writer);
256258
writer.endObject();
257259
}

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@
1515
*/
1616
package org.everit.json.schema;
1717

18-
import nl.jqno.equalsverifier.EqualsVerifier;
19-
import nl.jqno.equalsverifier.Warning;
18+
import static org.everit.json.schema.ObjectComparator.deepEquals;
19+
import static org.everit.json.schema.TestSupport.buildWithLocation;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import java.util.Optional;
23+
2024
import org.everit.json.schema.loader.SchemaLoader;
2125
import org.json.JSONObject;
2226
import org.junit.Assert;
2327
import org.junit.Test;
2428

25-
import java.util.Optional;
26-
27-
import static org.everit.json.schema.TestSupport.buildWithLocation;
28-
import static org.junit.Assert.assertTrue;
29+
import nl.jqno.equalsverifier.EqualsVerifier;
30+
import nl.jqno.equalsverifier.Warning;
2931

3032
public class StringSchemaTest {
3133

@@ -124,39 +126,48 @@ public void equalsVerifier() {
124126
public void toStringTest() {
125127
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");
126128
String actual = SchemaLoader.load(rawSchemaJson).toString();
127-
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
129+
assertTrue(deepEquals(rawSchemaJson, new JSONObject(actual)));
128130
}
129131

130132
@Test
131133
public void toStringWithNullableTrueTest() {
132134
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");
133135
rawSchemaJson.put("nullable", true);
134136
String actual = loadWithNullableSupport(rawSchemaJson).toString();
135-
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
137+
assertTrue(deepEquals(rawSchemaJson, new JSONObject(actual)));
136138
}
137139

138140
@Test
139141
public void toStringWithNullableFalseTest() {
140142
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");
141143
rawSchemaJson.put("nullable", false);
142144
String actual = loadWithNullableSupport(rawSchemaJson).toString();
143-
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
145+
assertTrue(deepEquals(rawSchemaJson, new JSONObject(actual)));
144146
}
145147

146148
@Test
147149
public void toStringNoFormat() {
148150
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");
149151
rawSchemaJson.remove("format");
150152
String actual = SchemaLoader.load(rawSchemaJson).toString();
151-
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
153+
assertTrue(deepEquals(rawSchemaJson, new JSONObject(actual)));
152154
}
153155

154156
@Test
155157
public void toStringNoExplicitType() {
156158
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");
157159
rawSchemaJson.remove("type");
158160
String actual = SchemaLoader.load(rawSchemaJson).toString();
159-
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
161+
assertTrue(deepEquals(rawSchemaJson, new JSONObject(actual)));
162+
}
163+
164+
@Test
165+
public void toString_ReadOnlyWriteOnly() {
166+
Schema subject = StringSchema.builder().readOnly(true).writeOnly(false).build();
167+
JSONObject actual = new JSONObject(subject.toString());
168+
169+
JSONObject expected = ResourceLoader.DEFAULT.readObj("tostring/stringschema-readonly-true-writeonly-false.json");
170+
assertTrue(deepEquals(actual, expected));
160171
}
161172

162173
@Test
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "string",
3+
"readOnly": true,
4+
"writeOnly": false
5+
}

0 commit comments

Comments
 (0)