Make it easier to document common portions of req and resp payloads
This commit adds a new andWithPrefix(String, FieldDescriptor[]) method to both RequestFieldsSnippet and ResponseFieldsSnippet. It can be used to add descriptors to an existing snippet, applying the given prefix to the additional descriptors as it does so. This allows the descriptors for a portion of a payload to be created once and then reused, irrespective of where in the payload the portion appears. Closes gh-221
This commit is contained in:
@@ -176,4 +176,19 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
|
||||
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefixedAdditionalDescriptors() throws IOException {
|
||||
this.snippet.expectRequestFields("prefixed-additional-descriptors")
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("a", "Object", "one").row("a.b", "Number", "two")
|
||||
.row("a.c", "String", "three"));
|
||||
|
||||
PayloadDocumentation.requestFields(fieldWithPath("a").description("one"))
|
||||
.andWithPrefix("a.", fieldWithPath("b").description("two"),
|
||||
fieldWithPath("c").description("three"))
|
||||
.document(operationBuilder("prefixed-additional-descriptors")
|
||||
.request("http://localhost")
|
||||
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -232,4 +232,18 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void prefixedAdditionalDescriptors() throws IOException {
|
||||
this.snippet.expectResponseFields("prefixed-additional-descriptors")
|
||||
.withContents(tableWithHeader("Path", "Type", "Description")
|
||||
.row("a", "Object", "one").row("a.b", "Number", "two")
|
||||
.row("a.c", "String", "three"));
|
||||
|
||||
PayloadDocumentation.responseFields(fieldWithPath("a").description("one"))
|
||||
.andWithPrefix("a.", fieldWithPath("b").description("two"),
|
||||
fieldWithPath("c").description("three"))
|
||||
.document(operationBuilder("prefixed-additional-descriptors").response()
|
||||
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user