Skip to content

Commit

Permalink
Allow QueryMapEncoder accept null value (#2086)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
quaff committed Jun 19, 2023
1 parent ad167f5 commit c435151
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/feign/querymap/BeanQueryMapEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/feign/querymap/FieldQueryMapEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<>();
Expand Down

0 comments on commit c435151

Please sign in to comment.