diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractBodySnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractBodySnippet.java index f9895e897..dfb1d963b 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractBodySnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractBodySnippet.java @@ -73,6 +73,11 @@ protected AbstractBodySnippet(String name, String type, PayloadSubsectionExtract protected Map 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); @@ -80,6 +85,7 @@ protected Map createModel(Operation operation) { Charset charset = extractCharset(contentType); String body = (charset != null) ? new String(content, charset) : new String(content); Map model = new HashMap<>(); + model.put("language", language); model.put("body", body); return model; } diff --git a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-request-body.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-request-body.snippet index e6cb8e9bd..00da2d085 100644 --- a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-request-body.snippet +++ b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-request-body.snippet @@ -1,4 +1,4 @@ -[source,options="nowrap"] +[source{{#language}},{{language}}{{/language}},options="nowrap"] ---- {{body}} ---- \ No newline at end of file diff --git a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-response-body.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-response-body.snippet index e6cb8e9bd..00da2d085 100644 --- a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-response-body.snippet +++ b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-response-body.snippet @@ -1,4 +1,4 @@ -[source,options="nowrap"] +[source{{#language}},{{language}}{{/language}},options="nowrap"] ---- {{body}} ---- \ No newline at end of file diff --git a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-request-body.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-request-body.snippet index b897d4095..6abf3f7e8 100644 --- a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-request-body.snippet +++ b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-request-body.snippet @@ -1,3 +1,3 @@ -``` +```{{#language}}{{language}}{{/language}} {{body}} ``` \ No newline at end of file diff --git a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-response-body.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-response-body.snippet index b897d4095..6abf3f7e8 100644 --- a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-response-body.snippet +++ b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-response-body.snippet @@ -1,3 +1,3 @@ -``` +```{{#language}}{{language}}{{/language}} {{body}} ``` \ No newline at end of file diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestBodySnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestBodySnippetTests.java index 87e35e552..07bc62423 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestBodySnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestBodySnippetTests.java @@ -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; @@ -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( diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseBodySnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseBodySnippetTests.java index 078fe097c..8955a004f 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseBodySnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseBodySnippetTests.java @@ -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; @@ -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"))