Skip to content

Commit

Permalink
pringMvcContract support parse params
Browse files Browse the repository at this point in the history
  • Loading branch information
puppy4c committed Apr 4, 2024
1 parent 6084609 commit ca111e1
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2023 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -87,6 +87,7 @@
* @author Darren Foong
* @author Ram Anaswara
* @author Sam Kruglov
* @author Tang Xiong
*/
public class SpringMvcContract extends Contract.BaseContract implements ResourceLoaderAware {

Expand Down Expand Up @@ -244,6 +245,9 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
// headers
parseHeaders(data, method, methodMapping);

// params
parseParams(data, method, methodMapping);

data.indexToExpander(new LinkedHashMap<>());
}

Expand Down Expand Up @@ -354,6 +358,19 @@ private void parseHeaders(MethodMetadata md, Method method, RequestMapping annot
}
}

private void parseParams(MethodMetadata data, Method method, RequestMapping methodMapping) {
String[] params = methodMapping.params();
if (params == null || params.length == 0) {
return;
}
for (String param : params) {
NameValueResolver nameValueResolver = new NameValueResolver(param);
if (!nameValueResolver.isNegated()) {
data.template().query(resolve(nameValueResolver.getName()), resolve(nameValueResolver.getValue()));
}
}
}

private Map<Class<? extends Annotation>, AnnotatedParameterProcessor> toAnnotatedArgumentProcessorMap(
List<AnnotatedParameterProcessor> processors) {
Map<Class<? extends Annotation>, AnnotatedParameterProcessor> result = new HashMap<>();
Expand Down Expand Up @@ -465,4 +482,41 @@ public Collection<String> setTemplateParameter(String name, Collection<String> r

}

private static class NameValueResolver {

private final String name;

private final String value;

private final boolean isNegated;

NameValueResolver(String expression) {
int separator = expression.indexOf('=');
if (separator == -1) {
this.isNegated = expression.startsWith("!");
this.name = (this.isNegated ? expression.substring(1) : expression);
this.value = null;
}
else {
this.isNegated = (separator > 0) && (expression.charAt(separator - 1) == '!');
this.name = (this.isNegated ? expression.substring(0, separator - 1)
: expression.substring(0, separator));
this.value = expression.substring(separator + 1);
}
}

public String getName() {
return name;
}

public String getValue() {
return value;
}

public boolean isNegated() {
return isNegated;
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2023 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -82,6 +82,7 @@
* @author Szymon Linowski
* @author Sam Kruglov
* @author Bhavya Agrawal
* @author Tang Xiong
**/

class SpringMvcContractTests {
Expand Down Expand Up @@ -462,6 +463,60 @@ void testProcessAnnotations_MapParams() throws Exception {
assertThat(data.queryMapIndex().intValue()).isEqualTo(0);
}

@Test
void testProcessAnnotations_ParseParams_SingleParam() throws Exception {
Method method = TestTemplate_ParseParams.class.getDeclaredMethod("singleParam");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().url()).isEqualTo("/test?p1=1");
assertThat(data.template().method()).isEqualTo("GET");
}

@Test
void testProcessAnnotations_ParseParams_MultipleParams() throws Exception {
Method method = TestTemplate_ParseParams.class.getDeclaredMethod("multipleParams");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().url()).isEqualTo("/test?p1=1&p2=2");
assertThat(data.template().method()).isEqualTo("GET");
}

@Test
void testProcessAnnotations_ParseParams_MixParams() throws Exception {
Method method = TestTemplate_ParseParams.class.getDeclaredMethod("mixParams");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().url()).isEqualTo("/test?p1=1&p2");
assertThat(data.template().method()).isEqualTo("GET");
}

@Test
void testProcessAnnotations_ParseParams_SingleParamWithoutValue() throws Exception {
Method method = TestTemplate_ParseParams.class.getDeclaredMethod("singleParamWithoutValue");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().url()).isEqualTo("/test?p1");
assertThat(data.template().method()).isEqualTo("GET");
}

@Test
void testProcessAnnotations_ParseParams_MultipleParamsWithoutValue() throws Exception {
Method method = TestTemplate_ParseParams.class.getDeclaredMethod("multipleParamsWithoutValue");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().url()).isEqualTo("/test?p1&p2");
assertThat(data.template().method()).isEqualTo("GET");
}

@Test
void testProcessAnnotations_ParseParams_NotEqualParams() throws Exception {
Method method = TestTemplate_ParseParams.class.getDeclaredMethod("notEqualParams");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().url()).isEqualTo("/test");
assertThat(data.template().method()).isEqualTo("GET");
}

@Test
void testProcessHeaders() throws Exception {
Method method = TestTemplate_Headers.class.getDeclaredMethod("getTest", String.class);
Expand Down Expand Up @@ -750,6 +805,28 @@ public interface TestTemplate_MapParams {

}

public interface TestTemplate_ParseParams {

@GetMapping(value = "test", params = "p1=1")
ResponseEntity<TestObject> singleParam();

@GetMapping(value = "test", params = { "p1=1", "p2=2" })
ResponseEntity<TestObject> multipleParams();

@GetMapping(value = "test", params = { "p1" })
ResponseEntity<TestObject> singleParamWithoutValue();

@GetMapping(value = "test", params = { "p1", "p2" })
ResponseEntity<TestObject> multipleParamsWithoutValue();

@GetMapping(value = "test", params = { "p1=1", "p2" })
ResponseEntity<TestObject> mixParams();

@GetMapping(value = "test", params = { "p1!=1" })
ResponseEntity<TestObject> notEqualParams();

}

public interface TestTemplate_HeaderMap {

@GetMapping("/headerMap")
Expand Down

0 comments on commit ca111e1

Please sign in to comment.