Skip to content

Commit

Permalink
Filter out sythetic fields from FieldQueryMapEncoder (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
velo authored Nov 13, 2018
1 parent 708926c commit d2d9621
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
14 changes: 6 additions & 8 deletions core/src/main/java/feign/querymap/FieldQueryMapEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import feign.codec.EncodeException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

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

private static ObjectParamMetadata parseObjectType(Class<?> type) {
List<Field> fields = new ArrayList<Field>();
for (Field field : type.getDeclaredFields()) {
if (!field.isAccessible()) {
field.setAccessible(true);
}
fields.add(field);
}
return new ObjectParamMetadata(fields);
return new ObjectParamMetadata(
Arrays.stream(type.getDeclaredFields())
.filter(field -> !field.isSynthetic())
.peek(field -> field.setAccessible(true))
.collect(Collectors.toList()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class PropertyQueryMapEncoderTest {
/** Test for {@link BeanQueryMapEncoder} */
public class BeanQueryMapEncoderTest {

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

Expand Down
64 changes: 64 additions & 0 deletions core/src/test/java/feign/querymap/FieldQueryMapEncoderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2012-2018 The Feign Authors
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package feign.querymap;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import feign.QueryMapEncoder;
import java.util.HashMap;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/** Test for {@link FieldQueryMapEncoder} */
public class FieldQueryMapEncoderTest {

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

private final QueryMapEncoder encoder = new FieldQueryMapEncoder();

@Test
public void testDefaultEncoder_normalClassWithValues() {
final Map<String, Object> expected = new HashMap<>();
expected.put("foo", "fooz");
expected.put("bar", "barz");
final NormalObject normalObject = new NormalObject("fooz", "barz");

final Map<String, Object> encodedMap = encoder.encode(normalObject);

assertEquals("Unexpected encoded query map", expected, encodedMap);
}

@Test
public void testDefaultEncoder_normalClassWithOutValues() {
final NormalObject normalObject = new NormalObject(null, null);

final Map<String, Object> encodedMap = encoder.encode(normalObject);

assertTrue("Non-empty map generated from null getter: " + encodedMap, encodedMap.isEmpty());
}

class NormalObject {

private NormalObject(String foo, String bar) {
this.foo = foo;
this.bar = bar;
}

private final String foo;
private final String bar;
}
}

0 comments on commit d2d9621

Please sign in to comment.