diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java b/spring-restdocs/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java index 91bf624d..a8ae9d2a 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java +++ b/spring-restdocs/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java @@ -16,6 +16,7 @@ package org.springframework.restdocs.request; +import java.io.IOException; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -46,6 +47,21 @@ class PathParametersSnippet extends AbstractParametersSnippet { super("path-parameters", attributes, descriptors); } + @Override + protected Map document(MvcResult result) throws IOException { + Map model = super.document(result); + model.put("path", remoteQueryStringIfPresent(extractUrlTemplate(result))); + return model; + } + + private String remoteQueryStringIfPresent(String urlTemplate) { + int index = urlTemplate.indexOf('?'); + if (index == -1) { + return urlTemplate; + } + return urlTemplate.substring(0, index); + } + @Override protected Set extractActualParameters(MvcResult result) { String urlTemplate = extractUrlTemplate(result); diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-path-parameters.snippet b/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-path-parameters.snippet index 067e1fb8..66db3862 100644 --- a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-path-parameters.snippet +++ b/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-path-parameters.snippet @@ -1,3 +1,4 @@ +.{{path}} |=== |Parameter|Description diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java b/spring-restdocs/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java index 1ba26c2b..43ed107b 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java +++ b/spring-restdocs/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java @@ -24,6 +24,7 @@ import static org.springframework.restdocs.request.RequestDocumentation.paramete import static org.springframework.restdocs.snippet.Attributes.attributes; import static org.springframework.restdocs.snippet.Attributes.key; import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader; +import static org.springframework.restdocs.test.SnippetMatchers.tableWithTitleAndHeader; import static org.springframework.restdocs.test.StubMvcResult.result; import static org.springframework.restdocs.test.TestRequestBuilders.get; @@ -89,8 +90,8 @@ public class PathParametersSnippetTests { @Test public void pathParameters() throws IOException { this.snippet.expectPathParameters("path-parameters").withContents( - tableWithHeader("Parameter", "Description").row("a", "one").row("b", - "two")); + tableWithTitleAndHeader("/{a}/{b}", "Parameter", "Description").row("a", + "one").row("b", "two")); new PathParametersSnippet(Arrays.asList( parameterWithName("a").description("one"), parameterWithName("b") .description("two"))).document( @@ -100,6 +101,21 @@ public class PathParametersSnippetTests { new RestDocumentationContext(null)))); } + @Test + public void pathParametersWithQueryString() throws IOException { + this.snippet.expectPathParameters("path-parameters-with-query-string") + .withContents( + tableWithTitleAndHeader("/{a}/{b}", "Parameter", "Description") + .row("a", "one").row("b", "two")); + new PathParametersSnippet(Arrays.asList( + parameterWithName("a").description("one"), parameterWithName("b") + .description("two"))).document( + "path-parameters-with-query-string", + result(get("/{a}/{b}?foo=bar", "alpha", "banana").requestAttr( + RestDocumentationContext.class.getName(), + new RestDocumentationContext(null)))); + } + @Test public void pathParametersWithCustomDescriptorAttributes() throws IOException { this.snippet.expectPathParameters( diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/test/SnippetMatchers.java b/spring-restdocs/src/test/java/org/springframework/restdocs/test/SnippetMatchers.java index 46eecfcd..b174cf40 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/test/SnippetMatchers.java +++ b/spring-restdocs/src/test/java/org/springframework/restdocs/test/SnippetMatchers.java @@ -44,8 +44,13 @@ public class SnippetMatchers { return new SnippetMatcher(); } + public static AsciidoctorTableMatcher tableWithTitleAndHeader(String title, + String... headers) { + return new AsciidoctorTableMatcher(title, headers); + } + public static AsciidoctorTableMatcher tableWithHeader(String... headers) { - return new AsciidoctorTableMatcher(headers); + return new AsciidoctorTableMatcher(null, headers); } public static HttpRequestMatcher httpRequest(RequestMethod method, String uri) { @@ -166,7 +171,10 @@ public class SnippetMatchers { public static class AsciidoctorTableMatcher extends AbstractSnippetContentMatcher { - private AsciidoctorTableMatcher(String... columns) { + private AsciidoctorTableMatcher(String title, String... columns) { + if (StringUtils.hasText(title)) { + this.addLine("." + title); + } this.addLine("|==="); String header = "|" + StringUtils