Skip to content
Closed
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 @@ -90,7 +90,7 @@ public byte[] extractSubsection(byte[] payload, MediaType contentType, List<Fiel
(descriptor) -> JsonFieldPath.compile(this.fieldPath + "." + descriptor.getPath()),
this::prependFieldPath));
if (value instanceof List) {
List<?> extractedList = (List<?>) value;
List<?> extractedList = getNotAbsentExtractedField(value);
JsonContentHandler contentHandler = new JsonContentHandler(payload, descriptorsByPath.values());
Set<JsonFieldPath> uncommonPaths = JsonFieldPaths.from(extractedList).getUncommon().stream()
.map((path) -> JsonFieldPath.compile(this.fieldPath + "." + path)).filter((path) -> {
Expand All @@ -116,6 +116,17 @@ public byte[] extractSubsection(byte[] payload, MediaType contentType, List<Fiel
}
}

private List<?> getNotAbsentExtractedField(Object value) {
List<?> extractedList = ((List<?>) value)
.stream()
.filter(field -> !ExtractedField.ABSENT.equals(field))
.collect(Collectors.toList());
if (extractedList.isEmpty()) {
throw new PayloadHandlingException(this.fieldPath + " does not identify a section of the payload");
}
return extractedList;
}

private FieldDescriptor prependFieldPath(FieldDescriptor original) {
FieldDescriptor prefixed = new FieldDescriptor(this.fieldPath + "." + original.getPath());
if (original.isOptional()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ public void extractMapSubsectionWithVaryingStructureDueToOptionalParentFieldsFro
assertThat(extracted).containsOnlyKeys("c");
}

@Test
public void extractSubSectionWithWildcardAndFieldDescriptors() throws IOException {
byte[] extractedPayload = new FieldPathPayloadSubsectionExtractor("*.d")
.extractSubsection("{\"a\":{\"b\":1},\"c\":{\"d\":{\"e\":1,\"f\":2}}}".getBytes(),
MediaType.APPLICATION_JSON,
Arrays.asList(new FieldDescriptor("e"), new FieldDescriptor("f")));
Map<String, Object> extracted = new ObjectMapper().readValue(extractedPayload, Map.class);
assertThat(extracted.size()).isEqualTo(2);
assertThat(extracted).containsOnlyKeys("e", "f");
}

@Test
public void extractedSubsectionIsPrettyPrintedWhenInputIsPrettyPrinted()
throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
Expand Down Expand Up @@ -163,5 +174,4 @@ public void extractNonExistentSubsection() throws JsonParseException, JsonMappin
.isInstanceOf(PayloadHandlingException.class)
.hasMessage("a.c does not identify a section of the payload");
}

}