Skip to content

Commit a1a4b44

Browse files
authored
Fix EC2 DescribeInstances query use locale-dependent formatting (#6238)
* Fix locale format * added unit test for the fix * comments addressed * remove unused import
1 parent 4bf8e38 commit a1a4b44

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
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 Query Protocol",
4+
"contributor": "",
5+
"description": "Fixed EC2 DescribeInstances query failure in non-ASCII digit locales by using ROOT locale when formatting query paths to ensure ASCII digits are always used."
6+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
public class ListQueryMarshaller implements QueryMarshaller<List<?>> {
3030
private static final PathResolver AWS_QUERY_PATH_RESOLVER = (path, i, listTrait) ->
3131
listTrait.isFlattened() ?
32-
String.format("%s.%d", path, i + 1) :
33-
String.format("%s.%s.%d", path, listTrait.memberFieldInfo().locationName(), i + 1);
34-
private static final PathResolver EC2_QUERY_PATH_RESOLVER = (path, i, listTrait) -> String.format("%s.%d", path, i + 1);
32+
path + "." + (i + 1) :
33+
path + "." + listTrait.memberFieldInfo().locationName() + "." + (i + 1);
34+
private static final PathResolver EC2_QUERY_PATH_RESOLVER = (path, i, listTrait) -> path + "." + (i + 1);
3535

3636
private static final EmptyListMarshaller AWS_QUERY_EMPTY_LIST_MARSHALLER =
3737
(context, path) -> context.request().putRawQueryParameter(path, "");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void marshall(QueryMarshallerContext context, String path, Map<String, ?>
4646

4747
private static String resolveMapPath(String path, MapTrait mapTrait, AtomicInteger entryNum, String s) {
4848
return mapTrait.isFlattened() ?
49-
String.format("%s.%d.%s", path, entryNum.get(), s) :
50-
String.format("%s.entry.%d.%s", path, entryNum.get(), s);
49+
path + "." + entryNum.get() + "." + s :
50+
path + ".entry." + entryNum.get() + "." + s;
5151
}
5252
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.protocols.query;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.Locale;
24+
import java.util.Map;
25+
import org.junit.jupiter.api.Test;
26+
import software.amazon.awssdk.core.SdkField;
27+
import software.amazon.awssdk.core.protocol.MarshallLocation;
28+
import software.amazon.awssdk.core.protocol.MarshallingType;
29+
import software.amazon.awssdk.core.traits.ListTrait;
30+
import software.amazon.awssdk.core.traits.LocationTrait;
31+
import software.amazon.awssdk.http.SdkHttpFullRequest;
32+
import software.amazon.awssdk.protocols.query.internal.marshall.ListQueryMarshaller;
33+
import software.amazon.awssdk.protocols.query.internal.marshall.QueryMarshaller;
34+
import software.amazon.awssdk.protocols.query.internal.marshall.QueryMarshallerContext;
35+
import software.amazon.awssdk.protocols.query.internal.marshall.QueryMarshallerRegistry;
36+
37+
38+
public class ListQueryMarshallerTest {
39+
40+
@Test
41+
public void localeSetAsNe_ParsedCorrectly() {
42+
Locale defaultLocale = Locale.getDefault();
43+
Locale.setDefault(new Locale("ne"));
44+
45+
ListQueryMarshaller marshaller = ListQueryMarshaller.ec2Query();
46+
47+
QueryMarshallerContext context = createTestContext();
48+
List<String> testList = Arrays.asList("value1", "value2");
49+
SdkField<List<?>> field = createTestField();
50+
51+
marshaller.marshall(context, "TestParam", testList, field);
52+
53+
Map<String, List<String>> params = context.request().rawQueryParameters();
54+
55+
assertEquals(Collections.singletonList("value1"), params.get("TestParam.1"));
56+
assertEquals(Collections.singletonList("value2"), params.get("TestParam.2"));
57+
Locale.setDefault(defaultLocale);
58+
}
59+
60+
private QueryMarshallerContext createTestContext() {
61+
62+
QueryMarshaller<String> stringMarshaller = (context, path, val, field) ->
63+
context.request().putRawQueryParameter(path, val);
64+
65+
QueryMarshallerRegistry registry = QueryMarshallerRegistry.builder()
66+
.marshaller(MarshallingType.STRING, stringMarshaller)
67+
.build();
68+
69+
return QueryMarshallerContext.builder()
70+
.request(SdkHttpFullRequest.builder())
71+
.marshallerRegistry(registry)
72+
.build();
73+
}
74+
75+
76+
77+
private SdkField<List<?>> createTestField() {
78+
SdkField<?> memberField = SdkField.builder(MarshallingType.STRING)
79+
.memberName("item")
80+
.traits(LocationTrait.builder()
81+
.location(MarshallLocation.PAYLOAD)
82+
.locationName("item")
83+
.build())
84+
.build();
85+
86+
ListTrait listTrait = ListTrait.builder()
87+
.memberFieldInfo(memberField)
88+
.memberLocationName("item")
89+
.isFlattened(true)
90+
.build();
91+
92+
return SdkField.builder(MarshallingType.LIST)
93+
.memberName("TestParam")
94+
.traits(LocationTrait.builder()
95+
.location(MarshallLocation.QUERY_PARAM)
96+
.locationName("TestParam")
97+
.build(),listTrait)
98+
.build();
99+
}
100+
}

0 commit comments

Comments
 (0)