Skip to content

Commit e15e9f0

Browse files
authored
Filter out sythetic fields from FieldQueryMapEncoder (OpenFeign#840)
1 parent 1727283 commit e15e9f0

3 files changed

Lines changed: 77 additions & 9 deletions

File tree

‎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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import static org.junit.Assert.assertEquals;
2323
import static org.junit.Assert.assertTrue;
2424

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

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

0 commit comments

Comments
 (0)