Stop field snippets requiring a type for missing, optional, ignored field

Missing optional fields require a type to be explicitly provided as
it cannot be inferred from the payload. Ignored fields are not
included in the documentation, yet, previously, a field that was
missing, optional, and ignored would still require a type to be
provided otherwise the test would fail. This was pointless as the
field wasn't going to appear in the documentation.

This commit updates the fields snippets so that a type is no longer
required for a field that is missing, optional and ignored.

Closes gh-289
This commit is contained in:
Andy Wilkinson
2016-08-19 19:30:22 +01:00
parent ec3a05926e
commit 9ba0a9b6be
3 changed files with 40 additions and 9 deletions

View File

@@ -100,15 +100,17 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet {
validateFieldDocumentation(contentHandler);
for (FieldDescriptor descriptor : this.fieldDescriptors) {
try {
descriptor.type(contentHandler.determineFieldType(descriptor));
}
catch (FieldDoesNotExistException ex) {
String message = "Cannot determine the type of the field '"
+ descriptor.getPath() + "' as it is not present in the "
+ "payload. Please provide a type using "
+ "FieldDescriptor.type(Object type).";
throw new FieldTypeRequiredException(message);
if (!descriptor.isIgnored()) {
try {
descriptor.type(contentHandler.determineFieldType(descriptor));
}
catch (FieldDoesNotExistException ex) {
String message = "Cannot determine the type of the field '"
+ descriptor.getPath() + "' as it is not present in the "
+ "payload. Please provide a type using "
+ "FieldDescriptor.type(Object type).";
throw new FieldTypeRequiredException(message);
}
}
}

View File

@@ -118,6 +118,21 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
.request("http://localhost").content("{}").build());
}
@Test
public void missingIgnoredOptionalRequestFieldDoesNotRequireAType()
throws IOException {
this.snippet
.expectRequestFields(
"missing-ignored-optional-request-field-does-not-require-a-type")
.withContents(tableWithHeader("Path", "Type", "Description"));
new RequestFieldsSnippet(Arrays
.asList(fieldWithPath("a.b").description("one").ignored().optional()))
.document(operationBuilder(
"missing-ignored-optional-request-field-does-not-require-a-type")
.request("http://localhost").content("{}")
.build());
}
@Test
public void presentOptionalRequestField() throws IOException {
this.snippet.expectRequestFields("present-optional-request-field")

View File

@@ -147,6 +147,20 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
.response().content("{}").build());
}
@Test
public void missingIgnoredOptionalResponseFieldDoesNotRequireAType()
throws IOException {
this.snippet
.expectResponseFields(
"missing-ignored-optional-response-field-does-not-require-a-type")
.withContents(tableWithHeader("Path", "Type", "Description"));
new ResponseFieldsSnippet(Arrays
.asList(fieldWithPath("a.b").description("one").ignored().optional()))
.document(operationBuilder(
"missing-ignored-optional-response-field-does-not-require-a-type")
.response().content("{}").build());
}
@Test
public void presentOptionalResponseField() throws IOException {
this.snippet.expectResponseFields("present-optional-response-field")