Skip to content

Commit

Permalink
Avoid clobbering values with an empty element.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilkington committed Aug 1, 2018
1 parent 52b68dd commit 057ea72
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/XMLCoding/XMLStackParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ internal class _XMLElement {
} else {
node[childElement.key] = newValue
}
} else {
// if the node is empty and there is no existing value
} else if node[childElement.key] == nil {
// an empty node can be treated as an empty dictionary
node[childElement.key] = [:]
}
Expand Down
21 changes: 21 additions & 0 deletions Tests/XMLCodingTests/XMLParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ class XMLParsingTests: XCTestCase {
XCTAssertNil(response.result.message)
}

func testEmptyElementNotEffectingPreviousElement() throws {
let inputString = """
<Response>
<Result>
<Message>message</Message>
</Result>
<Result/>
<Metadata>
<Id>id</Id>
</Metadata>
</Response>
"""

guard let inputData = inputString.data(using: .utf8) else {
return XCTFail()
}

let response = try XMLDecoder().decode(Response.self, from: inputData)

XCTAssertEqual("message", response.result.message)
}

static var allTests = [
("testEmptyElement", testEmptyElement),
Expand Down

0 comments on commit 057ea72

Please sign in to comment.