Skip to content

Commit 8b19c65

Browse files
committed
chore(codegen): use lib serde helpers instead of codegen
1 parent 66a6fd9 commit 8b19c65

File tree

8 files changed

+85
-89
lines changed

8 files changed

+85
-89
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,7 @@ static void generateDocumentBodyShapeSerde(
108108
*/
109109
static void generateJsonParseBody(GenerationContext context) {
110110
TypeScriptWriter writer = context.getWriter();
111-
112-
// Include a JSON body parser used to deserialize documents from HTTP responses.
113-
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
114-
writer.openBlock("const parseBody = (streamBody: any, context: __SerdeContext): "
115-
+ "any => collectBodyString(streamBody, context).then(encoded => {", "});", () -> {
116-
writer.openBlock("if (encoded.length) {", "}", () -> {
117-
writer.write("return JSON.parse(encoded);");
118-
});
119-
writer.write("return {};");
120-
});
121-
122-
writer.write("");
111+
writer.addImport("parseJsonBody", "parseBody", AwsDependency.AWS_SDK_CORE);
123112
}
124113

125114
static void generateJsonParseBodyWithQueryHeader(GenerationContext context) {
@@ -137,17 +126,7 @@ static void generateJsonParseBodyWithQueryHeader(GenerationContext context) {
137126
*/
138127
static void generateJsonParseErrorBody(GenerationContext context) {
139128
TypeScriptWriter writer = context.getWriter();
140-
141-
// Include a JSON body parser used to deserialize documents from HTTP responses.
142-
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
143-
writer.openBlock("const parseErrorBody = async (errorBody: any, context: __SerdeContext) => {",
144-
"}", () -> {
145-
writer.write("const value = await parseBody(errorBody, context);");
146-
writer.write("value.message = value.message ?? value.Message;");
147-
writer.write("return value;");
148-
});
149-
150-
writer.write("");
129+
writer.addImport("parseJsonErrorBody", "parseErrorBody", AwsDependency.AWS_SDK_CORE);
151130
}
152131

153132
/**
@@ -158,36 +137,7 @@ static void generateJsonParseErrorBody(GenerationContext context) {
158137
*/
159138
static void generateXmlParseBody(GenerationContext context) {
160139
TypeScriptWriter writer = context.getWriter();
161-
162-
// Include an XML body parser used to deserialize documents from HTTP responses.
163-
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
164-
writer.addImport("getValueFromTextNode", "__getValueFromTextNode", TypeScriptDependency.AWS_SMITHY_CLIENT);
165-
writer.addDependency(TypeScriptDependency.XML_PARSER);
166-
writer.addImport("XMLParser", null, TypeScriptDependency.XML_PARSER);
167-
writer.openBlock("const parseBody = (streamBody: any, context: __SerdeContext): "
168-
+ "any => collectBodyString(streamBody, context).then(encoded => {", "});", () -> {
169-
writer.openBlock("if (encoded.length) {", "}", () -> {
170-
// Temporararily creating parser inside the function.
171-
// Parser would be moved to runtime config in https://github.com/aws/aws-sdk-js-v3/issues/3979
172-
writer.write("const parser = new XMLParser({ attributeNamePrefix: '', htmlEntities: true, "
173-
+ "ignoreAttributes: false, ignoreDeclaration: true, parseTagValue: false, "
174-
+ "trimValues: false, tagValueProcessor: (_: any, val: any) => "
175-
+ "(val.trim() === '' && val.includes('\\n')) ? '': undefined });");
176-
writer.write("parser.addEntity('#xD', '\\r');");
177-
writer.write("parser.addEntity('#10', '\\n');");
178-
writer.write("const parsedObj = parser.parse(encoded);");
179-
writer.write("const textNodeName = '#text';");
180-
writer.write("const key = Object.keys(parsedObj)[0];");
181-
writer.write("const parsedObjToReturn = parsedObj[key];");
182-
writer.openBlock("if (parsedObjToReturn[textNodeName]) {", "}", () -> {
183-
writer.write("parsedObjToReturn[key] = parsedObjToReturn[textNodeName];");
184-
writer.write("delete parsedObjToReturn[textNodeName];");
185-
});
186-
writer.write("return __getValueFromTextNode(parsedObjToReturn);");
187-
});
188-
writer.write("return {};");
189-
});
190-
writer.write("");
140+
writer.addImport("parseXmlBody", "parseBody", AwsDependency.AWS_SDK_CORE);
191141
}
192142

193143
/**
@@ -198,19 +148,7 @@ static void generateXmlParseBody(GenerationContext context) {
198148
*/
199149
static void generateXmlParseErrorBody(GenerationContext context) {
200150
TypeScriptWriter writer = context.getWriter();
201-
202-
// Include a JSON body parser used to deserialize documents from HTTP responses.
203-
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
204-
writer.openBlock("const parseErrorBody = async (errorBody: any, context: __SerdeContext) => {",
205-
"}", () -> {
206-
writer.write("const value = await parseBody(errorBody, context);");
207-
writer.openBlock("if (value.Error) {", "}", () -> {
208-
writer.write("value.Error.message = value.Error.message ?? value.Error.Message;");
209-
});
210-
writer.write("return value;");
211-
});
212-
213-
writer.write("");
151+
writer.addImport("parseXmlErrorBody", "parseErrorBody", AwsDependency.AWS_SDK_CORE);
214152
}
215153

216154
/**

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsRestXml.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,8 @@ public void generateSharedComponents(GenerationContext context) {
110110
TypeScriptWriter writer = context.getWriter();
111111
writer.addDependency(AwsDependency.XML_BUILDER);
112112

113-
// Generate a function that handles the complex rules around deserializing
114-
// an error code from a rest-xml error.
115-
SymbolReference responseType = getApplicationProtocol().getResponseType();
116-
writer.openBlock("const loadRestXmlErrorCode = (\n"
117-
+ " output: $T,\n"
118-
+ " data: any\n"
119-
+ "): string | undefined => {", "};", responseType, () -> {
120-
// Attempt to fetch the error code from the specific location.
121-
String errorCodeCheckLocation = getErrorBodyLocation(context, "data") + "?.Code";
122-
String errorCodeAccessLocation = getErrorBodyLocation(context, "data") + ".Code";
123-
writer.openBlock("if ($L !== undefined) {", "}", errorCodeCheckLocation, () -> {
124-
writer.write("return $L;", errorCodeAccessLocation);
125-
});
113+
writer.addImport("loadRestXmlErrorCode", null, AwsDependency.AWS_SDK_CORE);
126114

127-
// Default a 404 status code to the NotFound code.
128-
writer.openBlock("if (output.statusCode == 404) {", "}", () -> writer.write("return 'NotFound';"));
129-
});
130-
writer.write("");
131115
writer.write(
132116
context.getStringStore().flushVariableDeclarationCode()
133117
);

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void generateSharedComponents(GenerationContext context) {
107107

108108
TypeScriptWriter writer = context.getWriter();
109109
writer.addUseImports(getApplicationProtocol().getResponseType());
110-
writer.write(IoUtils.readUtf8Resource(getClass(), "load-json-error-code-stub.ts"));
110+
writer.addImport("loadRestJsonErrorCode", null, AwsDependency.AWS_SDK_CORE);
111111

112112
if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
113113
AwsProtocolUtils.generateJsonParseBodyWithQueryHeader(context);

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/RestJsonProtocolGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void generateSharedComponents(GenerationContext context) {
112112
TypeScriptWriter writer = context.getWriter();
113113
writer.addUseImports(getApplicationProtocol().getResponseType());
114114
writer.addImport("take", null, TypeScriptDependency.AWS_SMITHY_CLIENT);
115-
writer.write(IoUtils.readUtf8Resource(getClass(), "load-json-error-code-stub.ts"));
115+
writer.addImport("loadRestJsonErrorCode", null, AwsDependency.AWS_SDK_CORE);
116116

117117
writer.write(
118118
context.getStringStore().flushVariableDeclarationCode()

packages/core/src/protocols/common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { collectBody } from "@smithy/smithy-client";
2+
import type { HttpResponse, SerdeContext } from "@smithy/types";
3+
4+
export const collectBodyString = (streamBody: any, context: SerdeContext): Promise<string> =>
5+
collectBody(streamBody, context).then((body) => context.utf8Encoder(body));

packages/core/src/protocols/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from "./coercing-serializers";
22
export * from "./json/awsExpectUnion";
3+
export * from "./json/parseJsonBody";
4+
export * from "./xml/parseXmlBody";
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
/**
2-
* Load an error code for the aws.rest-json-1.1 protocol.
3-
*/
4-
const loadRestJsonErrorCode = (output: __HttpResponse, data: any): string | undefined => {
1+
import type { HttpResponse, SerdeContext } from "@smithy/types";
2+
3+
import { collectBodyString } from "../common";
4+
5+
export const parseJsonBody = (streamBody: any, context: SerdeContext): any =>
6+
collectBodyString(streamBody, context).then((encoded) => {
7+
if (encoded.length) {
8+
return JSON.parse(encoded);
9+
}
10+
return {};
11+
});
12+
13+
export const parseJsonErrorBody = async (errorBody: any, context: SerdeContext) => {
14+
const value = await parseJsonBody(errorBody, context);
15+
value.message = value.message ?? value.Message;
16+
return value;
17+
};
18+
19+
export const loadRestJsonErrorCode = (output: HttpResponse, data: any): string | undefined => {
520
const findKey = (object: any, key: string) => Object.keys(object).find((k) => k.toLowerCase() === key.toLowerCase());
621

722
const sanitizeErrorCode = (rawValue: string | number): string => {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { getValueFromTextNode } from "@smithy/smithy-client";
2+
import type { HttpResponse, SerdeContext } from "@smithy/types";
3+
import { XMLParser } from "fast-xml-parser";
4+
5+
import { collectBodyString } from "../common";
6+
7+
export const parseXmlBody = (streamBody: any, context: SerdeContext): any =>
8+
collectBodyString(streamBody, context).then((encoded) => {
9+
if (encoded.length) {
10+
const parser = new XMLParser({
11+
attributeNamePrefix: "",
12+
htmlEntities: true,
13+
ignoreAttributes: false,
14+
ignoreDeclaration: true,
15+
parseTagValue: false,
16+
trimValues: false,
17+
tagValueProcessor: (_: any, val: any) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
18+
});
19+
parser.addEntity("#xD", "\r");
20+
parser.addEntity("#10", "\n");
21+
const parsedObj = parser.parse(encoded);
22+
const textNodeName = "#text";
23+
const key = Object.keys(parsedObj)[0];
24+
const parsedObjToReturn = parsedObj[key];
25+
if (parsedObjToReturn[textNodeName]) {
26+
parsedObjToReturn[key] = parsedObjToReturn[textNodeName];
27+
delete parsedObjToReturn[textNodeName];
28+
}
29+
return getValueFromTextNode(parsedObjToReturn);
30+
}
31+
return {};
32+
});
33+
34+
export const parseXmlErrorBody = async (errorBody: any, context: SerdeContext) => {
35+
const value = await parseXmlBody(errorBody, context);
36+
if (value.Error) {
37+
value.Error.message = value.Error.message ?? value.Error.Message;
38+
}
39+
return value;
40+
};
41+
42+
export const loadRestXmlErrorCode = (output: HttpResponse, data: any): string | undefined => {
43+
if (data?.Error?.Code !== undefined) {
44+
return data.Error.Code;
45+
}
46+
if (data?.Code !== undefined) {
47+
return data.Code;
48+
}
49+
if (output.statusCode == 404) {
50+
return "NotFound";
51+
}
52+
};

0 commit comments

Comments
 (0)