Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,19 @@ protected AbstractBodySnippet(String name, String type, PayloadSubsectionExtract
protected Map<String, Object> createModel(Operation operation) {
try {
MediaType contentType = getContentType(operation);
String language = null;
if (contentType != null) {
language = (contentType.getSubtypeSuffix() != null) ? contentType.getSubtypeSuffix()
: contentType.getSubtype();
}
byte[] content = getContent(operation);
if (this.subsectionExtractor != null) {
content = this.subsectionExtractor.extractSubsection(content, contentType);
}
Charset charset = extractCharset(contentType);
String body = (charset != null) ? new String(content, charset) : new String(content);
Map<String, Object> model = new HashMap<>();
model.put("language", language);
model.put("body", body);
return model;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[source,options="nowrap"]
[source{{#language}},{{language}}{{/language}},options="nowrap"]
----
{{body}}
----
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[source,options="nowrap"]
[source{{#language}},{{language}}{{/language}},options="nowrap"]
----
{{body}}
----
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```
```{{#language}}{{language}}{{/language}}
{{body}}
```
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```
```{{#language}}{{language}}{{/language}}
{{body}}
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.junit.Test;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.restdocs.AbstractSnippetTests;
import org.springframework.restdocs.templates.TemplateEngine;
import org.springframework.restdocs.templates.TemplateFormat;
Expand Down Expand Up @@ -58,6 +60,20 @@ public void requestWithNoBody() throws IOException {
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock(null, "nowrap").withContent(""));
}

@Test
public void requestWithMediaTypeJson() throws IOException {
requestBody().document(this.operationBuilder.request("http://localhost")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build());
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("json", "nowrap").withContent(""));
}

@Test
public void requestWithMediaTypeXml() throws IOException {
requestBody().document(this.operationBuilder.request("http://localhost")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build());
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("xml", "nowrap").withContent(""));
}

@Test
public void subsectionOfRequestBody() throws IOException {
requestBody(beneathPath("a.b")).document(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.junit.Test;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.restdocs.AbstractSnippetTests;
import org.springframework.restdocs.templates.TemplateEngine;
import org.springframework.restdocs.templates.TemplateFormat;
Expand Down Expand Up @@ -58,6 +60,20 @@ public void responseWithNoBody() throws IOException {
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock(null, "nowrap").withContent(""));
}

@Test
public void responseWithMediaTypeJson() throws IOException {
new ResponseBodySnippet().document(this.operationBuilder.response()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build());
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("json", "nowrap").withContent(""));
}

@Test
public void responseWithMediaTypeXml() throws IOException {
new ResponseBodySnippet().document(this.operationBuilder.response()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build());
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("xml", "nowrap").withContent(""));
}

@Test
public void subsectionOfResponseBody() throws IOException {
responseBody(beneathPath("a.b"))
Expand Down