diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractor.java index 9b0b0fbb..94292aa9 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractor.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractor.java @@ -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; + } + } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java index 7ef2306f..9ffcd7d7 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java @@ -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)); + } + }