Skip to content

Commit aa9cef9

Browse files
committed
Polish "Specify language derived from Content-Type in body snippets"
See gh-797
1 parent 9a44205 commit aa9cef9

File tree

6 files changed

+81
-15
lines changed

6 files changed

+81
-15
lines changed

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
* document a RESTful resource's request or response body.
3232
*
3333
* @author Andy Wilkinson
34+
* @author Achim Grimm
3435
*/
3536
public abstract class AbstractBodySnippet extends TemplatedSnippet {
3637

@@ -73,11 +74,7 @@ protected AbstractBodySnippet(String name, String type, PayloadSubsectionExtract
7374
protected Map<String, Object> createModel(Operation operation) {
7475
try {
7576
MediaType contentType = getContentType(operation);
76-
String language = null;
77-
if (contentType != null) {
78-
language = (contentType.getSubtypeSuffix() != null) ? contentType.getSubtypeSuffix()
79-
: contentType.getSubtype();
80-
}
77+
String language = determineLanguage(contentType);
8178
byte[] content = getContent(operation);
8279
if (this.subsectionExtractor != null) {
8380
content = this.subsectionExtractor.extractSubsection(content, contentType);
@@ -94,6 +91,13 @@ protected Map<String, Object> createModel(Operation operation) {
9491
}
9592
}
9693

94+
private String determineLanguage(MediaType contentType) {
95+
if (contentType == null) {
96+
return null;
97+
}
98+
return (contentType.getSubtypeSuffix() != null) ? contentType.getSubtypeSuffix() : contentType.getSubtype();
99+
}
100+
97101
private Charset extractCharset(MediaType contentType) {
98102
if (contentType == null) {
99103
return null;
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
```

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

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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;
@@ -61,6 +63,38 @@ public void requestPartWithNoBody() throws IOException {
6163
.is(codeBlock(null, "nowrap").withContent(""));
6264
}
6365

66+
@Test
67+
public void requestPartWithJsonMediaType() throws IOException {
68+
requestPartBody("one").document(this.operationBuilder.request("http://localhost").part("one", "".getBytes())
69+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build());
70+
assertThat(this.generatedSnippets.snippet("request-part-one-body"))
71+
.is(codeBlock("json", "nowrap").withContent(""));
72+
}
73+
74+
@Test
75+
public void requestPartWithJsonSubtypeMediaType() throws IOException {
76+
requestPartBody("one").document(this.operationBuilder.request("http://localhost").part("one", "".getBytes())
77+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PROBLEM_JSON_VALUE).build());
78+
assertThat(this.generatedSnippets.snippet("request-part-one-body"))
79+
.is(codeBlock("json", "nowrap").withContent(""));
80+
}
81+
82+
@Test
83+
public void requestPartWithXmlMediaType() throws IOException {
84+
requestPartBody("one").document(this.operationBuilder.request("http://localhost").part("one", "".getBytes())
85+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
86+
assertThat(this.generatedSnippets.snippet("request-part-one-body"))
87+
.is(codeBlock("xml", "nowrap").withContent(""));
88+
}
89+
90+
@Test
91+
public void requestPartWithXmlSubtypeMediaType() throws IOException {
92+
requestPartBody("one").document(this.operationBuilder.request("http://localhost").part("one", "".getBytes())
93+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build());
94+
assertThat(this.generatedSnippets.snippet("request-part-one-body"))
95+
.is(codeBlock("xml", "nowrap").withContent(""));
96+
}
97+
6498
@Test
6599
public void subsectionOfRequestPartBody() throws IOException {
66100
requestPartBody("one", beneathPath("a.b")).document(this.operationBuilder.request("http://localhost")

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,14 +61,28 @@ public void requestWithNoBody() throws IOException {
6161
}
6262

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

7070
@Test
71-
public void requestWithMediaTypeXml() throws IOException {
71+
public void requestWithJsonSubtypeMediaType() throws IOException {
72+
requestBody().document(this.operationBuilder.request("http://localhost")
73+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PROBLEM_JSON_VALUE).build());
74+
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("json", "nowrap").withContent(""));
75+
}
76+
77+
@Test
78+
public void requestWithXmlMediaType() throws IOException {
79+
requestBody().document(this.operationBuilder.request("http://localhost")
80+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
81+
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("xml", "nowrap").withContent(""));
82+
}
83+
84+
@Test
85+
public void requestWithXmlSubtypeMediaType() throws IOException {
7286
requestBody().document(this.operationBuilder.request("http://localhost")
7387
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build());
7488
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("xml", "nowrap").withContent(""));

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,14 +61,28 @@ public void responseWithNoBody() throws IOException {
6161
}
6262

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

7070
@Test
71-
public void responseWithMediaTypeXml() throws IOException {
71+
public void responseWithJsonSubtypeMediaType() throws IOException {
72+
new ResponseBodySnippet().document(this.operationBuilder.response()
73+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PROBLEM_JSON_VALUE).build());
74+
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("json", "nowrap").withContent(""));
75+
}
76+
77+
@Test
78+
public void responseWithXmlMediaType() throws IOException {
79+
new ResponseBodySnippet().document(this.operationBuilder.response()
80+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
81+
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("xml", "nowrap").withContent(""));
82+
}
83+
84+
@Test
85+
public void responseWithXmlSubtypeMediaType() throws IOException {
7286
new ResponseBodySnippet().document(this.operationBuilder.response()
7387
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build());
7488
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("xml", "nowrap").withContent(""));

0 commit comments

Comments
 (0)