Add support for reusing a snippet to document common elements
This commit updates all of the Snippet implementations that take one or more descriptors to provide an and method that can be used to create a new Snippet that has additional descriptors. Closes gh-168
This commit is contained in:
@@ -143,4 +143,27 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
.header("Accept", "*/*").build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectRequestHeaders("additional-descriptors")
|
||||
.withContents(tableWithHeader("Name", "Description").row("X-Test", "one")
|
||||
.row("Accept", "two").row("Accept-Encoding", "three")
|
||||
.row("Accept-Language", "four").row("Cache-Control", "five")
|
||||
.row("Connection", "six"));
|
||||
HeaderDocumentation
|
||||
.requestHeaders(headerWithName("X-Test").description("one"),
|
||||
headerWithName("Accept").description("two"),
|
||||
headerWithName("Accept-Encoding").description("three"),
|
||||
headerWithName("Accept-Language").description("four"))
|
||||
.and(headerWithName("Cache-Control").description("five"),
|
||||
headerWithName("Connection").description("six"))
|
||||
.document(operationBuilder("additional-descriptors")
|
||||
.request("http://localhost").header("X-Test", "test")
|
||||
.header("Accept", "*/*")
|
||||
.header("Accept-Encoding", "gzip, deflate")
|
||||
.header("Accept-Language", "en-US,en;q=0.5")
|
||||
.header("Cache-Control", "max-age=0")
|
||||
.header("Connection", "keep-alive").build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -133,4 +133,25 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
|
||||
.header("Etag", "lskjadldj3ii32l2ij23").build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectResponseHeaders("additional-descriptors")
|
||||
.withContents(tableWithHeader("Name", "Description").row("X-Test", "one")
|
||||
.row("Content-Type", "two").row("Etag", "three")
|
||||
.row("Cache-Control", "five").row("Vary", "six"));
|
||||
HeaderDocumentation
|
||||
.responseHeaders(headerWithName("X-Test").description("one"),
|
||||
headerWithName("Content-Type").description("two"),
|
||||
headerWithName("Etag").description("three"))
|
||||
.and(headerWithName("Cache-Control").description("five"),
|
||||
headerWithName("Vary")
|
||||
.description("six"))
|
||||
.document(operationBuilder("additional-descriptors").response()
|
||||
.header("X-Test", "test")
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Etag", "lskjadldj3ii32l2ij23")
|
||||
.header("Cache-Control", "max-age=0").header("Vary", "User-Agent")
|
||||
.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -132,4 +132,16 @@ public class LinksSnippetTests extends AbstractSnippetTests {
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectLinks("additional-descriptors")
|
||||
.withContents(tableWithHeader("Relation", "Description").row("a", "one")
|
||||
.row("b", "two"));
|
||||
HypermediaDocumentation
|
||||
.links(new StubLinkExtractor().withLinks(new Link("a", "alpha"),
|
||||
new Link("b", "bravo")),
|
||||
new LinkDescriptor("a").description("one"))
|
||||
.and(new LinkDescriptor("b").description("two"))
|
||||
.document(operationBuilder("additional-descriptors").build());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,4 +160,20 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectRequestFields("additional-descriptors")
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("a.b", "Number", "one").row("a.c", "String", "two")
|
||||
.row("a", "Object", "three"));
|
||||
|
||||
PayloadDocumentation
|
||||
.requestFields(fieldWithPath("a.b").description("one"),
|
||||
fieldWithPath("a.c").description("two"))
|
||||
.and(fieldWithPath("a").description("three"))
|
||||
.document(operationBuilder("additional-descriptors")
|
||||
.request("http://localhost")
|
||||
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -211,4 +211,25 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectResponseFields("additional-descriptors")
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("id", "Number", "one").row("date", "String", "two")
|
||||
.row("assets", "Array", "three").row("assets[]", "Object", "four")
|
||||
.row("assets[].id", "Number", "five")
|
||||
.row("assets[].name", "String", "six"));
|
||||
PayloadDocumentation
|
||||
.responseFields(fieldWithPath("id").description("one"),
|
||||
fieldWithPath("date").description("two"),
|
||||
fieldWithPath("assets").description("three"))
|
||||
.and(fieldWithPath("assets[]").description("four"),
|
||||
fieldWithPath("assets[].id").description("five"),
|
||||
fieldWithPath("assets[].name").description("six"))
|
||||
.document(operationBuilder("additional-descriptors").response()
|
||||
.content("{\"id\": 67,\"date\": \"2015-01-20\",\"assets\":"
|
||||
+ " [{\"id\":356,\"name\": \"sample\"}]}")
|
||||
.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -133,6 +133,19 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectPathParameters("additional-descriptors").withContents(
|
||||
tableWithTitleAndHeader(getTitle(), "Parameter", "Description")
|
||||
.row("a", "one").row("b", "two"));
|
||||
RequestDocumentation.pathParameters(parameterWithName("a").description("one"))
|
||||
.and(parameterWithName("b").description("two"))
|
||||
.document(operationBuilder("additional-descriptors")
|
||||
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
|
||||
"/{a}/{b}")
|
||||
.build());
|
||||
}
|
||||
|
||||
private String getTitle() {
|
||||
return this.templateFormat == TemplateFormats.asciidoctor() ? "/{a}/{b}"
|
||||
: "`/{a}/{b}`";
|
||||
|
||||
@@ -128,4 +128,16 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additionalDescriptors() throws IOException {
|
||||
this.snippet.expectRequestParameters("additional-descriptors")
|
||||
.withContents(tableWithHeader("Parameter", "Description").row("a", "one")
|
||||
.row("b", "two"));
|
||||
RequestDocumentation.requestParameters(parameterWithName("a").description("one"))
|
||||
.and(parameterWithName("b").description("two"))
|
||||
.document(operationBuilder("additional-descriptors")
|
||||
.request("http://localhost").param("a", "bravo")
|
||||
.param("b", "bravo").build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user