Skip to content

Commit 7636cb0

Browse files
authored
Add HTML entity encoder/decoder (#333)
1 parent e9ba41e commit 7636cb0

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,9 @@ private String registerBodyComparatorStub(String mediaType) {
459459
return "compareEquivalentJsonBodies(bodyString, r.body.toString())";
460460
case "application/xml":
461461
writer.addDependency(TypeScriptDependency.XML_PARSER);
462+
writer.addDependency(TypeScriptDependency.HTML_ENTITIES);
462463
writer.addImport("parse", "xmlParse", "fast-xml-parser");
464+
writer.addImport("decodeHTML", "decodeHTML", "entities");
463465
additionalStubs.add("protocol-test-xml-stub.ts");
464466
return "compareEquivalentXmlBodies(bodyString, r.body.toString())";
465467
case "application/octet-stream":

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ public enum TypeScriptDependency implements SymbolDependencyContainer {
7575

7676
// Conditionally added when interacting with specific protocol test bodyMediaType values.
7777
AWS_SDK_QUERYSTRING_BUILDER("dependencies", "@aws-sdk/querystring-builder", "3.15.0", false),
78-
XML_PARSER("dependencies", "fast-xml-parser", "3.19.0", false);
78+
79+
// Conditionally added when XML parser needs to be used.
80+
XML_PARSER("dependencies", "fast-xml-parser", "3.19.0", false),
81+
HTML_ENTITIES("dependencies", "entities", "2.2.0", false);
7982

8083
public static final String NORMAL_DEPENDENCY = "dependencies";
8184
public static final String DEV_DEPENDENCY = "devDependencies";

smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/protocol-test-xml-stub.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22
* Returns a map of key names that were un-equal to value objects showing the
33
* discrepancies between the components.
44
*/
5-
const compareEquivalentXmlBodies = (expectedBody: string, generatedBody: string): Object => {
6-
const decodeEscapedXml = (str: string) => {
7-
return str
8-
.replace(/&/g, "&")
9-
.replace(/'/g, "'")
10-
.replace(/"/g, '"')
11-
.replace(/>/g, ">")
12-
.replace(/&lt;/g, "<");
13-
};
14-
5+
const compareEquivalentXmlBodies = (
6+
expectedBody: string,
7+
generatedBody: string
8+
): Object => {
159
const parseConfig = {
16-
attributeNamePrefix: '',
10+
attributeNamePrefix: "",
1711
ignoreAttributes: false,
1812
parseNodeValue: false,
1913
trimValues: false,
20-
tagValueProcessor: (val: any, tagName: any) => val.trim() === "" ? "" : decodeEscapedXml(val)
14+
tagValueProcessor: (val: any, tagName: any) =>
15+
val.trim() === "" ? "" : decodeHTML(val),
2116
};
2217

2318
const parseXmlBody = (body: string) => {
@@ -36,4 +31,4 @@ const compareEquivalentXmlBodies = (expectedBody: string, generatedBody: string)
3631
const generatedParts = parseXmlBody(generatedBody);
3732

3833
return compareParts(expectedParts, generatedParts);
39-
}
34+
};

0 commit comments

Comments
 (0)