Skip to content

Commit 86c2310

Browse files
committed
Improve message when fields cannot be documented due to empty body
Closes gh-278
1 parent 6131730 commit 86c2310

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet {
4343

4444
private final boolean ignoreUndocumentedFields;
4545

46+
private final String type;
47+
4648
/**
4749
* Creates a new {@code AbstractFieldsSnippet} that will produce a snippet named
4850
* {@code <type>-fields}. The fields will be documented using the given
@@ -88,6 +90,7 @@ protected AbstractFieldsSnippet(String type, List<FieldDescriptor> descriptors,
8890
}
8991
this.fieldDescriptors = descriptors;
9092
this.ignoreUndocumentedFields = ignoreUndocumentedFields;
93+
this.type = type;
9194
}
9295

9396
@Override
@@ -116,13 +119,19 @@ protected Map<String, Object> createModel(Operation operation) {
116119
private ContentHandler getContentHandler(Operation operation) {
117120
MediaType contentType = getContentType(operation);
118121
ContentHandler contentHandler;
122+
119123
try {
124+
byte[] content = getContent(operation);
125+
if (content.length == 0) {
126+
throw new SnippetException("Cannot document " + this.type
127+
+ " fields as the " + this.type + " body is empty");
128+
}
120129
if (contentType != null
121130
&& MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
122-
contentHandler = new XmlContentHandler(getContent(operation));
131+
contentHandler = new XmlContentHandler(content);
123132
}
124133
else {
125-
contentHandler = new JsonContentHandler(getContent(operation));
134+
contentHandler = new JsonContentHandler(content);
126135
}
127136
}
128137
catch (IOException ex) {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ public void undocumentedRequestFieldAndMissingRequestField() throws IOException
9999
.content("{ \"a\": { \"c\": 5 }}").build());
100100
}
101101

102+
@Test
103+
public void attemptToDocumentFieldsWithNoRequestBody() throws IOException {
104+
this.thrown.expect(SnippetException.class);
105+
this.thrown.expectMessage(
106+
equalTo("Cannot document request fields as the request body is empty"));
107+
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
108+
.document(new OperationBuilder("no-request-body",
109+
this.snippet.getOutputDirectory()).request("http://localhost")
110+
.build());
111+
}
112+
102113
@Test
103114
public void undocumentedXmlRequestField() throws IOException {
104115
this.thrown.expect(SnippetException.class);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ public class ResponseFieldsSnippetFailureTests {
5050
@Rule
5151
public ExpectedException thrown = ExpectedException.none();
5252

53+
@Test
54+
public void attemptToDocumentFieldsWithNoResponseBody() throws IOException {
55+
this.thrown.expect(SnippetException.class);
56+
this.thrown.expectMessage(
57+
equalTo("Cannot document response fields as the response body is empty"));
58+
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
59+
.document(new OperationBuilder("no-response-body",
60+
this.snippet.getOutputDirectory()).request("http://localhost")
61+
.build());
62+
}
63+
5364
@Test
5465
public void undocumentedXmlResponseField() throws IOException {
5566
this.thrown.expect(SnippetException.class);

0 commit comments

Comments
 (0)