Improve message when fields cannot be documented due to empty body
Closes gh-278
This commit is contained in:
@@ -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 <type>-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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user