Skip to content

Commit 15980d7

Browse files
committed
Merge pull request #797 from achimgrimm
* gh-797: Polish "Specify language derived from Content-Type in body snippets" Specify language derived from Content-Type in body snippets Closes gh-797
2 parents bdba92e + aa9cef9 commit 15980d7

File tree

10 files changed

+114
-10
lines changed

10 files changed

+114
-10
lines changed

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

+11-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.
@@ -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,13 +74,15 @@ protected AbstractBodySnippet(String name, String type, PayloadSubsectionExtract
7374
protected Map<String, Object> createModel(Operation operation) {
7475
try {
7576
MediaType contentType = getContentType(operation);
77+
String language = determineLanguage(contentType);
7678
byte[] content = getContent(operation);
7779
if (this.subsectionExtractor != null) {
7880
content = this.subsectionExtractor.extractSubsection(content, contentType);
7981
}
8082
Charset charset = extractCharset(contentType);
8183
String body = (charset != null) ? new String(content, charset) : new String(content);
8284
Map<String, Object> model = new HashMap<>();
85+
model.put("language", language);
8386
model.put("body", body);
8487
return model;
8588
}
@@ -88,6 +91,13 @@ protected Map<String, Object> createModel(Operation operation) {
8891
}
8992
}
9093

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+
91101
private Charset extractCharset(MediaType contentType) {
92102
if (contentType == null) {
93103
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,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,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
```
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

+31-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;
@@ -58,6 +60,34 @@ public void requestWithNoBody() throws IOException {
5860
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock(null, "nowrap").withContent(""));
5961
}
6062

63+
@Test
64+
public void requestWithJsonMediaType() throws IOException {
65+
requestBody().document(this.operationBuilder.request("http://localhost")
66+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build());
67+
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("json", "nowrap").withContent(""));
68+
}
69+
70+
@Test
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 {
86+
requestBody().document(this.operationBuilder.request("http://localhost")
87+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build());
88+
assertThat(this.generatedSnippets.snippet("request-body")).is(codeBlock("xml", "nowrap").withContent(""));
89+
}
90+
6191
@Test
6292
public void subsectionOfRequestBody() throws IOException {
6393
requestBody(beneathPath("a.b")).document(

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

+31-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;
@@ -58,6 +60,34 @@ public void responseWithNoBody() throws IOException {
5860
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock(null, "nowrap").withContent(""));
5961
}
6062

63+
@Test
64+
public void responseWithJsonMediaType() throws IOException {
65+
new ResponseBodySnippet().document(this.operationBuilder.response()
66+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build());
67+
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("json", "nowrap").withContent(""));
68+
}
69+
70+
@Test
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 {
86+
new ResponseBodySnippet().document(this.operationBuilder.response()
87+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML_VALUE).build());
88+
assertThat(this.generatedSnippets.snippet("response-body")).is(codeBlock("xml", "nowrap").withContent(""));
89+
}
90+
6191
@Test
6292
public void subsectionOfResponseBody() throws IOException {
6393
responseBody(beneathPath("a.b"))

0 commit comments

Comments
 (0)