-
Notifications
You must be signed in to change notification settings - Fork 578
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(clients): trim values in parsed xml only if result is empty #2194
Conversation
a9b620d
to
38e0f59
Compare
The unit tests fail, as the output returned by fast-xml-parser now contains Codeimport { parse } from "fast-xml-parser";
const xmlData = `<Response>
<Errors>
<Error>
<Code>InvalidGreeting</Code>
<Message>Hi</Message>
</Error>
</Errors>
<RequestId>foo-id</RequestId>
</Response>`;
console.log(
`\noutput without trimValues set: ${JSON.stringify(parse(xmlData), null, 2)}`
);
console.log(
`\noutput with trimValues set to false: ${JSON.stringify(
parse(xmlData, {
trimValues: false,
}),
null,
2
)}`
); Outputoutput without trimValues set: {
"Response": {
"Errors": {
"Error": {
"Code": "InvalidGreeting",
"Message": "Hi"
}
},
"RequestId": "foo-id"
}
}
output with trimValues set to false: {
"Response": {
"#text": "\n\n\n",
"Errors": {
"#text": "\n \n",
"Error": {
"#text": "\n \n \n ",
"Code": "InvalidGreeting",
"Message": "Hi"
}
},
"RequestId": "foo-id"
}
} The trimming of |
Wrote a wrapper function to delete empty text nodes to work on parsed object. Codeimport { parse } from "fast-xml-parser";
const xmlData = `<Response>
<Errors>
<Error>
<Code>InvalidGreeting</Code>
<Message>Hi</Message>
</Error>
</Errors>
<RequestId>foo-id</RequestId>
</Response>`;
console.log(
`\noutput without trimValues set: ${JSON.stringify(parse(xmlData), null, 2)}`
);
const textNodeName = "#text";
const deleteEmptyTextNodes = (data, textNodeName) => {
if (Array.isArray(data)) {
return data.map((item) => deleteEmptyTextNodes(item, textNodeName));
}
if (data.constructor.name !== "Object") {
return data;
}
if (data[textNodeName] && data[textNodeName].trim() === "") {
delete data[textNodeName];
}
return Object.entries(data).reduce(
(acc, [key, value]) => ({
...acc,
[key]: deleteEmptyTextNodes(value, textNodeName),
}),
{}
);
};
console.log(
`\noutput with trimValues set to false with wrapper: ${JSON.stringify(
deleteEmptyTextNodes(
parse(xmlData, {
trimValues: false,
}),
textNodeName
),
null,
2
)}`
); Outputoutput without trimValues set: {
"Response": {
"Errors": {
"Error": {
"Code": "InvalidGreeting",
"Message": "Hi"
}
},
"RequestId": "foo-id"
}
}
output with trimValues set to false with wrapper: {
"Response": {
"Errors": {
"Error": {
"Code": "InvalidGreeting",
"Message": "Hi"
}
},
"RequestId": "foo-id"
}
} |
An easier solution would be to add the following code in tagValueProcessor: if (val.trim() === "") {
return "";
} This way, value will be trimmed only if it's empty, and no additional processing of parsed object would be required. |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Codecov Report
@@ Coverage Diff @@
## main #2194 +/- ##
=======================================
Coverage ? 61.83%
=======================================
Files ? 453
Lines ? 23287
Branches ? 5520
=======================================
Hits ? 14399
Misses ? 8888
Partials ? 0 Continue to review full report at Codecov.
|
Verified that integration tests are successful: $ yarn test:integration-legacy
yarn run v1.22.5
$ cucumber-js --fail-fast
...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
150 scenarios (150 passed)
523 steps (523 passed)
1m30.715s
Done in 95.50s.
$ yarn test:integration
yarn run v1.22.5
$ jest --config jest.config.integ.js --passWithNoTests
PASS clients/client-transcribe-streaming/test/index.integ.spec.ts (31.71 s)
TranscribeStream client
✓ should stream the transcript (28978 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 32.169 s
Ran all test suites.
Done in 32.83s. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread. |
Issue
Fixes: #1893
Description
Sets trimValues=false in fast-xml-parser configuration, so that values are not trimmed while parsing.
Testing
CI
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.