Skip to content

Commit 6ccde63

Browse files
committed
Fixing content-type header for JSON services
1 parent d649c9f commit 6ccde63

File tree

7 files changed

+59
-12
lines changed

7 files changed

+59
-12
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
@SdkProtectedApi
2525
public class DefaultJsonContentTypeResolver implements JsonContentTypeResolver {
2626

27+
private static final String REST_JSON_CONTENT_TYPE = "application/json";
28+
2729
private final String prefix;
2830

2931
public DefaultJsonContentTypeResolver(String prefix) {
@@ -32,6 +34,11 @@ public DefaultJsonContentTypeResolver(String prefix) {
3234

3335
@Override
3436
public String resolveContentType(AwsJsonProtocolMetadata protocolMetadata) {
35-
return prefix + protocolMetadata.protocolVersion();
37+
switch (protocolMetadata.protocol()) {
38+
case REST_JSON:
39+
return REST_JSON_CONTENT_TYPE;
40+
default:
41+
return prefix + protocolMetadata.protocolVersion();
42+
}
3643
}
3744
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public interface StructuredJsonGenerator {
3232
*/
3333
StructuredJsonGenerator NO_OP = new StructuredJsonGenerator() {
3434

35-
private final byte[] emptyBytes = new byte[0];
36-
3735
@Override
3836
public StructuredJsonGenerator writeStartArray() {
3937
return this;
@@ -126,7 +124,7 @@ public StructuredJsonGenerator writeNumber(String number) {
126124

127125
@Override
128126
public byte[] getBytes() {
129-
return emptyBytes;
127+
return null;
130128
}
131129

132130
@Override

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,12 @@ private SdkHttpFullRequest finishMarshalling() {
201201
}
202202

203203
byte[] content = jsonGenerator.getBytes();
204-
request.contentStreamProvider(() -> new ByteArrayInputStream(content));
205-
if (content.length > 0) {
206-
request.putHeader(CONTENT_LENGTH, Integer.toString(content.length));
204+
205+
if (content != null) {
206+
request.contentStreamProvider(() -> new ByteArrayInputStream(content));
207+
if (content.length > 0) {
208+
request.putHeader(CONTENT_LENGTH, Integer.toString(content.length));
209+
}
207210
}
208211
}
209212

@@ -214,7 +217,7 @@ private SdkHttpFullRequest finishMarshalling() {
214217
if (!request.headers().containsKey(CONTENT_TYPE) && !hasEvent) {
215218
if (hasEventStreamingInput) {
216219
request.putHeader(CONTENT_TYPE, MIMETYPE_EVENT_STREAM);
217-
} else if (contentType != null && !hasStreamingInput) {
220+
} else if (contentType != null && !hasStreamingInput && request.contentStreamProvider() != null) {
218221
request.putHeader(CONTENT_TYPE, contentType);
219222
}
220223
}

services/dynamodb/src/test/java/software/amazon/awssdk/services/dynamodb/PutItemRequestMarshallerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
2828
import software.amazon.awssdk.core.client.config.SdkClientOption;
2929
import software.amazon.awssdk.http.SdkHttpFullRequest;
30+
import software.amazon.awssdk.protocols.json.AwsJsonProtocol;
3031
import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory;
3132
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
3233
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
@@ -44,6 +45,7 @@ public class PutItemRequestMarshallerTest {
4445
.option(SdkClientOption.ENDPOINT, URI.create("http://localhost"))
4546
.build())
4647
.protocolVersion("1.1")
48+
.protocol(AwsJsonProtocol.AWS_JSON)
4749
.build());
4850

4951
/**l

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,24 @@
469469
}
470470
}
471471
},
472+
{
473+
"description": "GET operation does not have content-type header",
474+
"given": {
475+
"input": {
476+
}
477+
},
478+
"when": {
479+
"action": "marshall",
480+
"operation": "MembersInQueryParams"
481+
},
482+
"then": {
483+
"serializedAs": {
484+
"headers": {
485+
"doesNotContain": [ "content-type" ]
486+
}
487+
}
488+
}
489+
},
472490
{
473491
"description": "Input with greedy label in path",
474492
"given": {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@
3737
}
3838
}
3939
},
40+
{
41+
"description": "Content type header is set to application/json for a POST request",
42+
"when": {
43+
"action": "marshall",
44+
"operation": "AllTypes"
45+
},
46+
"then": {
47+
"serializedAs": {
48+
"headers": {
49+
"contains": {
50+
"content-type": "application/json"
51+
}
52+
}
53+
}
54+
}
55+
},
4056
{
4157
"description": "Operation with structure member explicitly marked as the payload is serialized as unwrapped JSON",
4258
"given": {

test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/EventTransformTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import software.amazon.awssdk.http.AbortableInputStream;
3030
import software.amazon.awssdk.http.SdkHttpFullRequest;
3131
import software.amazon.awssdk.http.SdkHttpFullResponse;
32+
import software.amazon.awssdk.protocols.json.AwsJsonProtocol;
3233
import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory;
3334
import software.amazon.awssdk.protocols.json.JsonOperationMetadata;
3435
import software.amazon.awssdk.services.protocolrestjson.model.InputEvent;
@@ -46,10 +47,12 @@ public class EventTransformTest {
4647
@BeforeClass
4748
public static void setup() {
4849
protocolFactory = AwsJsonProtocolFactory.builder()
49-
.clientConfiguration(SdkClientConfiguration.builder()
50-
.option(SdkClientOption.ENDPOINT, URI.create("http://foo.amazonaws.com"))
51-
.build())
52-
.build();
50+
.clientConfiguration(
51+
SdkClientConfiguration.builder()
52+
.option(SdkClientOption.ENDPOINT, URI.create("http://foo.amazonaws.com"))
53+
.build())
54+
.protocol(AwsJsonProtocol.AWS_JSON)
55+
.build();
5356
}
5457

5558
@Test

0 commit comments

Comments
 (0)