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<>();