Skip to content

Commit 2421267

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

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,18 @@ 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() : contentType.getSubtype();
79+
}
7680
byte[] content = getContent(operation);
7781
if (this.subsectionExtractor != null) {
7882
content = this.subsectionExtractor.extractSubsection(content, contentType);
7983
}
8084
Charset charset = extractCharset(contentType);
8185
String body = (charset != null) ? new String(content, charset) : new String(content);
8286
Map<String, Object> model = new HashMap<>();
87+
model.put("language", language);
8388
model.put("body", body);
8489
return model;
8590
}

spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-request-body.snippet

-4
This file was deleted.
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

+20
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,24 @@ 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)
67+
.build());
68+
assertThat(this.generatedSnippets.snippet("request-body"))
69+
.is(codeBlock("json", "nowrap").withContent(""));
70+
}
71+
72+
@Test
73+
public void requestWithMediaTypeXml() throws IOException {
74+
requestBody().document(this.operationBuilder.request("http://localhost")
75+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE)
76+
.build());
77+
assertThat(this.generatedSnippets.snippet("request-body"))
78+
.is(codeBlock("xml", "nowrap").withContent(""));
79+
}
80+
6181
@Test
6282
public void subsectionOfRequestBody() throws IOException {
6383
requestBody(beneathPath("a.b")).document(

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

+20-2
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;
@@ -29,8 +31,8 @@
2931
import static org.assertj.core.api.Assertions.assertThat;
3032
import static org.mockito.BDDMockito.given;
3133
import static org.mockito.Mockito.mock;
32-
import static org.springframework.restdocs.payload.PayloadDocumentation.beneathPath;
33-
import static org.springframework.restdocs.payload.PayloadDocumentation.responseBody;
34+
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
35+
import static org.springframework.restdocs.payload.PayloadDocumentation.requestBody;
3436
import static org.springframework.restdocs.snippet.Attributes.attributes;
3537
import static org.springframework.restdocs.snippet.Attributes.key;
3638

@@ -58,6 +60,22 @@ 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)
67+
.build());
68+
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("json", "nowrap").withContent(""));
69+
}
70+
71+
@Test
72+
public void responseWithMediaTypeXml() throws IOException {
73+
new ResponseBodySnippet().document(this.operationBuilder.response()
74+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE)
75+
.build());
76+
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("xml", "nowrap").withContent(""));
77+
}
78+
6179
@Test
6280
public void subsectionOfResponseBody() throws IOException {
6381
responseBody(beneathPath("a.b"))

0 commit comments

Comments
 (0)