Skip to content

Commit c18bcda

Browse files
author
Adrian Cole
committed
Exposes Encoder.MAP_STRING_WILDCARD to make form encoding easier
Before, instructions around form encoding were incomplete, particularly around how to get a hold of a `Map<String, ?>` type. This exposes `Encoder.MAP_STRING_WILDCARD` to make form encoding easier. Closes OpenFeign#259
1 parent c8a1483 commit c18bcda

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

core/src/main/java/feign/ReflectiveFeign.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ protected RequestTemplate resolve(Object[] argv, RequestTemplate mutable,
263263
}
264264
}
265265
try {
266-
encoder.encode(formVariables, Types.MAP_STRING_WILDCARD, mutable);
266+
encoder.encode(formVariables, Encoder.MAP_STRING_WILDCARD, mutable);
267267
} catch (EncodeException e) {
268268
throw e;
269269
} catch (RuntimeException e) {

core/src/main/java/feign/Types.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@
3434
*/
3535
final class Types {
3636

37-
/**
38-
* Type literal for {@code Map<String, ?>}.
39-
*/
40-
static final Type MAP_STRING_WILDCARD = new ParameterizedTypeImpl(null, Map.class, String.class,
41-
new WildcardTypeImpl(
42-
new Type[]{Object.class},
43-
new Type[]{}));
44-
4537
private static final Type[] EMPTY_TYPE_ARRAY = new Type[0];
4638

4739
private Types() {
@@ -313,7 +305,7 @@ private static void checkNotPrimitive(Type type) {
313305
}
314306
}
315307

316-
private static final class ParameterizedTypeImpl implements ParameterizedType {
308+
static final class ParameterizedTypeImpl implements ParameterizedType {
317309

318310
private final Type ownerType;
319311
private final Type rawType;
@@ -409,7 +401,7 @@ public String toString() {
409401
* support what the Java 6 language needs - at most one bound. If a lower bound is set, the upper
410402
* bound must be Object.class.
411403
*/
412-
private static final class WildcardTypeImpl implements WildcardType {
404+
static final class WildcardTypeImpl implements WildcardType {
413405

414406
private final Type upperBound;
415407
private final Type lowerBound;

core/src/main/java/feign/Util.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public class Util {
7878
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
7979
private static final int BUF_SIZE = 0x800; // 2K chars (4K bytes)
8080

81+
82+
/**
83+
* Type literal for {@code Map<String, ?>}.
84+
*/
85+
public static final Type MAP_STRING_WILDCARD =
86+
new Types.ParameterizedTypeImpl(null, Map.class, String.class,
87+
new Types.WildcardTypeImpl(new Type[]{Object.class}, new Type[0]));
88+
8189
private Util() { // no instances
8290
}
8391

core/src/main/java/feign/codec/Encoder.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.lang.reflect.Type;
1919

2020
import feign.RequestTemplate;
21+
import feign.Util;
2122

2223
import static java.lang.String.format;
2324

@@ -46,24 +47,28 @@
4647
* }
4748
* </pre>
4849
*
49-
* <p/> <h3>Form encoding</h3> <br> If any parameters are found in {@link
50-
* feign.MethodMetadata#formParams()}, they will be collected and passed to the Encoder as a {@code
51-
* Map<String, ?>}. <br>
50+
* <p><h3>Form encoding</h3> <p>If any parameters are found in {@link
51+
* feign.MethodMetadata#formParams()}, they will be collected and passed to the Encoder as a map.
52+
*
53+
* <p>Ex. The following is a form. Notice the parameters aren't consumed in the request line. A map
54+
* including "username" and "password" keys will passed to the encoder, and the body type will be
55+
* {@link #MAP_STRING_WILDCARD}.
5256
* <pre>
53-
* &#064;POST
54-
* &#064;Path(&quot;/&quot;)
57+
* &#064;RequestLine(&quot;POST /&quot;)
5558
* Session login(@Param(&quot;username&quot;) String username, @Param(&quot;password&quot;) String
5659
* password);
5760
* </pre>
5861
*/
5962
public interface Encoder {
63+
/** Type literal for {@code Map<String, ?>}, indicating the object to encode is a form. */
64+
Type MAP_STRING_WILDCARD = Util.MAP_STRING_WILDCARD;
6065

6166
/**
6267
* Converts objects to an appropriate representation in the template.
6368
*
6469
* @param object what to encode as the request body.
65-
* @param bodyType the type the object should be encoded as. {@code Map<String, ?>}, if form
66-
* encoding.
70+
* @param bodyType the type the object should be encoded as. {@link #MAP_STRING_WILDCARD}
71+
* indicates form encoding.
6772
* @param template the request template to populate.
6873
* @throws EncodeException when encoding failed due to a checked exception.
6974
*/

0 commit comments

Comments
 (0)