Skip to content

Commit 8b0b732

Browse files
authored
Changes EC2 query protocol marshalling to not marshall empty lists, b… (#5369)
* Changes EC2 query protocol marshalling to not marshall empty lists, because the server throws an exception in such cases. * Fix whitespace issue
1 parent 85352be commit 8b0b732

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Do not serialize empty lists for the EC2 variant of the Query protocol. The service returns exceptions if it gets query parameters with no values."
6+
}

core/protocols/aws-query-protocol/src/main/java/software/amazon/awssdk/protocols/query/internal/marshall/ListQueryMarshaller.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,29 @@
2626
*/
2727
@SdkInternalApi
2828
public class ListQueryMarshaller implements QueryMarshaller<List<?>> {
29+
private static final PathResolver AWS_QUERY_PATH_RESOLVER = (path, i, listTrait) ->
30+
listTrait.isFlattened() ?
31+
String.format("%s.%d", path, i + 1) :
32+
String.format("%s.%s.%d", path, listTrait.memberFieldInfo().locationName(), i + 1);
33+
private static final PathResolver EC2_QUERY_PATH_RESOLVER = (path, i, listTrait) -> String.format("%s.%d", path, i + 1);
34+
35+
private static final EmptyListMarshaller AWS_QUERY_EMPTY_LIST_MARSHALLER =
36+
(context, path) -> context.request().putRawQueryParameter(path, "");
37+
private static final EmptyListMarshaller EC2_QUERY_EMPTY_LIST_MARSHALLER = (context, path) -> { };
2938

3039
private final PathResolver pathResolver;
40+
private final EmptyListMarshaller emptyListMarshaller;
3141

32-
private ListQueryMarshaller(PathResolver pathResolver) {
42+
private ListQueryMarshaller(PathResolver pathResolver, EmptyListMarshaller emptyListMarshaller) {
3343
this.pathResolver = pathResolver;
44+
this.emptyListMarshaller = emptyListMarshaller;
3445
}
3546

3647
@Override
3748
public void marshall(QueryMarshallerContext context, String path, List<?> val, SdkField<List<?>> sdkField) {
38-
// Explicitly empty lists are marshalled as a query param with empty value in AWS/Query
49+
// Explicitly empty lists are marshalled as a query param with empty value in AWS/Query, but not in EC2/Query
3950
if (val.isEmpty() && !(val instanceof SdkAutoConstructList)) {
40-
context.request().putRawQueryParameter(path, "");
51+
emptyListMarshaller.marshall(context, path);
4152
return;
4253
}
4354
for (int i = 0; i < val.size(); i++) {
@@ -55,16 +66,18 @@ private interface PathResolver {
5566
String resolve(String path, int i, ListTrait listTrait);
5667
}
5768

69+
@FunctionalInterface
70+
private interface EmptyListMarshaller {
71+
void marshall(QueryMarshallerContext context, String path);
72+
}
73+
5874
/**
5975
* Wires up the {@link ListQueryMarshaller} with a {@link PathResolver} that respects the flattened trait.
6076
*
6177
* @return ListQueryMarshaller.
6278
*/
6379
public static ListQueryMarshaller awsQuery() {
64-
return new ListQueryMarshaller((path, i, listTrait) ->
65-
listTrait.isFlattened() ?
66-
String.format("%s.%d", path, i + 1) :
67-
String.format("%s.%s.%d", path, listTrait.memberFieldInfo().locationName(), i + 1));
80+
return new ListQueryMarshaller(AWS_QUERY_PATH_RESOLVER, AWS_QUERY_EMPTY_LIST_MARSHALLER);
6881
}
6982

7083
/**
@@ -74,6 +87,6 @@ public static ListQueryMarshaller awsQuery() {
7487
* @return ListQueryMarshaller.
7588
*/
7689
public static ListQueryMarshaller ec2Query() {
77-
return new ListQueryMarshaller((path, i, listTrait) -> String.format("%s.%d", path, i + 1));
90+
return new ListQueryMarshaller(EC2_QUERY_PATH_RESOLVER, EC2_QUERY_EMPTY_LIST_MARSHALLER);
7891
}
7992
}

test/protocol-tests-core/src/main/resources/software/amazon/awssdk/protocol/suites/cases/ec2-input.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,26 @@
167167
}
168168
}
169169
}
170+
},
171+
{
172+
"description": "An empty list is not marshalled in EC2/Query",
173+
"given": {
174+
"input": {
175+
"ListMemberWithOnlyMemberLocation": []
176+
}
177+
},
178+
"when": {
179+
"action": "marshall",
180+
"operation": "Ec2Types"
181+
},
182+
"then": {
183+
"serializedAs": {
184+
"params": {
185+
"doesNotContain": [
186+
"Item"
187+
]
188+
}
189+
}
190+
}
170191
}
171192
]

0 commit comments

Comments
 (0)