Skip to content

Bugfix/nested array #444

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions src/builder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,24 @@ class exports.Builder

# Case #3 Array data
else if Array.isArray child
# Simple-type children (numbers and strings) and complex children (objects and arrays) are converted
# differently. Arrays of simple children will repeat the parent element, e.g. `<foo>1</foo><foo>2</foo>`,
# while complex child arrays will be wrapped in their parent, e.g.
# `<messages><message type="foo"/message>fuz<message type="bar"/>baz</messages>`
for own index, entry of child
if typeof entry is 'string'
if typeof entry isnt 'object'
# single element, just append it as text
if @options.cdata && requiresCDATA entry
element = element.ele(key).raw(wrapCDATA entry).up()
else
element = element.ele(key, entry).up()
else
element = render(element.ele(key), entry).up()
if (!arrayElement)
arrayElement = element.ele(key)
render(arrayElement, entry)
if (arrayElement)
element = arrayElement.up()
arrayElement = null

# Case #4 Objects
else if typeof child is "object"
Expand Down
53 changes: 53 additions & 0 deletions test/builder.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,38 @@ module.exports =
diffeq expected, actual
test.finish()

'test attribute rendering': (test) ->
expected = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml foo="bar">
<MsgId>10</MsgId>
</xml>

"""
opts = cdata: true
builder = new xml2js.Builder opts
obj = {"xml":{
"$": {"foo": "bar"}
"MsgId":10}}
actual = builder.buildObject obj
diffeq expected, actual
test.finish()

'test nested attribute rendering': (test) ->
expected = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<MsgId foo="bar">10</MsgId>
</xml>

"""
opts = cdata: true
builder = new xml2js.Builder opts
obj = {"xml":{"MsgId":{"$": {"foo": "bar"}, "_": 10}}}
actual = builder.buildObject obj
diffeq expected, actual
test.finish()

'test does not error on array values when checking for cdata': (test) ->
expected = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Expand Down Expand Up @@ -281,3 +313,24 @@ module.exports =
actual = builder.buildObject obj
diffeq expected, actual
test.finish()

'test building obj with nested array': (test) ->
expected = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request>
<headers>
<header method="INVITE" name="X-Mode" value="standard"/>
<header method="INVITE" name="X-Prop" value="default"/>
</headers>
</request>

"""
opts = cdata: true
builder = new xml2js.Builder opts
obj = {request: { headers: [
{ header: { '$': { 'method': 'INVITE', name: 'X-Mode', value: 'standard' } } },
{ header: { '$': { 'method': 'INVITE', name: 'X-Prop', value: 'default' } } }
]}}
actual = builder.buildObject obj
diffeq expected, actual
test.finish()