Skip to content

Commit d2d9621

Browse files
authored
Filter out sythetic fields from FieldQueryMapEncoder (#840)
1 parent 708926c commit d2d9621

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import feign.codec.EncodeException;
1818
import java.lang.reflect.Field;
1919
import java.util.*;
20+
import java.util.stream.Collectors;
2021

2122
/**
2223
* the query map will be generated using member variable names as query parameter names.
@@ -66,14 +67,11 @@ private ObjectParamMetadata(List<Field> objectFields) {
6667
}
6768

6869
private static ObjectParamMetadata parseObjectType(Class<?> type) {
69-
List<Field> fields = new ArrayList<Field>();
70-
for (Field field : type.getDeclaredFields()) {
71-
if (!field.isAccessible()) {
72-
field.setAccessible(true);
73-
}
74-
fields.add(field);
75-
}
76-
return new ObjectParamMetadata(fields);
70+
return new ObjectParamMetadata(
71+
Arrays.stream(type.getDeclaredFields())
72+
.filter(field -> !field.isSynthetic())
73+
.peek(field -> field.setAccessible(true))
74+
.collect(Collectors.toList()));
7775
}
7876
}
7977
}

core/src/test/java/feign/querymap/PropertyQueryMapEncoderTest.java renamed to core/src/test/java/feign/querymap/BeanQueryMapEncoderTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import org.junit.Test;
2424
import org.junit.rules.ExpectedException;
2525

26-
public class PropertyQueryMapEncoderTest {
26+
/** Test for {@link BeanQueryMapEncoder} */
27+
public class BeanQueryMapEncoderTest {
2728

2829
@Rule public final ExpectedException thrown = ExpectedException.none();
2930

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright 2012-2018 The Feign Authors
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package feign.querymap;
15+
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertTrue;
18+
19+
import feign.QueryMapEncoder;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
import org.junit.rules.ExpectedException;
25+
26+
/** Test for {@link FieldQueryMapEncoder} */
27+
public class FieldQueryMapEncoderTest {
28+
29+
@Rule public final ExpectedException thrown = ExpectedException.none();
30+
31+
private final QueryMapEncoder encoder = new FieldQueryMapEncoder();
32+
33+
@Test
34+
public void testDefaultEncoder_normalClassWithValues() {
35+
final Map<String, Object> expected = new HashMap<>();
36+
expected.put("foo", "fooz");
37+
expected.put("bar", "barz");
38+
final NormalObject normalObject = new NormalObject("fooz", "barz");
39+
40+
final Map<String, Object> encodedMap = encoder.encode(normalObject);
41+
42+
assertEquals("Unexpected encoded query map", expected, encodedMap);
43+
}
44+
45+
@Test
46+
public void testDefaultEncoder_normalClassWithOutValues() {
47+
final NormalObject normalObject = new NormalObject(null, null);
48+
49+
final Map<String, Object> encodedMap = encoder.encode(normalObject);
50+
51+
assertTrue("Non-empty map generated from null getter: " + encodedMap, encodedMap.isEmpty());
52+
}
53+
54+
class NormalObject {
55+
56+
private NormalObject(String foo, String bar) {
57+
this.foo = foo;
58+
this.bar = bar;
59+
}
60+
61+
private final String foo;
62+
private final String bar;
63+
}
64+
}

0 commit comments

Comments
 (0)