Browse Source

Allow QueryMapEncoder accept null value (#2086)

For
```java
@GetMapping
public Paged<User> list(int page, int size, @QueryMap User user);
```
now we can use
```java
Paged<User> paged = userClient.list(1, 10, null);
```
instead of
```java
Paged<User> paged = userClient.list(1, 10, new User()); // query map will include primitive type default values which is unexpected
```
pull/2096/head
Yanming Zhou 1 year ago committed by GitHub
parent
commit
c43515137c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      core/src/main/java/feign/querymap/BeanQueryMapEncoder.java
  2. 3
      core/src/main/java/feign/querymap/FieldQueryMapEncoder.java
  3. 6
      core/src/test/java/feign/querymap/BeanQueryMapEncoderTest.java
  4. 6
      core/src/test/java/feign/querymap/FieldQueryMapEncoderTest.java

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

@ -38,6 +38,9 @@ public class BeanQueryMapEncoder implements QueryMapEncoder { @@ -38,6 +38,9 @@ public class BeanQueryMapEncoder implements QueryMapEncoder {
@Override
public Map<String, Object> encode(Object object) throws EncodeException {
if (object == null) {
return Collections.emptyMap();
}
try {
ObjectParamMetadata metadata = getMetadata(object.getClass());
Map<String, Object> propertyNameToValue = new HashMap<String, Object>();

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

@ -40,6 +40,9 @@ public class FieldQueryMapEncoder implements QueryMapEncoder { @@ -40,6 +40,9 @@ public class FieldQueryMapEncoder implements QueryMapEncoder {
@Override
public Map<String, Object> encode(Object object) throws EncodeException {
if (object == null) {
return Collections.emptyMap();
}
ObjectParamMetadata metadata =
classToMetadata.computeIfAbsent(object.getClass(), ObjectParamMetadata::parseObjectType);

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

@ -18,6 +18,7 @@ import feign.QueryMapEncoder; @@ -18,6 +18,7 @@ import feign.QueryMapEncoder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -34,6 +35,11 @@ public class BeanQueryMapEncoderTest { @@ -34,6 +35,11 @@ public class BeanQueryMapEncoderTest {
private final QueryMapEncoder encoder = new BeanQueryMapEncoder();
@Test
public void testDefaultEncoder_acceptNullValue() {
assertEquals("Empty map should be returned", Collections.EMPTY_MAP, encoder.encode(null));
}
@Test
public void testDefaultEncoder_normalClassWithValues() {
Map<String, Object> expected = new HashMap<>();

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

@ -17,6 +17,7 @@ import feign.Param; @@ -17,6 +17,7 @@ import feign.Param;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -33,6 +34,11 @@ public class FieldQueryMapEncoderTest { @@ -33,6 +34,11 @@ public class FieldQueryMapEncoderTest {
private final QueryMapEncoder encoder = new FieldQueryMapEncoder();
@Test
public void testDefaultEncoder_acceptNullValue() {
assertEquals("Empty map should be returned", Collections.EMPTY_MAP, encoder.encode(null));
}
@Test
public void testDefaultEncoder_normalClassWithValues() {
final Map<String, Object> expected = new HashMap<>();

Loading…
Cancel
Save