Skip to content

Commit 2e0b5dc

Browse files
committed
disabling readOnly / writeOnly keywords in older draft versions
1 parent 5fd1a9e commit 2e0b5dc

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,10 @@ private void loadCommonSchemaProperties(Schema.Builder builder) {
391391
ls.schemaJson().maybe(config.specVersion.idKeyword()).map(JsonValue::requireString).ifPresent(builder::id);
392392
ls.schemaJson().maybe("title").map(JsonValue::requireString).ifPresent(builder::title);
393393
ls.schemaJson().maybe("description").map(JsonValue::requireString).ifPresent(builder::description);
394-
ls.schemaJson().maybe("readOnly").map(JsonValue::requireBoolean).ifPresent(builder::readOnly);
395-
ls.schemaJson().maybe("writeOnly").map(JsonValue::requireBoolean).ifPresent(builder::writeOnly);
394+
if (ls.specVersion() == DRAFT_7) {
395+
ls.schemaJson().maybe("readOnly").map(JsonValue::requireBoolean).ifPresent(builder::readOnly);
396+
ls.schemaJson().maybe("writeOnly").map(JsonValue::requireBoolean).ifPresent(builder::writeOnly);
397+
}
396398
if (config.nullableSupport) {
397399
builder.nullable(ls.schemaJson()
398400
.maybe("nullable")

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
import static org.hamcrest.CoreMatchers.containsString;
1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertSame;
2021
import static org.junit.Assert.assertThat;
22+
import static org.junit.Assert.fail;
2123

2224
import java.io.ByteArrayInputStream;
2325
import java.io.InputStream;
2426
import java.util.List;
2527

2628
import org.everit.json.schema.loader.SchemaLoader;
27-
import org.junit.Assert;
2829

2930
public class TestSupport {
3031

@@ -114,8 +115,17 @@ public static SchemaLoader.SchemaLoaderBuilder v6Loader() {
114115
return SchemaLoader.builder().draftV6Support();
115116
}
116117

118+
public static SchemaLoader.SchemaLoaderBuilder v7Loader() {
119+
return SchemaLoader.builder().draftV7Support();
120+
}
121+
117122
public static Schema loadAsV6(Object schema) {
118-
SchemaLoader loader = v6Loader().schemaJson(schema).draftV6Support().build();
123+
SchemaLoader loader = v6Loader().schemaJson(schema).build();
124+
return loader.load().build();
125+
}
126+
127+
public static Schema loadAsV7(Object schema) {
128+
SchemaLoader loader = v7Loader().schemaJson(schema).build();
119129
return loader.load().build();
120130
}
121131

@@ -138,7 +148,7 @@ public static void expectFailure(final Schema failingSchema,
138148
try {
139149
test(failingSchema, expectedPointer, input);
140150
} catch (ValidationException e) {
141-
Assert.assertSame(expectedViolatedSchemaClass, e.getViolatedSchema().getClass());
151+
assertSame(expectedViolatedSchemaClass, e.getViolatedSchema().getClass());
142152
}
143153
}
144154

@@ -152,7 +162,7 @@ public static void expectFailure(final Schema failingSchema,
152162
try {
153163
test(failingSchema, expectedPointer, input);
154164
} catch (ValidationException e) {
155-
Assert.assertSame(expectedViolatedSchema, e.getViolatedSchema());
165+
assertSame(expectedViolatedSchema, e.getViolatedSchema());
156166
}
157167
}
158168

@@ -164,9 +174,9 @@ public static void expectFailure(final Schema failingSchema, final String expect
164174
public static void expectFailure(final Failure failure) {
165175
try {
166176
failure.validator.performValidation(failure.subject, failure.input);
167-
Assert.fail(failure.subject + " did not fail for " + failure.input);
177+
fail(failure.subject + " did not fail for " + failure.input);
168178
} catch (ValidationException e) {
169-
Assert.assertSame(failure.expectedViolatedSchema(), e.getViolatedSchema());
179+
assertSame(failure.expectedViolatedSchema(), e.getViolatedSchema());
170180
assertEquals(failure.expectedPointer, e.getPointerToViolation());
171181
assertEquals(failure.expectedSchemaLocation, e.getSchemaLocation());
172182
if (failure.expectedKeyword != null) {
@@ -186,7 +196,7 @@ private static void test(final Schema failingSchema, final String expectedPointe
186196
final Object input) {
187197
try {
188198
failingSchema.validate(input);
189-
Assert.fail(failingSchema + " did not fail for " + input);
199+
fail(failingSchema + " did not fail for " + input);
190200
} catch (ValidationException e) {
191201
if (expectedPointer != null) {
192202
assertEquals(expectedPointer, e.getPointerToViolation());

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

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

3+
import static org.everit.json.schema.TestSupport.loadAsV7;
34
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertTrue;
56
import static org.junit.Assert.fail;
67

7-
import org.everit.json.schema.loader.SchemaLoader;
88
import org.json.JSONObject;
99
import org.junit.Test;
1010

1111
public class ValidatorTest {
1212

13-
private static final ObjectSchema RW_SCHEMA = (ObjectSchema) SchemaLoader
14-
.load(ResourceLoader.DEFAULT.readObj("read-write-context.json"));
13+
private static final ObjectSchema RW_SCHEMA = (ObjectSchema) loadAsV7(ResourceLoader.DEFAULT.readObj("read-write-context.json"));
1514

1615
@Test
1716
public void testCollectAllMode() {

core/src/test/java/org/everit/json/schema/loader/ReadWriteContextLoadingTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.everit.json.schema.loader;
22

3+
import static org.everit.json.schema.TestSupport.loadAsV6;
4+
import static org.everit.json.schema.TestSupport.loadAsV7;
5+
import static org.junit.Assert.assertNull;
36
import static org.junit.Assert.assertTrue;
47

58
import org.everit.json.schema.ObjectSchema;
@@ -13,16 +16,25 @@ public class ReadWriteContextLoadingTest {
1316

1417
@Test
1518
public void testReadOnlyContext() {
16-
ObjectSchema rootSchema = (ObjectSchema) SchemaLoader.load(LOADER.readObj("read-write-context.json"));
19+
ObjectSchema rootSchema = (ObjectSchema) loadAsV7(LOADER.readObj("read-write-context.json"));
1720
Schema readOnlyProp = rootSchema.getPropertySchemas().get("readOnlyProp");
1821
assertTrue(readOnlyProp.isReadOnly());
1922
}
2023

2124
@Test
2225
public void testWriteOnlyContext() {
23-
ObjectSchema rootSchema = (ObjectSchema) SchemaLoader.load(LOADER.readObj("read-write-context.json"));
24-
Schema readOnlyProp = rootSchema.getPropertySchemas().get("writeOnlyProp");
25-
assertTrue(readOnlyProp.isWriteOnly());
26+
ObjectSchema rootSchema = (ObjectSchema) loadAsV7(LOADER.readObj("read-write-context.json"));
27+
Schema writeOnlyProp = rootSchema.getPropertySchemas().get("writeOnlyProp");
28+
assertTrue(writeOnlyProp.isWriteOnly());
29+
}
30+
31+
@Test
32+
public void worksOnlyInV7Mode() {
33+
ObjectSchema rootSchema = (ObjectSchema) loadAsV6(LOADER.readObj("read-write-context.json"));
34+
Schema readOnlyProp = rootSchema.getPropertySchemas().get("readOnlyProp");
35+
Schema writeOnlyProp = rootSchema.getPropertySchemas().get("writeOnlyProp");
36+
assertNull(readOnlyProp.isReadOnly());
37+
assertNull(writeOnlyProp.isWriteOnly());
2638
}
2739

2840
}

0 commit comments

Comments
 (0)