Skip to content

Fix rest-json marshalling for events #2805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ public boolean hasPayloadMembers() {

public boolean hasImplicitPayloadMembers() {
return !getUnboundMembers().isEmpty() ||
(isEvent() && !getUnboundEventMembers().isEmpty());
hasImplicitEventPayloadMembers();
}

public boolean hasImplicitEventPayloadMembers() {
return isEvent() && !getUnboundEventMembers().isEmpty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected FieldSpec operationInfoField() {
.add(".hasExplicitPayloadMember($L)", shapeModel.isHasPayloadMember() ||
shapeModel.getExplicitEventPayloadMember() != null)
.add(".hasPayloadMembers($L)", shapeModel.hasPayloadMembers())
.add(".hasImplicitPayloadMembers($L)", shapeModel.hasImplicitEventPayloadMembers())
// Adding httpMethod to avoid validation failure while creating the SdkHttpFullRequest
.add(".httpMethod($T.GET)", SdkHttpMethod.class)
.add(".hasEvent(true)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@SdkInternalApi
public class EventOneMarshaller implements Marshaller<EventOne> {
private static final OperationInfo SDK_OPERATION_BINDING = OperationInfo.builder().hasExplicitPayloadMember(false)
.hasPayloadMembers(true).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();
.hasPayloadMembers(true).hasImplicitPayloadMembers(true).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();

private final BaseAwsJsonProtocolFactory protocolFactory;

Expand All @@ -34,11 +34,10 @@ public SdkHttpFullRequest marshall(EventOne eventOne) {
ProtocolMarshaller<SdkHttpFullRequest> protocolMarshaller = protocolFactory
.createProtocolMarshaller(SDK_OPERATION_BINDING);
return protocolMarshaller.marshall(eventOne).toBuilder().putHeader(":message-type", "event")
.putHeader(":event-type", eventOne.sdkEventType().toString()).putHeader(":content-type", protocolFactory.getContentType())
.build();
.putHeader(":event-type", eventOne.sdkEventType().toString())
.putHeader(":content-type", protocolFactory.getContentType()).build();
} catch (Exception e) {
throw SdkClientException.builder().message("Unable to marshall request to JSON: " + e.getMessage()).cause(e).build();
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@SdkInternalApi
public class EventThreeMarshaller implements Marshaller<EventThree> {
private static final OperationInfo SDK_OPERATION_BINDING = OperationInfo.builder().hasExplicitPayloadMember(false)
.hasPayloadMembers(true).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();
.hasPayloadMembers(true).hasImplicitPayloadMembers(true).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();

private final BaseAwsJsonProtocolFactory protocolFactory;

Expand All @@ -41,4 +41,3 @@ public SdkHttpFullRequest marshall(EventThree eventThree) {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@SdkInternalApi
public class EventTwoMarshaller implements Marshaller<EventTwo> {
private static final OperationInfo SDK_OPERATION_BINDING = OperationInfo.builder().hasExplicitPayloadMember(false)
.hasPayloadMembers(true).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();
.hasPayloadMembers(true).hasImplicitPayloadMembers(true).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();

private final BaseAwsJsonProtocolFactory protocolFactory;

Expand All @@ -34,11 +34,10 @@ public SdkHttpFullRequest marshall(EventTwo eventTwo) {
ProtocolMarshaller<SdkHttpFullRequest> protocolMarshaller = protocolFactory
.createProtocolMarshaller(SDK_OPERATION_BINDING);
return protocolMarshaller.marshall(eventTwo).toBuilder().putHeader(":message-type", "event")
.putHeader(":event-type", eventTwo.sdkEventType().toString()).putHeader(":content-type", protocolFactory.getContentType())
.build();
.putHeader(":event-type", eventTwo.sdkEventType().toString())
.putHeader(":content-type", protocolFactory.getContentType()).build();
} catch (Exception e) {
throw SdkClientException.builder().message("Unable to marshall request to JSON: " + e.getMessage()).cause(e).build();
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@SdkInternalApi
public class InputEventMarshaller implements Marshaller<InputEvent> {
private static final OperationInfo SDK_OPERATION_BINDING = OperationInfo.builder().hasExplicitPayloadMember(true)
.hasPayloadMembers(true).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();
.hasPayloadMembers(true).hasImplicitPayloadMembers(false).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();

private final BaseAwsJsonProtocolFactory protocolFactory;

Expand All @@ -41,4 +41,3 @@ public SdkHttpFullRequest marshall(InputEvent inputEvent) {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@SdkInternalApi
public class InputEventTwoMarshaller implements Marshaller<InputEventTwo> {
private static final OperationInfo SDK_OPERATION_BINDING = OperationInfo.builder().hasExplicitPayloadMember(false)
.hasPayloadMembers(true).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();
.hasPayloadMembers(true).hasImplicitPayloadMembers(true).httpMethod(SdkHttpMethod.GET).hasEvent(true).build();

private final BaseAwsJsonProtocolFactory protocolFactory;

Expand All @@ -41,4 +41,3 @@ public SdkHttpFullRequest marshall(InputEventTwo inputEventTwo) {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@
"requestUri": "no-payload"
},
"input": {"shape": "NoPayloadPostRequest"}
},
"TestEventStream": {
"name": "TestEventStream",
"http": {
"method": "POST",
"requestUri": "eventstream"
},
"input": {"shape": "TestEventStreamRequest"}

}
},
"shapes":{
Expand Down Expand Up @@ -163,6 +172,101 @@
},
"documentation":"<p> The request structure for a blob payload request. </p>",
"payload":"data"
},
"TestEventStreamRequest": {
"type": "structure",
"required": [
"InputEventStream"
],
"members": {
"InputEventStream": {
"shape": "InputEventStream"
}
},
"payload":"InputEventStream"
},
"InputEventStream": {
"type": "structure",
"members": {
"BlobAndHeadersEvent": {
"shape": "BlobAndHeadersEvent"
},
"HeadersOnlyEvent": {
"shape": "HeadersOnlyEvent"
},
"ImplicitPayloadAndHeadersEvent": {
"shape": "ImplicitPayloadAndHeadersEvent"
}
},
"eventstream": true
},
"BlobAndHeadersEvent": {
"type": "structure",
"members": {
"BlobPayloadMember": {
"shape":"BlobPayloadMember",
"eventpayload":true
},
"HeaderMember": {
"shape": "String",
"eventheader": true
}
},
"event": true
},
"ImplicitPayloadAndHeadersEvent": {
"type": "structure",
"members": {
"StringMember": {
"shape":"String"
},
"HeaderMember": {
"shape": "String",
"eventheader": true
}
},
"event": true
},
"HeadersOnlyEvent": {
"type": "structure",
"members": {
"HeaderMember": {
"shape": "String",
"eventheader": true
}
},
"event": true
},
"BlobPayloadMember":{"type":"blob"},
"EventStream": {
"type": "structure",
"members": {
"EventOne": {
"shape": "EventOne"
},
"EventTwo": {
"shape": "EventTwo"
}
},
"eventstream": true
},
"EventOne": {
"type": "structure",
"members": {
"Foo": {
"shape": "String"
}
},
"event": true
},
"EventTwo": {
"type": "structure",
"members": {
"Bar": {
"shape": "String"
}
},
"event": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.protocol.tests;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.http.ContentStreamProvider;
import software.amazon.awssdk.http.SdkHttpFullRequest;
import software.amazon.awssdk.protocols.json.AwsJsonProtocol;
import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory;
import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.BlobAndHeadersEvent;
import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.HeadersOnlyEvent;
import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.ImplicitPayloadAndHeadersEvent;
import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.InputEventStream;

import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.ProtocolRestJsonContentTypeException;
import software.amazon.awssdk.services.protocolrestjsoncontenttype.transform.BlobAndHeadersEventMarshaller;
import software.amazon.awssdk.services.protocolrestjsoncontenttype.transform.HeadersOnlyEventMarshaller;
import software.amazon.awssdk.services.protocolrestjsoncontenttype.transform.ImplicitPayloadAndHeadersEventMarshaller;

public class RestJsonEventStreamProtocolTest {
private static final String EVENT_CONTENT_TYPE_HEADER = ":content-type";

@Test
public void implicitPayloadAndHeaders_payloadMemberPresent() {
ImplicitPayloadAndHeadersEventMarshaller marshaller = new ImplicitPayloadAndHeadersEventMarshaller(protocolFactory());

ImplicitPayloadAndHeadersEvent event = InputEventStream.implicitPayloadAndHeadersEventBuilder()
.stringMember("hello rest-json")
.headerMember("hello rest-json")
.build();

SdkHttpFullRequest marshalledEvent = marshaller.marshall(event);

assertThat(marshalledEvent.headers().get(EVENT_CONTENT_TYPE_HEADER)).containsExactly("application/json");

String content = contentAsString(marshalledEvent);
assertThat(content).isEqualTo("{\"StringMember\":\"hello rest-json\"}");
}

@Test
public void implicitPayloadAndHeaders_payloadMemberNotPresent() {
ImplicitPayloadAndHeadersEventMarshaller marshaller = new ImplicitPayloadAndHeadersEventMarshaller(protocolFactory());

ImplicitPayloadAndHeadersEvent event = InputEventStream.implicitPayloadAndHeadersEventBuilder()
.headerMember("hello rest-json")
.build();

SdkHttpFullRequest marshalledEvent = marshaller.marshall(event);

assertThat(marshalledEvent.headers().get(EVENT_CONTENT_TYPE_HEADER)).containsExactly("application/json");

String content = contentAsString(marshalledEvent);
assertThat(content).isEqualTo("{}");
}

@Test
public void blobAndHeadersEvent() {
BlobAndHeadersEventMarshaller marshaller = new BlobAndHeadersEventMarshaller(protocolFactory());

BlobAndHeadersEvent event = InputEventStream.blobAndHeadersEventBuilder()
.headerMember("hello rest-json")
.blobPayloadMember(SdkBytes.fromUtf8String("hello rest-json"))
.build();

SdkHttpFullRequest marshalledEvent = marshaller.marshall(event);

assertThat(marshalledEvent.headers().get(EVENT_CONTENT_TYPE_HEADER)).containsExactly("application/octet-stream");

String content = contentAsString(marshalledEvent);
assertThat(content).isEqualTo("hello rest-json");
}

@Test
public void headersOnly() {
HeadersOnlyEventMarshaller marshaller = new HeadersOnlyEventMarshaller(protocolFactory());

HeadersOnlyEvent event = InputEventStream.headersOnlyEventBuilder()
.headerMember("hello rest-json")
.build();

SdkHttpFullRequest marshalledEvent = marshaller.marshall(event);

assertThat(marshalledEvent.headers().keySet()).doesNotContain(EVENT_CONTENT_TYPE_HEADER);

String content = contentAsString(marshalledEvent);
assertThat(content).isEqualTo("");
}

private static AwsJsonProtocolFactory protocolFactory() {
return AwsJsonProtocolFactory.builder()
.clientConfiguration(
SdkClientConfiguration.builder()
.option(SdkClientOption.ENDPOINT,
URI.create("https://test.aws.com"))
.build())
.defaultServiceExceptionSupplier(ProtocolRestJsonContentTypeException::builder)
.protocol(AwsJsonProtocol.REST_JSON)
.protocolVersion("1.1")
.build();
}

private static String contentAsString(SdkHttpFullRequest request) {
return request.contentStreamProvider()
.map(ContentStreamProvider::newStream)
.map(s -> SdkBytes.fromInputStream(s).asString(StandardCharsets.UTF_8))
.orElse(null);
}
}