diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java index fcc67c36..393dd8fd 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java @@ -43,6 +43,8 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet { private final boolean ignoreUndocumentedFields; + private final String type; + /** * Creates a new {@code AbstractFieldsSnippet} that will produce a snippet named * {@code -fields}. The fields will be documented using the given @@ -88,6 +90,7 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet { } this.fieldDescriptors = descriptors; this.ignoreUndocumentedFields = ignoreUndocumentedFields; + this.type = type; } @Override @@ -116,13 +119,19 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet { private ContentHandler getContentHandler(Operation operation) { MediaType contentType = getContentType(operation); ContentHandler contentHandler; + try { + byte[] content = getContent(operation); + if (content.length == 0) { + throw new SnippetException("Cannot document " + this.type + + " fields as the " + this.type + " body is empty"); + } if (contentType != null && MediaType.APPLICATION_XML.isCompatibleWith(contentType)) { - contentHandler = new XmlContentHandler(getContent(operation)); + contentHandler = new XmlContentHandler(content); } else { - contentHandler = new JsonContentHandler(getContent(operation)); + contentHandler = new JsonContentHandler(content); } } catch (IOException ex) { diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetFailureTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetFailureTests.java index eeab98cf..a6dceaf0 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetFailureTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetFailureTests.java @@ -99,6 +99,17 @@ public class RequestFieldsSnippetFailureTests { .content("{ \"a\": { \"c\": 5 }}").build()); } + @Test + public void attemptToDocumentFieldsWithNoRequestBody() throws IOException { + this.thrown.expect(SnippetException.class); + this.thrown.expectMessage( + equalTo("Cannot document request fields as the request body is empty")); + new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one"))) + .document(new OperationBuilder("no-request-body", + this.snippet.getOutputDirectory()).request("http://localhost") + .build()); + } + @Test public void undocumentedXmlRequestField() throws IOException { this.thrown.expect(SnippetException.class); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetFailureTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetFailureTests.java index 98a31d45..e3385a03 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetFailureTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetFailureTests.java @@ -50,6 +50,17 @@ public class ResponseFieldsSnippetFailureTests { @Rule public ExpectedException thrown = ExpectedException.none(); + @Test + public void attemptToDocumentFieldsWithNoResponseBody() throws IOException { + this.thrown.expect(SnippetException.class); + this.thrown.expectMessage( + equalTo("Cannot document response fields as the response body is empty")); + new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one"))) + .document(new OperationBuilder("no-response-body", + this.snippet.getOutputDirectory()).request("http://localhost") + .build()); + } + @Test public void undocumentedXmlResponseField() throws IOException { this.thrown.expect(SnippetException.class);