Skip to content

Commit

Permalink
Merge pull request #4 from LiveUI/singletonList
Browse files Browse the repository at this point in the history
Fix to handle singleton lists with ListDecodingStrategy.
  • Loading branch information
rafiki270 authored Aug 11, 2018
2 parents f5d46f4 + 7786903 commit 4a53225
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Sources/XMLCoding/Decoder/XMLUnkeyedDecodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ internal struct _XMLUnkeyedDecodingContainer : UnkeyedDecodingContainer {
case .collapseListUsingItemTag(let itemTag):
if container.count == 1,
let itemKeyMap = container[0] as? [AnyHashable: Any],
let list = itemKeyMap[itemTag] as? [Any] {
self.container = list
let entry = itemKeyMap[itemTag] {
// if there are multiple items in the list,
// the entry will be a list
if let list = entry as? [Any] {
self.container = list
} else {
// otherwise it will be the singleton entry;
// place it in a list for the container
self.container = [entry]
}
} else {
self.container = []
}
Expand Down
49 changes: 49 additions & 0 deletions Tests/XMLCodingTests/XMLParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ let LIST_XML = """
</Response>
"""

let SINGLETON_LIST_XML = """
<Response>
<Result />
<MetadataList>
<item>
<Id>id1</Id>
</item>
</MetadataList>
</Response>
"""

class XMLParsingTests: XCTestCase {
struct Result: Codable {
let message: String?
Expand Down Expand Up @@ -130,6 +141,22 @@ class XMLParsingTests: XCTestCase {
XCTAssertEqual(LIST_XML, encodedString)
}

func testSingletonListDecodingWithDefaultStrategy() throws {
guard let inputData = SINGLETON_LIST_XML.data(using: .utf8) else {
return XCTFail()
}

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

XCTAssertEqual(1, response.metadataList.items.count)

// encode the output to make sure we get what we started with
let data = try XMLEncoder().encode(response, withRootKey: "Response")
let encodedString = String(data: data, encoding: .utf8) ?? ""

XCTAssertEqual(SINGLETON_LIST_XML, encodedString)
}

func testListDecodingWithCollapseItemTagStrategy() throws {
guard let inputData = LIST_XML.data(using: .utf8) else {
return XCTFail()
Expand All @@ -150,11 +177,33 @@ class XMLParsingTests: XCTestCase {

XCTAssertEqual(LIST_XML, encodedString)
}

func testSingletonListDecodingWithCollapseItemTagStrategy() throws {
guard let inputData = SINGLETON_LIST_XML.data(using: .utf8) else {
return XCTFail()
}

let decoder = XMLDecoder()
decoder.listDecodingStrategy = .collapseListUsingItemTag("item")
let response = try decoder.decode(ResponseWithCollapsedList.self, from: inputData)

XCTAssertEqual(1, response.metadataList.count)

let encoder = XMLEncoder()
encoder.listEncodingStrategy = .expandListWithItemTag("item")

// encode the output to make sure we get what we started with
let data = try encoder.encode(response, withRootKey: "Response")
let encodedString = String(data: data, encoding: .utf8) ?? ""

XCTAssertEqual(SINGLETON_LIST_XML, encodedString)
}

static var allTests = [
("testEmptyElement", testEmptyElement),
("testEmptyElementNotEffectingPreviousElement", testEmptyElementNotEffectingPreviousElement),
("testListDecodingWithDefaultStrategy", testListDecodingWithDefaultStrategy),
("testSingletonListDecodingWithDefaultStrategy", testSingletonListDecodingWithDefaultStrategy),
("testListDecodingWithCollapseItemTagStrategy", testListDecodingWithCollapseItemTagStrategy)
]
}

0 comments on commit 4a53225

Please sign in to comment.