Skip to content

Fix failing duplicate identifier detection #375

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
#### Master

* Added support of mutable CellViewModels.
* Fixes failing duplicate identity detection

## [4.0.1](https://github.com/RxSwiftCommunity/RxDataSources/releases/tag/4.0.1)

Expand Down
16 changes: 16 additions & 0 deletions Sources/Differentiator/Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,19 @@ public enum Diff {
dictionary[key] = i
}

var finalKeys = Set<OptimizedIdentity<Identity>>()

for (i, items) in finalItemCache.enumerated() {
for j in 0 ..< items.count {
let item = items[j]
var identity = item.identity
let key = OptimizedIdentity(&identity)

if finalKeys.contains(key) {
throw Error.duplicateItem(item: item)
}
finalKeys.insert(key)

guard let initialItemPathIndex = dictionary[key] else {
continue
Copy link
Author

@teameh teameh Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that if the item was not present in the initial sections, we always continue and it's never checked if the finalSections contain items with duplicate identifiers!

}
Expand Down Expand Up @@ -518,8 +526,16 @@ public enum Diff {
var initialSectionData = ContiguousArray<SectionAssociatedData>(repeating: SectionAssociatedData.initial, count: initialSections.count)
var finalSectionData = ContiguousArray<SectionAssociatedData>(repeating: SectionAssociatedData.initial, count: finalSections.count)

var finalSectionIdentities = Set<Section.Identity>()

for (i, section) in finalSections.enumerated() {
finalSectionData[i].itemCount = finalSections[i].items.count

if finalSectionIdentities.contains(section.identity) {
throw Error.duplicateSection(section: section)
}
finalSectionIdentities.insert(section.identity)

guard let initialSectionIndex = initialSectionIndexes[section.identity] else {
continue
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we need to not only look in the initialSectionIndexes but also store the identity in an extra Set (any better data types for this use case?) and check if the final sections don't contain any duplicates

}
Expand Down
106 changes: 100 additions & 6 deletions Tests/RxDataSourcesTests/AlgorithmTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,77 @@ extension AlgorithmTests {

// errors
extension AlgorithmTests {
func testThrowsErrorOnDuplicateItem() {
let initial: [s] = [
func testThrowsErrorOnDuplicateItem_inInitialSections() {
let sections: [s] = [
s(1, [
i(1111, ""),
i(1111, "")
])
]

do {
_ = try Diff.differencesForSectionedView(initialSections: sections, finalSections: sections)
XCTFail("Should throw exception")
}
catch let exception {
guard case let .duplicateItem(item) = exception as! Diff.Error else {
XCTFail("Not required error")
return
}

XCTAssertEqual(item as! i, i(1111, ""))
}
}

func testThrowsErrorOnDuplicateItem_inFinalSections() {
let final: [s] = [
s(1, [
i(1111, ""),
i(1111, "")
])
]

do {
_ = try Diff.differencesForSectionedView(initialSections: [], finalSections: final)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix this will not throw

XCTFail("Should throw exception")
}
catch let exception {
guard case let .duplicateItem(item) = exception as! Diff.Error else {
XCTFail("Not required error")
return
}

XCTAssertEqual(item as! i, i(1111, ""))
}
}

func testThrowsErrorOnDuplicateItemInDifferentSection_inInitialSections() {
let sections: [s] = [
s(1, [
i(1111, "")
]),
s(2, [
i(1111, "")
])

]

do {
_ = try Diff.differencesForSectionedView(initialSections: sections, finalSections: sections)
XCTFail("Should throw exception")
}
catch let exception {
guard case let .duplicateItem(item) = exception as! Diff.Error else {
XCTFail("Not required error")
return
}

XCTAssertEqual(item as! i, i(1111, ""))
}
}

func testThrowsErrorOnDuplicateItemInDifferentSection_inFinalSections() {
let final: [s] = [
s(1, [
i(1111, "")
]),
Expand All @@ -332,7 +401,7 @@ extension AlgorithmTests {
]

do {
_ = try Diff.differencesForSectionedView(initialSections: initial, finalSections: initial)
_ = try Diff.differencesForSectionedView(initialSections: [], finalSections: final)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix this will not throw

XCTFail("Should throw exception")
}
catch let exception {
Expand All @@ -345,8 +414,33 @@ extension AlgorithmTests {
}
}

func testThrowsErrorOnDuplicateSection() {
let initial: [s] = [
func testThrowsErrorOnDuplicateSection_inInitialSections() {
let sections: [s] = [
s(1, [
i(1111, "")
]),
s(1, [
i(1112, "")
])
]

do {
_ = try Diff.differencesForSectionedView(initialSections: sections, finalSections: sections)
XCTFail("Should throw exception")
}
catch let exception {
guard case let .duplicateSection(section) = exception as! Diff.Error else {
XCTFail("Not required error")
return
}

XCTAssertEqual(section as! s, s(1, [
i(1112, "")
]))
}
}
func testThrowsErrorOnDuplicateSection_inFinalSections() {
let final: [s] = [
s(1, [
i(1111, "")
]),
Expand All @@ -357,7 +451,7 @@ extension AlgorithmTests {
]

do {
_ = try Diff.differencesForSectionedView(initialSections: initial, finalSections: initial)
_ = try Diff.differencesForSectionedView(initialSections: [], finalSections: final)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix this will not throw

XCTFail("Should throw exception")
}
catch let exception {
Expand Down