From 88fe8a6f137c03846e3870d8242defe87a0aa7c8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 15 Jun 2021 11:37:29 +0100 Subject: [PATCH] Stop pretty printing from truncating content starts as valid JSON Fixes gh-730 --- .../preprocess/PrettyPrintingContentModifier.java | 7 ++++--- .../preprocess/PrettyPrintingContentModifierTests.java | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java index dff8f1d3..4e6efbe2 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.xml.sax.ErrorHandler; @@ -140,8 +141,8 @@ public class PrettyPrintingContentModifier implements ContentModifier { private static final class JsonPrettyPrinter implements PrettyPrinter { - private final ObjectMapper objectMapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, - true); + private final ObjectMapper objectMapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true) + .configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, true); @Override public byte[] prettyPrint(byte[] original) throws IOException { diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java index d5a7d484..f1cc0ac3 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java @@ -64,7 +64,14 @@ public class PrettyPrintingContentModifierTests { this.outputCapture.expect(isEmptyString()); assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes(), null)) .isEqualTo(content.getBytes()); + } + @Test + public void nonJsonContentThatInitiallyLooksLikeJsonIsHandledGracefully() throws Exception { + String content = "\"abc\",\"def\""; + this.outputCapture.expect(isEmptyString()); + assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes(), null)) + .isEqualTo(content.getBytes()); } @Test