Skip to content

Commit 2ab7f7b

Browse files
ptoffygwynne
andauthored
Disallow homonymous parameters in partial (#150)
* Disallow homonymous parameters in partial * Update Sources/RoutingKit/TrieRouter/TrieRouterBuilder.swift Co-authored-by: Gwynne Raskind <gwynne@vapor.codes> * Fix spacing --------- Co-authored-by: Gwynne Raskind <gwynne@vapor.codes>
1 parent 6629807 commit 2ab7f7b

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

Sources/RoutingKit/TrieRouter/TrieRouterBuilder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ public struct TrieRouterBuilder<Output: Sendable>: RouterBuilder {
119119
)
120120
return node.copyWith(wildcard: newWildcard)
121121
case .partialParameter(let template, let components, let parameters):
122+
precondition(
123+
parameters.count == Set(parameters).count, #"Partial ":\#(template)" contains multiple parameters with the same name"#
124+
)
122125
var partials = node.partials ?? []
123126
let child = partials.first(where: { $0.template == template })?.node ?? Node()
124127
let updatedChild = insertRoute(node: child, path: path.dropFirst(), output: output)

Tests/RoutingKitTests/RouterTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ struct RouterTests {
354354
builder.register(20, at: ["p", ":{open"])
355355
}
356356
}
357+
358+
@Test("Same name partial parameter fails")
359+
func sameNamePartialParameter() async throws {
360+
await #expect(processExitsWith: .failure) {
361+
var b = TrieRouterBuilder<Int>()
362+
b.register(1, at: [":{param}.{param}"])
363+
}
364+
}
357365
#endif
358366

359367
@Test func testPartialGreedyAnchors() throws {

0 commit comments

Comments
 (0)