Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
Use only first item in array, when root element is an array
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Sep 29, 2018
1 parent a942838 commit 3af7997
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
40 changes: 40 additions & 0 deletions lib/adapter/serialize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import soapResponseFlightWithoutHead from '../../tests/helpers/soap5'

import serialize from './serialize'

// Setup

const bossIdVendorSoap = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<CardNew xmlns="http://example.com/webservices/">
<customerkey>AD0B2E69-4640-FBF4-9CC7-0C713FDF3B11</customerkey>
<rfid>04793182CB4884</rfid>
<installationid>138</installationid>
</CardNew>
</soap:Body>
</soap:Envelope>`

// Tests

test('should return soap string from object', async (t) => {
const data = {
GetPaymentMethodsResponse: {
Expand Down Expand Up @@ -36,6 +51,31 @@ test('should return soap string from object', async (t) => {
t.deepEqual(ret, expected)
})

test('should use only first element when root element is an array', async (t) => {
const data = {
CardNew: [{
customerkey: 'AD0B2E69-4640-FBF4-9CC7-0C713FDF3B11',
rfid: '04793182CB4884',
installationid: '138'
}]
}
const endpoint = {
uri: 'http://api2.test/Economy/InvoiceOrder/V001/InvoiceService.asmx',
namespace: 'http://example.com/webservices/',
soap: { version: '1.2' }
}
const request = {
method: 'GET',
data,
endpoint
}
const expectedData = bossIdVendorSoap

const ret = await serialize(request)

t.deepEqual(ret.data, expectedData)
})

test('should set object at path', async (t) => {
const data = {
PaymentMethod: [
Expand Down
7 changes: 4 additions & 3 deletions lib/adapter/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ const prepareProp = (element, props, key, namespaces) => {
const prefix = extractNamespacePrefix(key)
if (namespaces[prefix]) {
const { [prefix]: namespace, ...rest } = namespaces
const theElement = (Array.isArray(element[key])) ? element[key][0] : element[key]
return {
...props,
[key]: prepareObject({
...element[key],
_attributes: setAttribute(element[key], generateNamespaceKey(prefix), namespace)
...theElement,
_attributes: setAttribute(theElement, generateNamespaceKey(prefix), namespace)
}, rest)
}
}
Expand All @@ -60,7 +61,7 @@ const prepareObject = (element, namespaces) => {
return element
}

return Object.keys(element).reduce((el, key) => prepareProp(element, el, key, namespaces), {})
return Object.keys(element).reduce((props, key) => prepareProp(element, props, key, namespaces), {})
}

const prepareData = (data, path, namespace, { prefix = 'soap', version = '1.1' }) => {
Expand Down

0 comments on commit 3af7997

Please sign in to comment.