Skip to content

Commit 3d71912

Browse files
authored
Add x-amzn-query-mode to http header when service has @awsQueryCompatible trait (#5707)
* Added hasAWSQueryCompatible field to the marshaller to determine if add "x-amzn-query-mode" to http header * lines adjusted * test case adjusted to also test BaseAwsJsonProtocolFactory * lines adjusted
1 parent fb1dca7 commit 3d71912

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

core/protocols/aws-json-protocol/src/main/java/software/amazon/awssdk/protocols/json/BaseAwsJsonProtocolFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ public final ProtocolMarshaller<SdkHttpFullRequest> createProtocolMarshaller(Ope
236236
.operationInfo(operationInfo)
237237
.sendExplicitNullForPayload(false)
238238
.protocolMetadata(protocolMetadata)
239+
.hasAwsQueryCompatible(hasAwsQueryCompatible)
239240
.build();
240241
}
241242

core/protocols/aws-json-protocol/src/main/java/software/amazon/awssdk/protocols/json/internal/marshall/JsonProtocolMarshaller.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ public class JsonProtocolMarshaller implements ProtocolMarshaller<SdkHttpFullReq
7272
private final JsonMarshallerContext marshallerContext;
7373
private final boolean hasEventStreamingInput;
7474
private final boolean hasEvent;
75+
private final boolean hasAwsQueryCompatible;
7576

7677
JsonProtocolMarshaller(URI endpoint,
7778
StructuredJsonGenerator jsonGenerator,
7879
String contentType,
7980
OperationInfo operationInfo,
80-
AwsJsonProtocolMetadata protocolMetadata) {
81+
AwsJsonProtocolMetadata protocolMetadata,
82+
boolean hasAwsQueryCompatible) {
8183
this.endpoint = endpoint;
8284
this.jsonGenerator = jsonGenerator;
8385
this.contentType = contentType;
@@ -88,6 +90,7 @@ public class JsonProtocolMarshaller implements ProtocolMarshaller<SdkHttpFullReq
8890
this.hasEventStreamingInput = operationInfo.hasEventStreamingInput();
8991
this.hasEvent = operationInfo.hasEvent();
9092
this.request = fillBasicRequestParams(operationInfo);
93+
this.hasAwsQueryCompatible = hasAwsQueryCompatible;
9194
this.marshallerContext = JsonMarshallerContext.builder()
9295
.jsonGenerator(jsonGenerator)
9396
.marshallerRegistry(MARSHALLER_REGISTRY)
@@ -292,6 +295,10 @@ private SdkHttpFullRequest finishMarshalling() {
292295
}
293296
}
294297

298+
if (hasAwsQueryCompatible) {
299+
request.putHeader("x-amzn-query-mode", "true");
300+
}
301+
295302
return request.build();
296303
}
297304

