Merge branch '1.1.x'

This commit is contained in:
Andy Wilkinson
2016-08-19 19:53:06 +01:00
5 changed files with 60 additions and 13 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

@@ -116,7 +116,7 @@ public class PathParametersSnippet extends AbstractParametersSnippet {
@Override
protected Set<String> extractActualParameters(Operation operation) {
String urlTemplate = extractUrlTemplate(operation);
String urlTemplate = removeQueryStringIfPresent(extractUrlTemplate(operation));
Matcher matcher = NAMES_PATTERN.matcher(urlTemplate);
Set<String> actualParameters = new HashSet<>();
while (matcher.find()) {
@@ -129,9 +129,8 @@ public class PathParametersSnippet extends AbstractParametersSnippet {
private String extractUrlTemplate(Operation operation) {
String urlTemplate = (String) operation.getAttributes()
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE);
Assert.notNull(urlTemplate,
"urlTemplate not found. If you are using MockMvc, did you use RestDocumentationRequestBuilders to "
+ "build the request?");
Assert.notNull(urlTemplate, "urlTemplate not found. If you are using MockMvc did "
+ "you use RestDocumentationRequestBuilders to build the request?");
return urlTemplate;
}

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")

View File

@@ -130,6 +130,23 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
"/{a}/{b}?foo=bar").build());
}
@Test
public void pathParametersWithQueryStringWithParameters() throws IOException {
this.snippet
.expectPathParameters("path-parameters-with-query-string-with-parameters")
.withContents(
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
.row("`a`", "one").row("`b`", "two"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
parameterWithName("b").description("two")))
.document(operationBuilder(
"path-parameters-with-query-string-with-parameters")
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}?foo={c}")
.build());
}
@Test
public void pathParametersWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);