Consider sometimes present field beneath optional array to not be missing

The changes made in 7476562d inadvertently caused a field that is
an descendant of an array and a child of an optional field to be
considered missing when its parent is only sometimes present. Due to
the parent being optional, the parent’s absence should not cause the
absent child to be considered to be missing.

Closes gh-519
This commit is contained in:
Andy Wilkinson
2018-06-26 12:06:33 +01:00
parent 20bc7b3cd8
commit 7bde04ea46
2 changed files with 13 additions and 0 deletions

View File

@@ -81,6 +81,9 @@ class JsonContentHandler implements ContentHandler {
}
private boolean isMissing(FieldDescriptor candidate, Object payload) {
if (!this.fieldProcessor.hasField(candidate.getPath(), payload)) {
return true;
}
try {
ExtractedField extracted = this.fieldProcessor.extract(candidate.getPath(),
payload);

View File

@@ -157,4 +157,14 @@ public class JsonContentHandlerTests {
assertThat(missingFields.size(), is(equalTo(0)));
}
@Test
public void describedFieldThatIsSometimesPresentChildOfOptionalArrayIsNotConsideredMissing() {
List<FieldDescriptor> missingFields = new JsonContentHandler(
"{\"a\":[ {\"b\": \"bravo\"}, {\"b\": \"bravo\", \"c\": { \"d\": \"delta\"}}]}"
.getBytes()).findMissingFields(
Arrays.asList(new FieldDescriptor("a.[].c").optional(),
new FieldDescriptor("a.[].c.d]")));
assertThat(missingFields.size(), is(equalTo(0)));
}
}