Maintain formatting during subsection extraction

Closes gh-466
This commit is contained in:
Andy Wilkinson
2018-11-22 11:33:34 +00:00
parent 9e1ba8a922
commit 71198a402a
2 changed files with 53 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ import java.util.List;
import java.util.Set;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.http.MediaType;
import org.springframework.restdocs.payload.JsonFieldProcessor.ExtractedField;
@@ -39,6 +40,9 @@ public class FieldPathPayloadSubsectionExtractor
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper prettyPrintingOjectMapper = new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT);
private final String fieldPath;
private final String subsectionId;
@@ -90,7 +94,7 @@ public class FieldPathPayloadSubsectionExtractor
throw new PayloadHandlingException(message);
}
}
return objectMapper.writeValueAsBytes(value);
return getObjectMapper(payload).writeValueAsBytes(value);
}
catch (IOException ex) {
throw new PayloadHandlingException(ex);
@@ -115,4 +119,20 @@ public class FieldPathPayloadSubsectionExtractor
return new FieldPathPayloadSubsectionExtractor(this.fieldPath, subsectionId);
}
private ObjectMapper getObjectMapper(byte[] payload) {
if (isPrettyPrinted(payload)) {
return prettyPrintingOjectMapper;
}
return objectMapper;
}
private boolean isPrettyPrinted(byte[] payload) {
for (byte b : payload) {
if (b == '\n') {
return true;
}
}
return false;
}
}

View File

@@ -20,8 +20,10 @@ import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -116,4 +118,34 @@ public class FieldPathPayloadSubsectionExtractorTests {
MediaType.APPLICATION_JSON);
}
@Test
public void extractedSubsectionIsPrettyPrintedWhenInputIsPrettyPrinted()
throws JsonParseException, JsonMappingException, JsonProcessingException,
IOException {
ObjectMapper objectMapper = new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT);
byte[] prettyPrintedPayload = objectMapper.writeValueAsBytes(
objectMapper.readValue("{\"a\": { \"b\": { \"c\": 1 }}}", Object.class));
byte[] extractedSubsection = new FieldPathPayloadSubsectionExtractor("a.b")
.extractSubsection(prettyPrintedPayload, MediaType.APPLICATION_JSON);
byte[] prettyPrintedSubsection = objectMapper
.writeValueAsBytes(objectMapper.readValue("{\"c\": 1 }", Object.class));
assertThat(new String(extractedSubsection))
.isEqualTo(new String(prettyPrintedSubsection));
}
@Test
public void extractedSubsectionIsNotPrettyPrintedWhenInputIsNotPrettyPrinted()
throws JsonParseException, JsonMappingException, JsonProcessingException,
IOException {
ObjectMapper objectMapper = new ObjectMapper();
byte[] payload = objectMapper.writeValueAsBytes(
objectMapper.readValue("{\"a\": { \"b\": { \"c\": 1 }}}", Object.class));
byte[] extractedSubsection = new FieldPathPayloadSubsectionExtractor("a.b")
.extractSubsection(payload, MediaType.APPLICATION_JSON);
byte[] subsection = objectMapper
.writeValueAsBytes(objectMapper.readValue("{\"c\": 1 }", Object.class));
assertThat(new String(extractedSubsection)).isEqualTo(new String(subsection));
}
}