Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 60abd8c

Browse files
#424: added "hal+json" as compatible content type for ArrayLimitingJsonContentModifier (#425)
1 parent 10064cf commit 60abd8c

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/response/JsonContentModifier.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
*/
2020
package capital.scalable.restdocs.response;
2121

22-
import static org.springframework.http.MediaType.APPLICATION_JSON;
23-
2422
import java.io.IOException;
23+
import java.util.Arrays;
24+
import java.util.List;
2525

2626
import com.fasterxml.jackson.databind.JsonNode;
2727
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -30,16 +30,16 @@
3030

3131
public abstract class JsonContentModifier implements ContentModifier {
3232

33-
private ObjectMapper objectMapper;
33+
private static final List<MediaType> supportedMediaTypes = Arrays.asList(MediaType.APPLICATION_JSON, MediaType.valueOf("application/*+json"));
34+
private final ObjectMapper objectMapper;
3435

3536
public JsonContentModifier(ObjectMapper objectMapper) {
3637
this.objectMapper = objectMapper;
3738
}
3839

3940
@Override
4041
public byte[] modifyContent(byte[] originalContent, MediaType contentType) {
41-
if (originalContent.length > 0 && contentType != null
42-
&& contentType.includes(APPLICATION_JSON)) {
42+
if (originalContent.length > 0 && supportedMediaTypes.stream().anyMatch(type -> type.isCompatibleWith(contentType))) {
4343
return modifyContent(originalContent);
4444
} else {
4545
return originalContent;

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/response/ResponseModifyingPreprocessorsTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ public void shouldShortenJsonArrays() {
7373
.expectedResult("[1,2,3]"));
7474
}
7575

76+
@Test
77+
public void shouldShortenJsonArraysForHALMediaType() {
78+
preprocessorTest(new TestData()
79+
.content("[1,2,3,4]")
80+
.contentType("application/hal+json")
81+
.preprocessor(
82+
ResponseModifyingPreprocessors.limitJsonArrayLength(mapper))
83+
.expectedResult("[1,2,3]"));
84+
}
85+
86+
@Test
87+
public void shouldShortenJsonArraysForAnyJsonSuffixMediaType() {
88+
preprocessorTest(new TestData()
89+
.content("[1,2,3,4]")
90+
.contentType("application/whatever+json")
91+
.preprocessor(
92+
ResponseModifyingPreprocessors.limitJsonArrayLength(mapper))
93+
.expectedResult("[1,2,3]"));
94+
}
95+
7696
@Test
7797
public void shouldShortenNestedJsonArrays() {
7898
preprocessorTest(new TestData()

0 commit comments

Comments
 (0)