diff --git a/Sources/HttpApi/APIGatewayV2+HTTPRequest.swift b/Sources/HttpApi/APIGatewayV2+HTTPRequest.swift index 1af0939..0227a68 100644 --- a/Sources/HttpApi/APIGatewayV2+HTTPRequest.swift +++ b/Sources/HttpApi/APIGatewayV2+HTTPRequest.swift @@ -18,13 +18,18 @@ import OpenAPIRuntime extension APIGatewayV2Request { + // OpenAPIGenerator expects the path to include the query string + var pathWithQueryString: String { + rawPath + (rawQueryString.isEmpty ? "" : "?\(rawQueryString)") + } + /// Return an `HTTPRequest` for this `APIGatewayV2Request` public func httpRequest() throws -> HTTPRequest { HTTPRequest( method: self.context.http.method, scheme: "https", authority: "", - path: self.rawPath, + path: pathWithQueryString, headerFields: self.headers.httpFields() ) } diff --git a/Sources/Router/OpenAPILambdaRouterTrie.swift b/Sources/Router/OpenAPILambdaRouterTrie.swift index 7032bf3..1d5ca2d 100644 --- a/Sources/Router/OpenAPILambdaRouterTrie.swift +++ b/Sources/Router/OpenAPILambdaRouterTrie.swift @@ -101,7 +101,8 @@ struct URIPath: URIPathCollection { } // search for each path component. If a component is not found, it might be a parameter - let pathComponents = path.split(separator: "/") + // stop at the start of the query string + let pathComponents = path.prefix(while: { $0 != "?" }).split(separator: "/") var currentNode = nodeHTTP for component in pathComponents { if let child = currentNode.child(with: component) { diff --git a/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift b/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift index beecae5..610c736 100644 --- a/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift +++ b/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift @@ -328,19 +328,31 @@ struct RouterGraphTests { #expect(handler != nil) } - @Test("Find handler 2") - func testFindHandler2() throws { + @Test( + "Find handler 2", + arguments: [ + "/element3/value1/element4", + "/element3/value2/element4", + "/element3/value1/element4?param1=value1", + ] + ) + func testFindHandler2( + pathToTest: String + ) throws { // given let strMethod = "GET" let method = HTTPRequest.Method(strMethod)! let graph = prepareGraphForFind(for: method) - let pathToTest = "/element3/value1/element4" //when - #expect(throws: Never.self) { try graph.find(method: method, path: pathToTest) } - let (handler, metadata) = try graph.find(method: method, path: pathToTest) - #expect(metadata.count == 1) - #expect(handler != nil) + #expect(throws: Never.self) { + let (handler, metadata) = try graph.find(method: method, path: pathToTest) + + // then (we can not test if the query string param have been decoded, that's the job of the openapi runtime.) + #expect(metadata.count == 1) + #expect(handler != nil) + } + } @Test("Find handler 3")