Skip to content

Commit 9437a90

Browse files
committed
added test for ObjectSchema immutability, also changed multipleSchemaDepViolation() test not to rely on HashMap iteration order
1 parent 1212fbe commit 9437a90

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,14 @@ public ObjectSchema(final Builder builder) {
195195
builder.requiredProperties));
196196
this.minProperties = builder.minProperties;
197197
this.maxProperties = builder.maxProperties;
198-
this.propertyDependencies = Collections.unmodifiableMap(builder.propertyDependencies);
199-
this.schemaDependencies = Collections.unmodifiableMap(builder.schemaDependencies);
198+
this.propertyDependencies = copyMap(builder.propertyDependencies);
199+
this.schemaDependencies = copyMap(builder.schemaDependencies);
200200
this.requiresObject = builder.requiresObject;
201-
this.patternProperties = Collections.unmodifiableMap(builder.patternProperties);
201+
this.patternProperties = copyMap(builder.patternProperties);
202+
}
203+
204+
private final <K, V> Map<K, V> copyMap(final Map<K, V> original) {
205+
return Collections.unmodifiableMap(new HashMap<>(original));
202206
}
203207

204208
private Stream<String> getAdditionalProperties(final JSONObject subject) {

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,22 @@ public void multipleSchemaDepViolation() {
8080
subject.validate(OBJECTS.get("schemaDepViolation"));
8181
Assert.fail("did not throw ValidationException");
8282
} catch (ValidationException e) {
83-
ValidationException billingAddressFailure = e.getCausingExceptions().get(0)
84-
.getCausingExceptions().get(0);
83+
ValidationException creditCardFailure = e.getCausingExceptions().get(0);
84+
ValidationException ageFailure = e.getCausingExceptions().get(1);
85+
// due to schemaDeps being stored in (unsorted) HashMap, the exceptions may need to be swapped
86+
if (creditCardFailure.getCausingExceptions().size() == 0) {
87+
ValidationException tmp = creditCardFailure;
88+
creditCardFailure = ageFailure;
89+
ageFailure = tmp;
90+
}
91+
ValidationException billingAddressFailure = creditCardFailure.getCausingExceptions().get(0);
8592
Assert.assertEquals("#/billing_address", billingAddressFailure.getPointerToViolation());
8693
Assert.assertEquals(billingAddressSchema, billingAddressFailure.getViolatedSchema());
87-
ValidationException billingNameFailure = e.getCausingExceptions().get(0)
94+
ValidationException billingNameFailure = creditCardFailure
8895
.getCausingExceptions().get(1);
8996
Assert.assertEquals("#/billing_name", billingNameFailure.getPointerToViolation());
9097
Assert.assertEquals(billingNameSchema, billingNameFailure.getViolatedSchema());
91-
ValidationException ageFailure = e.getCausingExceptions().get(1);
98+
9299
Assert.assertEquals("#", ageFailure.getPointerToViolation());
93100
Assert.assertEquals("required key [age] not found", ageFailure.getMessage());
94101
}
@@ -216,6 +223,21 @@ public void schemaForNoAdditionalProperties() {
216223
.schemaOfAdditionalProperties(BooleanSchema.INSTANCE).build();
217224
}
218225

226+
@Test
227+
public void testImmutability() {
228+
ObjectSchema.Builder builder = ObjectSchema.builder();
229+
builder.propertyDependency("a", "b");
230+
builder.schemaDependency("a", BooleanSchema.INSTANCE);
231+
builder.patternProperty("aaa", BooleanSchema.INSTANCE);
232+
ObjectSchema schema = builder.build();
233+
builder.propertyDependency("c", "a");
234+
builder.schemaDependency("b", BooleanSchema.INSTANCE);
235+
builder.patternProperty("bbb", BooleanSchema.INSTANCE);
236+
Assert.assertEquals(1, schema.getPropertyDependencies().size());
237+
Assert.assertEquals(1, schema.getSchemaDependencies().size());
238+
Assert.assertEquals(1, schema.getPatternProperties().size());
239+
}
240+
219241
@Test
220242
public void typeFailure() {
221243
TestSupport.expectFailure(ObjectSchema.builder().build(), "#", "a");

0 commit comments

Comments
 (0)