Support regex expression for @PathVariable (#577)

Fix gh-576
This commit is contained in:
Yanming Zhou
2021-07-27 17:54:47 +08:00
committed by GitHub
parent 3322f95fe4
commit a3704f0614
3 changed files with 19 additions and 1 deletions

View File

@@ -52,6 +52,9 @@ public interface StoreClient {
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
Store update(@PathVariable("storeId") Long storeId, Store store);
@RequestMapping(method = RequestMethod.DELETE, value = "/stores/{storeId:\\d+}")
void delete(@PathVariable Long storeId);
}
----

View File

@@ -34,6 +34,7 @@ import static feign.Util.emptyToNull;
*
* @author Jakub Narloch
* @author Abhijit Sarkar
* @author Yanming Zhou
* @see AnnotatedParameterProcessor
*/
public class PathVariableParameterProcessor implements AnnotatedParameterProcessor {
@@ -54,7 +55,8 @@ public class PathVariableParameterProcessor implements AnnotatedParameterProcess
MethodMetadata data = context.getMethodMetadata();
String varName = '{' + name + '}';
if (!data.template().url().contains(varName) && !searchMapValues(data.template().queries(), varName)
String varNameRegex = ".*\\{" + name + "(:[^}]+)?\\}.*";
if (!data.template().url().matches(varNameRegex) && !searchMapValues(data.template().queries(), varName)
&& !searchMapValues(data.template().headers(), varName)) {
data.formParams().add(name);
}

View File

@@ -130,6 +130,16 @@ public class SpringMvcContractTests {
assertThat(data.template().decodeSlash()).isTrue();
}
@Test
public void testProcessAnnotationOnMethod_Simple_RegexPathVariable() throws Exception {
Method method = TestTemplate_Simple.class.getDeclaredMethod("getTestWithDigitalId", String.class);
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);
assertThat(data.template().url()).isEqualTo("/test/{id:\\d+}");
assertThat(data.template().method()).isEqualTo("GET");
assertThat(data.formParams()).isEmpty();
}
@Test
public void testProcessAnnotationOnMethod_Simple_SlashEncoded() throws Exception {
contract = new SpringMvcContract(Collections.emptyList(), getConversionService(), false);
@@ -588,6 +598,9 @@ public class SpringMvcContractTests {
@RequestMapping(value = "/test/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<TestObject> getTest(@PathVariable("id") String id);
@GetMapping("/test/{id:\\d+}")
ResponseEntity<TestObject> getTestWithDigitalId(@PathVariable("id") String id);
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
TestObject getTest();