Skip to content

Commit

Permalink
XML parser now initializes child-list elements as empty arrays.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
trevorrowe committed Feb 20, 2013
1 parent c6a85e9 commit eae5b32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/xml/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ AWS.XML.Parser = inherit({
} else if (error) {
throw AWS.util.error(error, {code: 'XMLParserError'});
} else { // empty xml document
return {};
return this.parseStructure({}, this.rules);
}

},

parseStructure: function parseStructure(structure, rules) {
var data = {};
AWS.util.each.call(this, structure, function (name, value) {
var rule = rules[name] || {};
data[rule.name || name] = this.parseMember(value, rule);

// force array members to always be present
AWS.util.each.call(this, rules, function(memberName, memberRules) {
if (memberRules.type == 'list') {
data[memberRules.name || memberName] = [];
}
});

AWS.util.each.call(this, structure, function (xmlName, value) {
var rule = rules[xmlName] || {};
data[rule.name || xmlName] = this.parseMember(value, rule);
});

return data;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/xml/parser.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ describe 'AWS.XML.Parser', ->
parse xml, rules, (data) ->
expect(data).toEqual({items:[]})

it 'returns missing lists as []', ->
xml = '<xml></xml>'
rules =
type: 'structure'
members:
items:
type: 'list'
members:
type: 'string'
parse xml, rules, (data) ->
expect(data).toEqual({items:[]})

it 'Converts xml lists of strings into arrays of strings', ->
xml = """
<xml>
Expand Down

0 comments on commit eae5b32

Please sign in to comment.