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: decode escaped XML #952

Merged
merged 3 commits into from
Feb 28, 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 @@ -29,6 +29,7 @@
import software.amazon.smithy.model.traits.XmlNamespaceTrait;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;
import software.amazon.smithy.utils.IoUtils;

/**
* Utility methods for generating AWS protocols.
Expand Down Expand Up @@ -112,6 +113,9 @@ static void generateJsonParseBody(GenerationContext context) {
static void generateXmlParseBody(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

// Include function that decodes XML escape characters.
writer.write(IoUtils.readUtf8Resource(AwsProtocolUtils.class, "decodeEscapedXML.ts"));

// Include an XML body parser used to deserialize documents from HTTP responses.
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.addDependency(AwsDependency.XML_PARSER);
Expand All @@ -120,7 +124,8 @@ static void generateXmlParseBody(GenerationContext context) {
writer.openBlock("return collectBodyString(streamBody, context).then(encoded => {", "});", () -> {
writer.openBlock("if (encoded.length) {", "}", () -> {
writer.write("const parsedObj = xmlParse(encoded, { attributeNamePrefix: '', "
+ "ignoreAttributes: false, parseNodeValue: false });");
+ "ignoreAttributes: false, parseNodeValue: false, tagValueProcessor: (val, tagName) "
+ "=> decodeEscapedXML(val) });");
writer.write("return parsedObj[Object.keys(parsedObj)[0]];");
});
writer.write("return {};");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const decodeEscapedXML = (str: string) => {
return str
.replace(/&/g, "&")
.replace(/'/g, "'")
.replace(/"/g, '"')
.replace(/>/g, ">")
.replace(/&lt;/g, "<");
};