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 f9895e89..3ccd5688 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import org.springframework.restdocs.snippet.TemplatedSnippet; * document a RESTful resource's request or response body. * * @author Andy Wilkinson + * @author Achim Grimm */ public abstract class AbstractBodySnippet extends TemplatedSnippet { @@ -73,6 +74,7 @@ public abstract class AbstractBodySnippet extends TemplatedSnippet { protected Map createModel(Operation operation) { try { MediaType contentType = getContentType(operation); + String language = determineLanguage(contentType); byte[] content = getContent(operation); if (this.subsectionExtractor != null) { content = this.subsectionExtractor.extractSubsection(content, contentType); @@ -80,6 +82,7 @@ public abstract class AbstractBodySnippet extends TemplatedSnippet { 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; } @@ -88,6 +91,13 @@ public abstract class AbstractBodySnippet extends TemplatedSnippet { } } + private String determineLanguage(MediaType contentType) { + if (contentType == null) { + return null; + } + return (contentType.getSubtypeSuffix() != null) ? contentType.getSubtypeSuffix() : contentType.getSubtype(); + } + private Charset extractCharset(MediaType contentType) { if (contentType == null) { return null; 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 e6cb8e9b..00da2d08 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-request-part-body.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-request-part-body.snippet index e6cb8e9b..00da2d08 100644 --- a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-request-part-body.snippet +++ b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/default-request-part-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 e6cb8e9b..00da2d08 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 b897d409..6abf3f7e 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-request-part-body.snippet b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-request-part-body.snippet index b897d409..6abf3f7e 100644 --- a/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-request-part-body.snippet +++ b/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/default-request-part-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 b897d409..6abf3f7e 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/RequestBodyPartSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestBodyPartSnippetTests.java index 85a686d9..b88288ee 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestBodyPartSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestBodyPartSnippetTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ import java.io.IOException; 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; @@ -61,6 +63,38 @@ public class RequestBodyPartSnippetTests extends AbstractSnippetTests { .is(codeBlock(null, "nowrap").withContent("")); } + @Test + public void requestPartWithJsonMediaType() throws IOException { + requestPartBody("one").document(this.operationBuilder.request("http://localhost").part("one", "".getBytes()) + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build()); + assertThat(this.generatedSnippets.snippet("request-part-one-body")) + .is(codeBlock("json", "nowrap").withContent("")); + } + + @Test + public void requestPartWithJsonSubtypeMediaType() throws IOException { + requestPartBody("one").document(this.operationBuilder.request("http://localhost").part("one", "".getBytes()) + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PROBLEM_JSON_VALUE).build()); + assertThat(this.generatedSnippets.snippet("request-part-one-body")) + .is(codeBlock("json", "nowrap").withContent("")); + } + + @Test + public void requestPartWithXmlMediaType() throws IOException { + requestPartBody("one").document(this.operationBuilder.request("http://localhost").part("one", "".getBytes()) + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()); + assertThat(this.generatedSnippets.snippet("request-part-one-body")) + .is(codeBlock("xml", "nowrap").withContent("")); + } + + @Test + public void requestPartWithXmlSubtypeMediaType() throws IOException { + requestPartBody("one").document(this.operationBuilder.request("http://localhost").part("one", "".getBytes()) + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build()); + assertThat(this.generatedSnippets.snippet("request-part-one-body")) + .is(codeBlock("xml", "nowrap").withContent("")); + } + @Test public void subsectionOfRequestPartBody() throws IOException { requestPartBody("one", beneathPath("a.b")).document(this.operationBuilder.request("http://localhost") 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 87e35e55..622bef5e 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ import java.io.IOException; 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,34 @@ public class RequestBodySnippetTests extends AbstractSnippetTests { assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock(null, "nowrap").withContent("")); } + @Test + public void requestWithJsonMediaType() 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 requestWithJsonSubtypeMediaType() throws IOException { + requestBody().document(this.operationBuilder.request("http://localhost") + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PROBLEM_JSON_VALUE).build()); + assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("json", "nowrap").withContent("")); + } + + @Test + public void requestWithXmlMediaType() throws IOException { + requestBody().document(this.operationBuilder.request("http://localhost") + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()); + assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("xml", "nowrap").withContent("")); + } + + @Test + public void requestWithXmlSubtypeMediaType() 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 078fe097..1b5cd46f 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ import java.io.IOException; 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,34 @@ public class ResponseBodySnippetTests extends AbstractSnippetTests { assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock(null, "nowrap").withContent("")); } + @Test + public void responseWithJsonMediaType() 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 responseWithJsonSubtypeMediaType() throws IOException { + new ResponseBodySnippet().document(this.operationBuilder.response() + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PROBLEM_JSON_VALUE).build()); + assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("json", "nowrap").withContent("")); + } + + @Test + public void responseWithXmlMediaType() throws IOException { + new ResponseBodySnippet().document(this.operationBuilder.response() + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()); + assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("xml", "nowrap").withContent("")); + } + + @Test + public void responseWithXmlSubtypeMediaType() 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"))