Skip to content

Commit 88fe8a6

Browse files
committed
Stop pretty printing from truncating content starts as valid JSON
Fixes gh-730
1 parent f3e305a commit 88fe8a6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import javax.xml.transform.sax.SAXSource;
3535
import javax.xml.transform.stream.StreamResult;
3636

37+
import com.fasterxml.jackson.databind.DeserializationFeature;
3738
import com.fasterxml.jackson.databind.ObjectMapper;
3839
import com.fasterxml.jackson.databind.SerializationFeature;
3940
import org.xml.sax.ErrorHandler;
@@ -140,8 +141,8 @@ public void fatalError(SAXParseException exception) throws SAXException {
140141

141142
private static final class JsonPrettyPrinter implements PrettyPrinter {
142143

143-
private final ObjectMapper objectMapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT,
144-
true);
144+
private final ObjectMapper objectMapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true)
145+
.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, true);
145146

146147
@Override
147148
public byte[] prettyPrint(byte[] original) throws IOException {

spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ public void nonJsonAndNonXmlContentIsHandledGracefully() throws Exception {
6464
this.outputCapture.expect(isEmptyString());
6565
assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes(), null))
6666
.isEqualTo(content.getBytes());
67+
}
6768

69+
@Test
70+
public void nonJsonContentThatInitiallyLooksLikeJsonIsHandledGracefully() throws Exception {
71+
String content = "\"abc\",\"def\"";
72+
this.outputCapture.expect(isEmptyString());
73+
assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes(), null))
74+
.isEqualTo(content.getBytes());
6875
}
6976

7077
@Test

0 commit comments

Comments
 (0)