From e8ee60e5332701ccf1f5d290b3e76b568a443c3c Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Wed, 21 Jul 2021 09:54:02 +0800 Subject: [PATCH] Allow QueryMapEncoder accept null value For ```java @GetMapping public Paged list(int page, int size, @QueryMap User user); ``` now we can use ```java Paged paged = userClient.list(1, 10, null); ``` instead of ```java Paged paged = userClient.list(1, 10, new User()); // query map will include primitive type default values which is unexpected ``` --- core/src/main/java/feign/querymap/BeanQueryMapEncoder.java | 3 +++ core/src/main/java/feign/querymap/FieldQueryMapEncoder.java | 3 +++ .../test/java/feign/querymap/BeanQueryMapEncoderTest.java | 6 ++++++ .../test/java/feign/querymap/FieldQueryMapEncoderTest.java | 6 ++++++ 4 files changed, 18 insertions(+) diff --git a/core/src/main/java/feign/querymap/BeanQueryMapEncoder.java b/core/src/main/java/feign/querymap/BeanQueryMapEncoder.java index 5e241f775..a6a378f3b 100644 --- a/core/src/main/java/feign/querymap/BeanQueryMapEncoder.java +++ b/core/src/main/java/feign/querymap/BeanQueryMapEncoder.java @@ -38,6 +38,9 @@ public class BeanQueryMapEncoder implements QueryMapEncoder { @Override public Map encode(Object object) throws EncodeException { + if (object == null) { + return Collections.emptyMap(); + } try { ObjectParamMetadata metadata = getMetadata(object.getClass()); Map propertyNameToValue = new HashMap(); diff --git a/core/src/main/java/feign/querymap/FieldQueryMapEncoder.java b/core/src/main/java/feign/querymap/FieldQueryMapEncoder.java index 7de362f4e..6554df6fe 100644 --- a/core/src/main/java/feign/querymap/FieldQueryMapEncoder.java +++ b/core/src/main/java/feign/querymap/FieldQueryMapEncoder.java @@ -40,6 +40,9 @@ public class FieldQueryMapEncoder implements QueryMapEncoder { @Override public Map encode(Object object) throws EncodeException { + if (object == null) { + return Collections.emptyMap(); + } ObjectParamMetadata metadata = classToMetadata.computeIfAbsent(object.getClass(), ObjectParamMetadata::parseObjectType); diff --git a/core/src/test/java/feign/querymap/BeanQueryMapEncoderTest.java b/core/src/test/java/feign/querymap/BeanQueryMapEncoderTest.java index 97a1bf66d..b56293a2f 100644 --- a/core/src/test/java/feign/querymap/BeanQueryMapEncoderTest.java +++ b/core/src/test/java/feign/querymap/BeanQueryMapEncoderTest.java @@ -18,6 +18,7 @@ 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 { 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 expected = new HashMap<>(); diff --git a/core/src/test/java/feign/querymap/FieldQueryMapEncoderTest.java b/core/src/test/java/feign/querymap/FieldQueryMapEncoderTest.java index 6961f3eba..dbeeb7ce8 100644 --- a/core/src/test/java/feign/querymap/FieldQueryMapEncoderTest.java +++ b/core/src/test/java/feign/querymap/FieldQueryMapEncoderTest.java @@ -17,6 +17,7 @@ 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 { 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 expected = new HashMap<>();