Skip to content

Commit

Permalink
Overriding query parameter name (#1184)
Browse files Browse the repository at this point in the history
* Add possibility to override request parameter name in objects by @param

Fixes #1183
  • Loading branch information
boggard committed Mar 9, 2020
1 parent 2cc907c commit 68f7984
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
10 changes: 5 additions & 5 deletions core/src/main/java/feign/Param.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2019 The Feign Authors
* Copyright 2012-2020 The Feign Authors
*
* 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
Expand All @@ -14,15 +14,15 @@
package feign;

import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* A named template parameter applied to {@link Headers}, {@linkplain RequestLine} or
* {@linkplain Body}
* A named template parameter applied to {@link Headers}, {@linkplain RequestLine},
* {@linkplain Body}, POJO fields or beans properties when it expanding
*/
@Retention(RUNTIME)
@java.lang.annotation.Target(PARAMETER)
@java.lang.annotation.Target({PARAMETER, FIELD, METHOD})
public @interface Param {

/**
Expand Down
11 changes: 8 additions & 3 deletions core/src/main/java/feign/querymap/BeanQueryMapEncoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2019 The Feign Authors
* Copyright 2012-2020 The Feign Authors
*
* 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
Expand All @@ -13,12 +13,14 @@
*/
package feign.querymap;

import feign.Param;
import feign.QueryMapEncoder;
import feign.codec.EncodeException;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
Expand All @@ -40,9 +42,12 @@ public Map<String, Object> encode(Object object) throws EncodeException {
ObjectParamMetadata metadata = getMetadata(object.getClass());
Map<String, Object> propertyNameToValue = new HashMap<String, Object>();
for (PropertyDescriptor pd : metadata.objectProperties) {
Object value = pd.getReadMethod().invoke(object);
Method method = pd.getReadMethod();
Object value = method.invoke(object);
if (value != null && value != object) {
propertyNameToValue.put(pd.getName(), value);
Param alias = method.getAnnotation(Param.class);
String name = alias != null ? alias.value() : pd.getName();
propertyNameToValue.put(name, value);
}
}
return propertyNameToValue;
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/feign/querymap/FieldQueryMapEncoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2019 The Feign Authors
* Copyright 2012-2020 The Feign Authors
*
* 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
Expand All @@ -13,6 +13,7 @@
*/
package feign.querymap;

import feign.Param;
import feign.QueryMapEncoder;
import feign.codec.EncodeException;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -40,7 +41,9 @@ public Map<String, Object> encode(Object object) throws EncodeException {
for (Field field : metadata.objectFields) {
Object value = field.get(object);
if (value != null && value != object) {
fieldNameToValue.put(field.getName(), value);
Param alias = field.getAnnotation(Param.class);
String name = alias != null ? alias.value() : field.getName();
fieldNameToValue.put(name, value);
}
}
return fieldNameToValue;
Expand Down
36 changes: 35 additions & 1 deletion core/src/test/java/feign/querymap/BeanQueryMapEncoderTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2019 The Feign Authors
* Copyright 2012-2020 The Feign Authors
*
* 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
Expand All @@ -13,11 +13,13 @@
*/
package feign.querymap;

import feign.Param;
import feign.QueryMapEncoder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -70,6 +72,38 @@ public void testDefaultEncoder_haveSuperClass() {
assertEquals("Unexpected encoded query map", expected, encodedMap);
}

@Test
public void testDefaultEncoder_withOverriddenParamName() {
HashSet<Object> expectedNames = new HashSet<>();
expectedNames.add("fooAlias");
expectedNames.add("bar");
final NormalObjectWithOverriddenParamName normalObject =
new NormalObjectWithOverriddenParamName("fooz", "barz");

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

assertEquals("@Param ignored", expectedNames, encodedMap.keySet());
}

class NormalObjectWithOverriddenParamName {

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

private String foo;
private String bar;

@Param("fooAlias")
public String getFoo() {
return foo;
}

public String getBar() {
return bar;
}
}

class NormalObject {

Expand Down
32 changes: 29 additions & 3 deletions core/src/test/java/feign/querymap/FieldQueryMapEncoderTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2019 The Feign Authors
* Copyright 2012-2020 The Feign Authors
*
* 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
Expand All @@ -13,14 +13,15 @@
*/
package feign.querymap;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import feign.Param;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import feign.QueryMapEncoder;
import static org.junit.Assert.*;

/**
* Test for {@link FieldQueryMapEncoder}
Expand Down Expand Up @@ -53,6 +54,19 @@ public void testDefaultEncoder_normalClassWithOutValues() {
assertTrue("Non-empty map generated from null getter: " + encodedMap, encodedMap.isEmpty());
}

@Test
public void testDefaultEncoder_withOverriddenParamName() {
HashSet<Object> expectedNames = new HashSet<>();
expectedNames.add("fooAlias");
expectedNames.add("bar");
final NormalObjectWithOverriddenParamName normalObject =
new NormalObjectWithOverriddenParamName("fooz", "barz");

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

assertEquals("@Param ignored", expectedNames, encodedMap.keySet());
}

class NormalObject {

private NormalObject(String foo, String bar) {
Expand All @@ -64,4 +78,16 @@ private NormalObject(String foo, String bar) {
private final String bar;
}

class NormalObjectWithOverriddenParamName {

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

@Param("fooAlias")
private final String foo;
private final String bar;
}

}

0 comments on commit 68f7984

Please sign in to comment.