Skip to content

Commit eae5b32

Browse files
committed
XML parser now initializes child-list elements as empty arrays.
Previously, a child element that was described as a list but that did not appear in the XML response would not be set in the response data. This makes them more consistent, always getting initalized to an empty array, reguardless if the list child is in the XML.
1 parent c6a85e9 commit eae5b32

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/xml/parser.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,24 @@ AWS.XML.Parser = inherit({
5555
} else if (error) {
5656
throw AWS.util.error(error, {code: 'XMLParserError'});
5757
} else { // empty xml document
58-
return {};
58+
return this.parseStructure({}, this.rules);
5959
}
6060

6161
},
6262

6363
parseStructure: function parseStructure(structure, rules) {
6464
var data = {};
65-
AWS.util.each.call(this, structure, function (name, value) {
66-
var rule = rules[name] || {};
67-
data[rule.name || name] = this.parseMember(value, rule);
65+
66+
// force array members to always be present
67+
AWS.util.each.call(this, rules, function(memberName, memberRules) {
68+
if (memberRules.type == 'list') {
69+
data[memberRules.name || memberName] = [];
70+
}
71+
});
72+
73+
AWS.util.each.call(this, structure, function (xmlName, value) {
74+
var rule = rules[xmlName] || {};
75+
data[rule.name || xmlName] = this.parseMember(value, rule);
6876
});
6977

7078
return data;

test/unit/xml/parser.spec.coffee

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ describe 'AWS.XML.Parser', ->
8686
parse xml, rules, (data) ->
8787
expect(data).toEqual({items:[]})
8888

89+
it 'returns missing lists as []', ->
90+
xml = '<xml></xml>'
91+
rules =
92+
type: 'structure'
93+
members:
94+
items:
95+
type: 'list'
96+
members:
97+
type: 'string'
98+
parse xml, rules, (data) ->
99+
expect(data).toEqual({items:[]})
100+
89101
it 'Converts xml lists of strings into arrays of strings', ->
90102
xml = """
91103
<xml>

0 commit comments

Comments
 (0)