Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ private boolean isNestedBeneathMissingOptionalField(FieldDescriptor descriptor,
List<FieldDescriptor> candidates = new ArrayList<>(this.fieldDescriptors);
candidates.remove(descriptor);
for (FieldDescriptor candidate : candidates) {
if (candidate.isOptional() && descriptor.getPath().startsWith(candidate.getPath())
if (candidate.isOptional() && isNestedBeneath(candidate.getPath(), descriptor.getPath())
&& isMissing(candidate, payload)) {
return true;
}
}
return false;
}

private boolean isNestedBeneath(String ancestorPath, String path) {
return path.equals(ancestorPath) || path.startsWith(ancestorPath + ".") || path.startsWith(ancestorPath + "[");
}

private boolean isMissing(FieldDescriptor candidate, Object payload) {
if (!this.fieldProcessor.hasField(candidate.getPath(), payload)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ void describedFieldThatIsNotPresentNestedBeneathOptionalFieldThatIsNotPresentIsN
assertThat(missingFields.size()).isEqualTo(0);
}

@Test
void describedFieldThatIsNotPresentWithPathPrefixedByPathOfMissingOptionalFieldIsConsideredMissing() {
List<FieldDescriptor> descriptors = Arrays.asList(new FieldDescriptor("a").optional(),
new FieldDescriptor("ab"));
List<FieldDescriptor> missingFields = new JsonContentHandler("{\"c\":\"charlie\"}".getBytes(), descriptors)
.findMissingFields();
assertThat(missingFields.size()).isEqualTo(1);
assertThat(missingFields.get(0).getPath()).isEqualTo("ab");
}

@Test
void describedNestedFieldThatIsNotPresentWithPathPrefixedByPathOfMissingOptionalFieldIsConsideredMissing() {
List<FieldDescriptor> descriptors = Arrays.asList(new FieldDescriptor("a.b").optional(),
new FieldDescriptor("a.bc"));
List<FieldDescriptor> missingFields = new JsonContentHandler("{\"a\":{}}".getBytes(), descriptors)
.findMissingFields();
assertThat(missingFields.size()).isEqualTo(1);
assertThat(missingFields.get(0).getPath()).isEqualTo("a.bc");
}

@Test
void describedFieldThatIsNotPresentNestedBeneathOptionalArrayThatIsEmptyIsNotConsideredMissing() {
List<FieldDescriptor> descriptors = Arrays.asList(new FieldDescriptor("outer"),
Expand Down
Loading