core/protocols/aws-json-protocol/src/main/java/software/amazon/awssdk/protocols/json/internal/marshall/JsonProtocolMarshallerBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public final class JsonProtocolMarshallerBuilder {
3535
private OperationInfo operationInfo;
3636
private boolean sendExplicitNullForPayload;
3737
private AwsJsonProtocolMetadata protocolMetadata;
38+
private boolean hasAwsQueryCompatible = false;
3839

3940
private JsonProtocolMarshallerBuilder() {
4041
}
@@ -102,6 +103,14 @@ public JsonProtocolMarshallerBuilder protocolMetadata(AwsJsonProtocolMetadata pr
102103
return this;
103104
}
104105

106+
/**
107+
* @param hasAwsQueryCompatible True if the service is AWS Query compatible, (has the @awsQueryCompatible trait)
108+
*/
109+
public JsonProtocolMarshallerBuilder hasAwsQueryCompatible(boolean hasAwsQueryCompatible) {
110+
this.hasAwsQueryCompatible = hasAwsQueryCompatible;
111+
return this;
112+
}
113+
105114
/**
106115
* @return New instance of {@link ProtocolMarshaller}. If {@link #sendExplicitNullForPayload} is true then the marshaller
107116
* will be wrapped with {@link NullAsEmptyBodyProtocolRequestMarshaller}.
@@ -111,7 +120,8 @@ public ProtocolMarshaller<SdkHttpFullRequest> build() {
111120
jsonGenerator,
112121
contentType,
113122
operationInfo,
114-
protocolMetadata);
123+
protocolMetadata,
124+
hasAwsQueryCompatible);
115125
return sendExplicitNullForPayload ? protocolMarshaller
116126
: new NullAsEmptyBodyProtocolRequestMarshaller(protocolMarshaller);
117127
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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.json;
17+
18+
import java.net.URI;
19+
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.Map;
22+
import org.junit.jupiter.api.Test;
23+
import software.amazon.awssdk.core.ClientEndpointProvider;
24+
import software.amazon.awssdk.core.SdkField;
25+
import software.amazon.awssdk.core.SdkPojo;
26+
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
27+
import software.amazon.awssdk.core.client.config.SdkClientOption;
28+
import software.amazon.awssdk.http.SdkHttpFullRequest;
29+
import software.amazon.awssdk.http.SdkHttpMethod;
30+
import software.amazon.awssdk.protocols.core.OperationInfo;
31+
import software.amazon.awssdk.protocols.core.ProtocolMarshaller;
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
public class AWSQueryModeTest {
35+
36+
private static final OperationInfo EMPTY_OPERATION_INFO = OperationInfo.builder()
37+
.httpMethod(SdkHttpMethod.POST)
38+
.hasImplicitPayloadMembers(true)
39+
.build();
40+
41+
/*
42+
* A simple test POJO to marshall
43+
*/
44+
private static final class TestPojo implements SdkPojo {
45+
46+
private TestPojo() {}
47+
48+
@Override
49+
public List<SdkField<?>> sdkFields() {
50+
return Collections.emptyList();
51+
}
52+
53+
@Override
54+
public boolean equalsBySdkFields(Object other) {
55+
if (!(other instanceof TestPojo)) {
56+
return false;
57+
}
58+
return true;
59+
}
60+
61+
@Override
62+
public Map<String, SdkField<?>> sdkFieldNameToField() {
63+
return Collections.emptyMap();
64+
}
65+
66+
}
67+
68+
@Test
69+
public void testMarshallWithAwsQueryCompatibleTrue() {
70+
SdkClientConfiguration clientConfig =
71+
SdkClientConfiguration.builder()
72+
.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER,
73+
ClientEndpointProvider.forEndpointOverride(URI.create("http://localhost")))
74+
.build();
75+
AwsJsonProtocolFactory factory =
76+
AwsJsonProtocolFactory.builder()
77+
.clientConfiguration(clientConfig)
78+
.protocolVersion("1.1")
79+
.protocol(AwsJsonProtocol.AWS_JSON)
80+
.hasAwsQueryCompatible(true)
81+
.build();
82+
83+
ProtocolMarshaller<SdkHttpFullRequest> marshaller = factory.createProtocolMarshaller(EMPTY_OPERATION_INFO);
84+
SdkPojo testPojo = new TestPojo();
85+
86+
SdkHttpFullRequest result = marshaller.marshall(testPojo);
87+
88+
assertThat(result.headers()).containsKey("x-amzn-query-mode");
89+
assertThat(result.headers().get("x-amzn-query-mode").get(0)).isEqualTo("true");
90+
}
91+
92+
@Test
93+
public void testMarshallWithNoAwsQueryCompatible() {
94+
SdkClientConfiguration clientConfig =
95+
SdkClientConfiguration.builder()
96+
.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER,
97+
ClientEndpointProvider.forEndpointOverride(URI.create("http://localhost")))
98+
.build();
99+
AwsJsonProtocolFactory factory =
100+
AwsJsonProtocolFactory.builder()
101+
.clientConfiguration(clientConfig)
102+
.protocolVersion("1.1")
103+
.protocol(AwsJsonProtocol.AWS_JSON)
104+
.build();
105+
106+
ProtocolMarshaller<SdkHttpFullRequest> marshaller = factory.createProtocolMarshaller(EMPTY_OPERATION_INFO);
107+
SdkPojo testPojo = new TestPojo();
108+
109+
SdkHttpFullRequest result = marshaller.marshall(testPojo);
110+
111+
assertThat(result.headers()).doesNotContainKey("x-amzn-query-mode");
112+
}
113+
114+
}

0 commit comments

Comments
 (0)