Skip to content
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

fix: parse body only once for error response #694

Merged
merged 1 commit into from
Jan 10, 2020
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 @@ -88,8 +88,7 @@ static void generateJsonParseBody(GenerationContext context) {
// Include a JSON body parser used to deserialize documents from HTTP responses.
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.openBlock("const parseBody = (streamBody: any, context: __SerdeContext): any => {", "};", () -> {
writer.openBlock("return context.streamCollector(streamBody).then((body: any) => {", "});", () -> {
writer.write("const encoded = context.utf8Encoder(body);");
writer.openBlock("return collectBodyString(streamBody, context).then(encoded => {", "});", () -> {
writer.openBlock("if (encoded.length) {", "}", () -> {
writer.write("return JSON.parse(encoded);");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
*/
abstract class JsonRpcProtocolGenerator extends HttpRpcProtocolGenerator {

/**
* Creates a AWS JSON RPC protocol generator.
*/
JsonRpcProtocolGenerator() {
super(true);
}

protected Format getDocumentTimestampFormat() {
return Format.EPOCH_SECONDS;
}
Expand Down Expand Up @@ -96,8 +103,8 @@ private DocumentMemberSerVisitor getMemberSerVisitor(GenerationContext context,
@Override
protected void writeErrorCodeParser(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.write("const errorTypeParts: String = data[\"__type\"].split('#');");
// parsedOutput is in scope because HttpRpcProtocolGenerator.isErrorCodeInBody is true
writer.write("const errorTypeParts: String = parsedOutput.body[\"__type\"].split('#');");
writer.write("errorCode = (errorTypeParts[1] === undefined) ? errorTypeParts[0] : errorTypeParts[1];");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
* @see <a href="https://awslabs.github.io/smithy/spec/http.html">Smithy HTTP protocol bindings.</a>
*/
abstract class RestJsonProtocolGenerator extends HttpBindingProtocolGenerator {
/**
* Creates a AWS JSON RPC protocol generator.
*/
RestJsonProtocolGenerator() {
super(false);
}

@Override
protected TimestampFormatTrait.Format getDocumentTimestampFormat() {
Expand Down