Skip to content

Commit 26dd2ed

Browse files
committed
Modify response- and request-body snippets to have language attribute depending on Content-Type header
1 parent 683686e commit 26dd2ed

File tree

7 files changed

+42
-4
lines changed

7 files changed

+42
-4
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractBodySnippet.java

+6
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,19 @@ protected AbstractBodySnippet(String name, String type, PayloadSubsectionExtract
7373
protected Map<String, Object> createModel(Operation operation) {
7474
try {
7575
MediaType contentType = getContentType(operation);
76+
String language = null;
77+
if (contentType != null) {
78+
language = (contentType.getSubtypeSuffix() != null) ? contentType.getSubtypeSuffix()
79+
: contentType.getSubtype();
80+
}
7681
byte[] content = getContent(operation);
7782
if (this.subsectionExtractor != null) {
7883
content = this.subsectionExtractor.extractSubsection(content, contentType);
7984
}
8085
Charset charset = extractCharset(contentType);
8186
String body = (charset != null) ? new String(content, charset) : new String(content);
8287
Map<String, Object> model = new HashMap<>();
88+
model.put("language", language);
8389
model.put("body", body);
8490
return model;
8591
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[source,options="nowrap"]
1+
[source{{#language}},{{language}}{{/language}},options="nowrap"]
22
----
33
{{body}}
44
----
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[source,options="nowrap"]
1+
[source{{#language}},{{language}}{{/language}},options="nowrap"]
22
----
33
{{body}}
44
----
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
```
1+
```{{#language}}{{language}}{{/language}}
22
{{body}}
33
```
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
```
1+
```{{#language}}{{language}}{{/language}}
22
{{body}}
33
```

spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestBodySnippetTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.junit.Test;
2222

23+
import org.springframework.http.HttpHeaders;
24+
import org.springframework.http.MediaType;
2325
import org.springframework.restdocs.AbstractSnippetTests;
2426
import org.springframework.restdocs.templates.TemplateEngine;
2527
import org.springframework.restdocs.templates.TemplateFormat;
@@ -58,6 +60,20 @@ public void requestWithNoBody() throws IOException {
5860
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock(null, "nowrap").withContent(""));
5961
}
6062

63+
@Test
64+
public void requestWithMediaTypeJson() throws IOException {
65+
requestBody().document(this.operationBuilder.request("http://localhost")
66+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build());
67+
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("json", "nowrap").withContent(""));
68+
}
69+
70+
@Test
71+
public void requestWithMediaTypeXml() throws IOException {
72+
requestBody().document(this.operationBuilder.request("http://localhost")
73+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build());
74+
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("xml", "nowrap").withContent(""));
75+
}
76+
6177
@Test
6278
public void subsectionOfRequestBody() throws IOException {
6379
requestBody(beneathPath("a.b")).document(

spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseBodySnippetTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.junit.Test;
2222

23+
import org.springframework.http.HttpHeaders;
24+
import org.springframework.http.MediaType;
2325
import org.springframework.restdocs.AbstractSnippetTests;
2426
import org.springframework.restdocs.templates.TemplateEngine;
2527
import org.springframework.restdocs.templates.TemplateFormat;
@@ -58,6 +60,20 @@ public void responseWithNoBody() throws IOException {
5860
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock(null, "nowrap").withContent(""));
5961
}
6062

63+
@Test
64+
public void responseWithMediaTypeJson() throws IOException {
65+
new ResponseBodySnippet().document(this.operationBuilder.response()
66+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build());
67+
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("json", "nowrap").withContent(""));
68+
}
69+
70+
@Test
71+
public void responseWithMediaTypeXml() throws IOException {
72+
new ResponseBodySnippet().document(this.operationBuilder.response()
73+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build());
74+
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("xml", "nowrap").withContent(""));
75+
}
76+
6177
@Test
6278
public void subsectionOfResponseBody() throws IOException {
6379
responseBody(beneathPath("a.b"))

0 commit comments

Comments
 (0)