Browse Source
* Added support for custom param encoding * Added ability to inherit @CustomParam annotation * Updated class cast style to match rest of code * Updated to use QueryMap for custom pojo query parameters * Clarification in README of QueryMap POJO usage * Removed unused line * Updated custom POJO QueryMap test to prove that private fields can be used * Removed no-longer-valid test endpoint * Renamed tests to more accurately reflect their contents * More test cleanup * Modified QueryMap POJO encoding to use specified QueryMapEncoder (default implementation provided) * Corrected typo in README.md * Fixed merge conflict and typo in test namepull/689/head
Shadow Man
7 years ago
committed by
Marvin Froeder
12 changed files with 421 additions and 32 deletions
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
/** |
||||
* Copyright 2012-2018 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 |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License |
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
||||
* or implied. See the License for the specific language governing permissions and limitations under |
||||
* the License. |
||||
*/ |
||||
package feign; |
||||
|
||||
import feign.codec.EncodeException; |
||||
import java.lang.reflect.Field; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* A QueryMapEncoder encodes Objects into maps of query parameter names to values. |
||||
*/ |
||||
public interface QueryMapEncoder { |
||||
|
||||
/** |
||||
* Encodes the given object into a query map. |
||||
* |
||||
* @param object the object to encode |
||||
* @return the map represented by the object |
||||
*/ |
||||
Map<String, Object> encode (Object object); |
||||
|
||||
class Default implements QueryMapEncoder { |
||||
|
||||
private final Map<Class<?>, ObjectParamMetadata> classToMetadata = |
||||
new HashMap<Class<?>, ObjectParamMetadata>(); |
||||
|
||||
@Override |
||||
public Map<String, Object> encode (Object object) throws EncodeException { |
||||
try { |
||||
ObjectParamMetadata metadata = getMetadata(object.getClass()); |
||||
Map<String, Object> fieldNameToValue = new HashMap<String, Object>(); |
||||
for (Field field : metadata.objectFields) { |
||||
Object value = field.get(object); |
||||
if (value != null && value != object) { |
||||
fieldNameToValue.put(field.getName(), value); |
||||
} |
||||
} |
||||
return fieldNameToValue; |
||||
} catch (IllegalAccessException e) { |
||||
throw new EncodeException("Failure encoding object into query map", e); |
||||
} |
||||
} |
||||
|
||||
private ObjectParamMetadata getMetadata(Class<?> objectType) { |
||||
ObjectParamMetadata metadata = classToMetadata.get(objectType); |
||||
if (metadata == null) { |
||||
metadata = ObjectParamMetadata.parseObjectType(objectType); |
||||
classToMetadata.put(objectType, metadata); |
||||
} |
||||
return metadata; |
||||
} |
||||
|
||||
private static class ObjectParamMetadata { |
||||
|
||||
private final List<Field> objectFields; |
||||
|
||||
private ObjectParamMetadata (List<Field> objectFields) { |
||||
this.objectFields = Collections.unmodifiableList(objectFields); |
||||
} |
||||
|
||||
private static ObjectParamMetadata parseObjectType(Class<?> type) { |
||||
List<Field> fields = new ArrayList<Field>(); |
||||
for (Field field : type.getDeclaredFields()) { |
||||
if (!field.isAccessible()) { |
||||
field.setAccessible(true); |
||||
} |
||||
fields.add(field); |
||||
} |
||||
return new ObjectParamMetadata(fields); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
/** |
||||
* Copyright 2012-2018 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 |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License |
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
||||
* or implied. See the License for the specific language governing permissions and limitations under |
||||
* the License. |
||||
*/ |
||||
package feign; |
||||
|
||||
public class CustomPojo { |
||||
|
||||
private final String name; |
||||
private final Integer number; |
||||
|
||||
CustomPojo(String name, Integer number) { |
||||
this.name = name; |
||||
this.number = number; |
||||
} |
||||
} |
@ -0,0 +1,78 @@
@@ -0,0 +1,78 @@
|
||||
/** |
||||
* Copyright 2012-2018 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 |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License |
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
||||
* or implied. See the License for the specific language governing permissions and limitations under |
||||
* the License. |
||||
*/ |
||||
package feign; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.ExpectedException; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
public class DefaultQueryMapEncoderTest { |
||||
|
||||
@Rule |
||||
public final ExpectedException thrown = ExpectedException.none(); |
||||
|
||||
private final QueryMapEncoder encoder = new QueryMapEncoder.Default(); |
||||
|
||||
@Test |
||||
public void testEncodesObject_visibleFields() { |
||||
Map<String, Object> expected = new HashMap<>(); |
||||
expected.put("foo", "fooz"); |
||||
expected.put("bar", "barz"); |
||||
expected.put("baz", "bazz"); |
||||
VisibleFieldsObject object = new VisibleFieldsObject(); |
||||
object.foo = "fooz"; |
||||
object.bar = "barz"; |
||||
object.baz = "bazz"; |
||||
|
||||
Map<String, Object> encodedMap = encoder.encode(object); |
||||
assertEquals("Unexpected encoded query map", expected, encodedMap); |
||||
} |
||||
|
||||
@Test |
||||
public void testEncodesObject_visibleFields_emptyObject() { |
||||
VisibleFieldsObject object = new VisibleFieldsObject(); |
||||
Map<String, Object> encodedMap = encoder.encode(object); |
||||
assertTrue("Non-empty map generated from null fields: " + encodedMap, encodedMap.isEmpty()); |
||||
} |
||||
|
||||
@Test |
||||
public void testEncodesObject_nonVisibleFields() { |
||||
Map<String, Object> expected = new HashMap<>(); |
||||
expected.put("foo", "fooz"); |
||||
expected.put("bar", "barz"); |
||||
QueryMapEncoderObject object = new QueryMapEncoderObject("fooz", "barz"); |
||||
|
||||
Map<String, Object> encodedMap = encoder.encode(object); |
||||
assertEquals("Unexpected encoded query map", expected, encodedMap); |
||||
} |
||||
|
||||
@Test |
||||
public void testEncodesObject_nonVisibleFields_emptyObject() { |
||||
QueryMapEncoderObject object = new QueryMapEncoderObject(null, null); |
||||
Map<String, Object> encodedMap = encoder.encode(object); |
||||
assertTrue("Non-empty map generated from null fields", encodedMap.isEmpty()); |
||||
} |
||||
|
||||
static class VisibleFieldsObject { |
||||
String foo; |
||||
String bar; |
||||
String baz; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
/** |
||||
* Copyright 2012-2018 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 |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License |
||||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
||||
* or implied. See the License for the specific language governing permissions and limitations under |
||||
* the License. |
||||
*/ |
||||
package feign; |
||||
|
||||
class QueryMapEncoderObject { |
||||
private final String foo; |
||||
private final String bar; |
||||
|
||||
QueryMapEncoderObject (String foo, String bar) { |
||||
this.foo = foo; |
||||
this.bar = bar; |
||||
} |
||||
} |
Loading…
Reference in new issue