Skip to content

Commit

Permalink
feat(http): now using xmlRootKey from dest config while building XML …
Browse files Browse the repository at this point in the history
…payload
  • Loading branch information
sandeepdsvs committed Jan 27, 2025
1 parent eac2a2f commit 61e8b18
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/cdk/v2/destinations/http/procWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ steps:
- name: prepareBody
template: |
const payload = $.getCustomMappings(.message, .destination.Config.propertiesMapping);
$.context.payload = $.prepareBody(payload, $.context.format);
$.context.payload = $.prepareBody(payload, $.context.format, .destination.Config.xmlRootKey);
- name: buildResponseForProcessTransformation
template: |
Expand Down
21 changes: 11 additions & 10 deletions src/cdk/v2/destinations/http/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,24 @@ const preprocessJson = (obj) => {
}, {});
};

const getXMLPayload = (payload) => {
const getXMLPayload = (payload, rootKey) => {
const builderOptions = {
ignoreAttributes: false, // Include attributes if they exist
suppressEmptyNode: false, // Ensures that null or undefined values are not omitted
attributeNamePrefix: '@_',
};

if (Object.keys(payload).length !== 1) {
throw new ConfigurationError(
`Error: XML supports only one root key. Please update request body mappings accordingly`,
);
if (!rootKey) {
throw new ConfigurationError(`Error: XML root key is invalid`);

Check warning on line 135 in src/cdk/v2/destinations/http/utils.js

View check run for this annotation

Codecov / codecov/patch

src/cdk/v2/destinations/http/utils.js#L135

Added line #L135 was not covered by tests
}
const rootKey = Object.keys(payload)[0];

const builder = new XMLBuilder(builderOptions);
const processesPayload = preprocessJson(payload);
processesPayload[rootKey]['@_xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
const processesPayload = {
[rootKey]: {
...preprocessJson(payload),
'@_xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
},
};
return `<?xml version="1.0" encoding="UTF-8"?>${builder.build(processesPayload)}`;
};

Expand All @@ -165,11 +166,11 @@ const metadataHeaders = (contentType) => {
}
};

const prepareBody = (payload, contentType) => {
const prepareBody = (payload, contentType, xmlRootKey) => {
let responseBody;
if (contentType === CONTENT_TYPES_MAP.XML && !isEmptyObject(payload)) {
responseBody = {
payload: getXMLPayload(payload),
payload: getXMLPayload(payload, xmlRootKey),
};
} else {
responseBody = removeUndefinedAndNullValues(payload);
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/v2/destinations/http/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ describe('Utils Functions', () => {

describe('prepareBody', () => {
test('should prepare XML payload when content type is XML', () => {
const payload = { root: { key: 'value', key2: null } };
const payload = { key: 'value', key2: null };
const expectedXML =
'<?xml version="1.0" encoding="UTF-8"?><root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><key>value</key><key2 xsi:nil></key2></root>';
const result = prepareBody(payload, 'XML');
const result = prepareBody(payload, 'XML', 'root');
expect(result).toEqual({ payload: expectedXML });
});

Expand Down
13 changes: 7 additions & 6 deletions test/integrations/destinations/http/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const destinations: Destination[] = [
bearerToken: 'test-token',
method: 'POST',
format: 'XML',
xmlRootKey: 'body',
headers: [
{
to: '$.h1',
Expand All @@ -168,27 +169,27 @@ const destinations: Destination[] = [
propertiesMapping: [
{
from: '$.event',
to: '$.body.event',
to: '$.event',
},
{
from: '$.properties.currency',
to: '$.body.currency',
to: '$.currency',
},
{
from: '$.userId',
to: '$.body.userId',
to: '$.userId',
},
{
from: '$.properties.products[*].product_id',
to: '$.body.properties.items[*].item_id',
to: '$.properties.items[*].item_id',
},
{
from: '$.properties.products[*].name',
to: '$.body.properties.items[*].name',
to: '$.properties.items[*].name',
},
{
from: '$.properties.products[*].price',
to: '$.body.properties.items[*].price',
to: '$.properties.items[*].price',
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export const configuration: ProcessorTestData[] = [
body: [
{
error:
'Error: XML supports only one root key. Please update request body mappings accordingly: Workflow: procWorkflow, Step: prepareBody, ChildStep: undefined, OriginalError: Error: XML supports only one root key. Please update request body mappings accordingly',
'Error: XML root key is invalid: Workflow: procWorkflow, Step: prepareBody, ChildStep: undefined, OriginalError: Error: XML root key is invalid',
statusCode: 400,
metadata: generateMetadata(1),
statTags: { ...processorInstrumentationErrorStatTags },
Expand Down

0 comments on commit 61e8b18

Please sign in to comment.