Skip to content

Commit 448967f

Browse files
committed
supporting the nullable flag in Schema#toString()
1 parent 9eb0229 commit 448967f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public void describeTo(JSONPrinter writer) {
220220
writer.ifPresent("description", description);
221221
writer.ifPresent("id", id);
222222
writer.ifPresent("default", defaultValue);
223+
writer.ifPresent("nullable", nullable);
223224
describePropertiesTo(writer);
224225
writer.endObject();
225226
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import static org.junit.Assert.assertTrue;
2929

3030
public class StringSchemaTest {
31+
32+
private static Schema loadWithNullableSupport(JSONObject rawSchemaJson) {
33+
return SchemaLoader.builder().nullableSupport(true).schemaJson(rawSchemaJson).build().load().build();
34+
}
3135

3236
@Test
3337
public void formatFailure() {
@@ -123,6 +127,22 @@ public void toStringTest() {
123127
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
124128
}
125129

130+
@Test
131+
public void toStringWithNullableTrueTest() {
132+
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");
133+
rawSchemaJson.put("nullable", true);
134+
String actual = loadWithNullableSupport(rawSchemaJson).toString();
135+
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
136+
}
137+
138+
@Test
139+
public void toStringWithNullableFalseTest() {
140+
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");
141+
rawSchemaJson.put("nullable", false);
142+
String actual = loadWithNullableSupport(rawSchemaJson).toString();
143+
assertTrue(ObjectComparator.deepEquals(rawSchemaJson, new JSONObject(actual)));
144+
}
145+
126146
@Test
127147
public void toStringNoFormat() {
128148
JSONObject rawSchemaJson = ResourceLoader.DEFAULT.readObj("tostring/stringschema.json");

0 commit comments

Comments
 (0)