12
12
import com .google .gson .stream .JsonWriter ;
13
13
14
14
import com .stripe .Stripe ;
15
-
16
15
import com .stripe .param .common .EmptyParam ;
16
+
17
17
import java .io .IOException ;
18
18
import java .util .Map ;
19
19
@@ -32,30 +32,50 @@ public void deserializeAndTransform(Map<String, Object> outerMap,
32
32
Map .Entry <String , JsonElement > jsonEntry ,
33
33
UntypedMapDeserializer untypedMapDeserializer ) {
34
34
String key = jsonEntry .getKey ();
35
- JsonElement value = jsonEntry .getValue ();
35
+ JsonElement jsonValue = jsonEntry .getValue ();
36
36
if (ApiRequestParams .EXTRA_PARAMS_KEY .equals (key )) {
37
- if (!value .isJsonObject ()) {
37
+ if (!jsonValue .isJsonObject ()) {
38
38
throw new IllegalStateException (String .format (
39
39
"Unexpected schema for extra params. JSON object is expected at key `%s`, but found"
40
40
+ " `%s`. This is likely a problem with this current library version `%s`. "
41
41
+
"Please contact [email protected] for assistance." ,
42
- ApiRequestParams .EXTRA_PARAMS_KEY , value , Stripe .VERSION ));
42
+ ApiRequestParams .EXTRA_PARAMS_KEY , jsonValue , Stripe .VERSION ));
43
43
}
44
44
// JSON value now corresponds to the extra params map, and is also deserialized as a map.
45
45
// Instead of putting this result map under the original key, flatten the map
46
46
// by adding all its key/value pairs to the outer map instead.
47
47
Map <String , Object > extraParamsMap =
48
- untypedMapDeserializer .deserialize (value .getAsJsonObject ());
49
- outerMap .putAll (extraParamsMap );
48
+ untypedMapDeserializer .deserialize (jsonValue .getAsJsonObject ());
49
+ for (Map .Entry <String , Object > entry : extraParamsMap .entrySet ()) {
50
+ validateDuplicateKey (outerMap , entry .getKey (), entry .getValue ());
51
+ outerMap .put (entry .getKey (), entry .getValue ());
52
+ }
50
53
} else {
54
+ Object value = untypedMapDeserializer .deserializeJsonElement (jsonValue );
55
+ validateDuplicateKey (outerMap , key , value );
56
+
51
57
// Normal deserialization where output map has the same structure as the given JSON content.
52
58
// The deserialized content is an untyped `Object` and added to the outer map at the
53
59
// original key.
54
- outerMap .put (key , untypedMapDeserializer . deserializeJsonElement ( value ) );
60
+ outerMap .put (key , value );
55
61
}
56
62
}
57
63
}
58
64
65
+ private static void validateDuplicateKey (Map <String , Object > outerMap ,
66
+ String paramKey , Object paramValue ) {
67
+ if (outerMap .containsKey (paramKey )) {
68
+ throw new IllegalArgumentException (String .format (
69
+ "Found multiple param values for the same param key. This can happen because you passed "
70
+ + "additional parameters via `putExtraParam` that conflict with the existing params. "
71
+ + "Found param key `%s` with values `%s` and `%s`. "
72
+ + "If you wish to pass additional params for nested parameters, you "
73
+ + "should add extra params at the nested params themselves, not from the "
74
+ + "top-level param." ,
75
+ paramKey , outerMap .get (paramKey ), paramValue ));
76
+ }
77
+ }
78
+
59
79
/**
60
80
* Type adapter to convert an empty enum to null value to comply with the lower-lever encoding
61
81
* logic for the API request parameters.
0 commit comments