Skip to content

Commit 68f7984

Browse files
authored
Overriding query parameter name (#1184)
* Add possibility to override request parameter name in objects by @param Fixes #1183
1 parent 2cc907c commit 68f7984

File tree

5 files changed

+82
-14
lines changed

5 files changed

+82
-14
lines changed

core/src/main/java/feign/Param.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2019 The Feign Authors
2+
* Copyright 2012-2020 The Feign Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
@@ -14,15 +14,15 @@
1414
package feign;
1515

1616
import java.lang.annotation.Retention;
17-
import static java.lang.annotation.ElementType.PARAMETER;
17+
import static java.lang.annotation.ElementType.*;
1818
import static java.lang.annotation.RetentionPolicy.RUNTIME;
1919

2020
/**
21-
* A named template parameter applied to {@link Headers}, {@linkplain RequestLine} or
22-
* {@linkplain Body}
21+
* A named template parameter applied to {@link Headers}, {@linkplain RequestLine},
22+
* {@linkplain Body}, POJO fields or beans properties when it expanding
2323
*/
2424
@Retention(RUNTIME)
25-
@java.lang.annotation.Target(PARAMETER)
25+
@java.lang.annotation.Target({PARAMETER, FIELD, METHOD})
2626
public @interface Param {
2727

2828
/**

core/src/main/java/feign/querymap/BeanQueryMapEncoder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2019 The Feign Authors
2+
* Copyright 2012-2020 The Feign Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
@@ -13,12 +13,14 @@
1313
*/
1414
package feign.querymap;
1515

16+
import feign.Param;
1617
import feign.QueryMapEncoder;
1718
import feign.codec.EncodeException;
1819
import java.beans.IntrospectionException;
1920
import java.beans.Introspector;
2021
import java.beans.PropertyDescriptor;
2122
import java.lang.reflect.InvocationTargetException;
23+
import java.lang.reflect.Method;
2224
import java.util.*;
2325

2426
/**
@@ -40,9 +42,12 @@ public Map<String, Object> encode(Object object) throws EncodeException {
4042
ObjectParamMetadata metadata = getMetadata(object.getClass());
4143
Map<String, Object> propertyNameToValue = new HashMap<String, Object>();
4244
for (PropertyDescriptor pd : metadata.objectProperties) {
43-
Object value = pd.getReadMethod().invoke(object);
45+
Method method = pd.getReadMethod();
46+
Object value = method.invoke(object);
4447
if (value != null && value != object) {
45-
propertyNameToValue.put(pd.getName(), value);
48+
Param alias = method.getAnnotation(Param.class);
49+
String name = alias != null ? alias.value() : pd.getName();
50+
propertyNameToValue.put(name, value);
4651
}
4752
}
4853
return propertyNameToValue;

core/src/main/java/feign/querymap/FieldQueryMapEncoder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2019 The Feign Authors
2+
* Copyright 2012-2020 The Feign Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
@@ -13,6 +13,7 @@
1313
*/
1414
package feign.querymap;
1515

16+
import feign.Param;
1617
import feign.QueryMapEncoder;
1718
import feign.codec.EncodeException;
1819
import java.lang.reflect.Field;
@@ -40,7 +41,9 @@ public Map<String, Object> encode(Object object) throws EncodeException {
4041
for (Field field : metadata.objectFields) {
4142
Object value = field.get(object);
4243
if (value != null && value != object) {
43-
fieldNameToValue.put(field.getName(), value);
44+
Param alias = field.getAnnotation(Param.class);
45+
String name = alias != null ? alias.value() : field.getName();
46+
fieldNameToValue.put(name, value);
4447
}
4548
}
4649
return fieldNameToValue;

core/src/test/java/feign/querymap/BeanQueryMapEncoderTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2019 The Feign Authors
2+
* Copyright 2012-2020 The Feign Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
@@ -13,11 +13,13 @@
1313
*/
1414
package feign.querymap;
1515

16+
import feign.Param;
1617
import feign.QueryMapEncoder;
1718
import org.junit.Rule;
1819
import org.junit.Test;
1920
import org.junit.rules.ExpectedException;
2021
import java.util.HashMap;
22+
import java.util.HashSet;
2123
import java.util.Map;
2224
import static org.junit.Assert.assertEquals;
2325
import static org.junit.Assert.assertTrue;
@@ -70,6 +72,38 @@ public void testDefaultEncoder_haveSuperClass() {
7072
assertEquals("Unexpected encoded query map", expected, encodedMap);
7173
}
7274

75+
@Test
76+
public void testDefaultEncoder_withOverriddenParamName() {
77+
HashSet<Object> expectedNames = new HashSet<>();
78+
expectedNames.add("fooAlias");
79+
expectedNames.add("bar");
80+
final NormalObjectWithOverriddenParamName normalObject =
81+
new NormalObjectWithOverriddenParamName("fooz", "barz");
82+
83+
final Map<String, Object> encodedMap = encoder.encode(normalObject);
84+
85+
assertEquals("@Param ignored", expectedNames, encodedMap.keySet());
86+
}
87+
88+
class NormalObjectWithOverriddenParamName {
89+
90+
private NormalObjectWithOverriddenParamName(String foo, String bar) {
91+
this.foo = foo;
92+
this.bar = bar;
93+
}
94+
95+
private String foo;
96+
private String bar;
97+
98+
@Param("fooAlias")
99+
public String getFoo() {
100+
return foo;
101+
}
102+
103+
public String getBar() {
104+
return bar;
105+
}
106+
}
73107

74108
class NormalObject {
75109

core/src/test/java/feign/querymap/FieldQueryMapEncoderTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2019 The Feign Authors
2+
* Copyright 2012-2020 The Feign Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
@@ -13,14 +13,15 @@
1313
*/
1414
package feign.querymap;
1515

16-
import static org.junit.Assert.assertEquals;
17-
import static org.junit.Assert.assertTrue;
16+
import feign.Param;
1817
import org.junit.Rule;
1918
import org.junit.Test;
2019
import org.junit.rules.ExpectedException;
2120
import java.util.HashMap;
21+
import java.util.HashSet;
2222
import java.util.Map;
2323
import feign.QueryMapEncoder;
24+
import static org.junit.Assert.*;
2425

2526
/**
2627
* Test for {@link FieldQueryMapEncoder}
@@ -53,6 +54,19 @@ public void testDefaultEncoder_normalClassWithOutValues() {
5354
assertTrue("Non-empty map generated from null getter: " + encodedMap, encodedMap.isEmpty());
5455
}
5556

57+
@Test
58+
public void testDefaultEncoder_withOverriddenParamName() {
59+
HashSet<Object> expectedNames = new HashSet<>();
60+
expectedNames.add("fooAlias");
61+
expectedNames.add("bar");
62+
final NormalObjectWithOverriddenParamName normalObject =
63+
new NormalObjectWithOverriddenParamName("fooz", "barz");
64+
65+
final Map<String, Object> encodedMap = encoder.encode(normalObject);
66+
67+
assertEquals("@Param ignored", expectedNames, encodedMap.keySet());
68+
}
69+
5670
class NormalObject {
5771

5872
private NormalObject(String foo, String bar) {
@@ -64,4 +78,16 @@ private NormalObject(String foo, String bar) {
6478
private final String bar;
6579
}
6680

81+
class NormalObjectWithOverriddenParamName {
82+
83+
private NormalObjectWithOverriddenParamName(String foo, String bar) {
84+
this.foo = foo;
85+
this.bar = bar;
86+
}
87+
88+
@Param("fooAlias")
89+
private final String foo;
90+
private final String bar;
91+
}
92+
6793
}

0 commit comments

Comments
 (0)