Skip to content
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
12 changes: 12 additions & 0 deletions Sources/Knit/DuplicateRegistrationDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ public final class DuplicateRegistrationDetector {
/// Useful to check that the count of this set is non-zero to confirm the Detector was set up correctly.
public private(set) var existingRegistrations = Set<Key>()

/// Service types that should be ignored from this check.
/// This should not be used for production environments but may be needed for test configuraitons
public var ignoredServices: [Any.Type]

public init(
ignoredServices: [Any.Type] = [],
duplicateWasDetected: ((Key) -> Void)? = nil
Copy link
Copy Markdown
Contributor

@bradfol bradfol Oct 28, 2025

Choose a reason for hiding this comment

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

The detector currently keeps all duplicates on the detectedKeys array. Right now cash-ios is just checking duplicateDetection.detectedKeys.isEmpty but we could be ignoring keys from that list with the existing APIs. Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That works. Will close this PR and do it that way

) {
self.ignoredServices = ignoredServices
self.duplicateWasDetected = duplicateWasDetected
}

Expand All @@ -51,6 +57,12 @@ extension DuplicateRegistrationDetector: Behavior {
name: name
)

// Skip any service types which are expected to have duplicates for testing configurations
let isIgnored = ignoredServices.contains { $0 == type }
if isIgnored {
return
}

let preInsertCount = existingRegistrations.count
existingRegistrations.insert(key)

Expand Down
23 changes: 23 additions & 0 deletions Tests/KnitTests/DuplicateRegistrationDetectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,29 @@ final class DuplicateRegistrationDetectorTests: XCTestCase {
XCTAssertEqual(duplicateRegistrationDetector.detectedKeys.count, 1)
}

func testIgnoredServices() throws {
var reportedDuplicates = [DuplicateRegistrationDetector.Key]()
let duplicateRegistrationDetector = DuplicateRegistrationDetector(
ignoredServices: [
String.self,
]
) {
reportedDuplicates.append($0)
}
let container = SwinjectContainer(
behaviors: [duplicateRegistrationDetector]
)

container.register(String.self, factory: { _ in "one" })
container.register(Int.self, factory: { _ in 1 })
XCTAssertEqual(reportedDuplicates.count, 0)

container.register(String.self, factory: { _ in "two" })
XCTAssertEqual(reportedDuplicates.count, 0)
container.register(Int.self, factory: { _ in 2 })
XCTAssertEqual(reportedDuplicates.count, 1)
}

func testCustomStringDescription() throws {
assertCustomStringDescription(key: DuplicateRegistrationDetector.Key(
serviceType: String.self,
Expand Down