From 1197bb40fb61f61fc27e5b536c23dffc651c9d1d Mon Sep 17 00:00:00 2001 From: Emlyn Murphy Date: Fri, 25 Oct 2024 17:05:24 -0400 Subject: [PATCH 1/2] Put query string into the path for OpenAPIGenerator code --- Sources/HttpApi/APIGatewayV2+HTTPRequest.swift | 7 ++++++- Sources/Router/OpenAPILambdaRouterTrie.swift | 2 +- .../Router/RouterGraphTest.swift | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) 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..e7ebd8a 100644 --- a/Sources/Router/OpenAPILambdaRouterTrie.swift +++ b/Sources/Router/OpenAPILambdaRouterTrie.swift @@ -101,7 +101,7 @@ struct URIPath: URIPathCollection { } // search for each path component. If a component is not found, it might be a parameter - let pathComponents = path.split(separator: "/") + 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..6d88177 100644 --- a/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift +++ b/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift @@ -323,18 +323,26 @@ struct RouterGraphTests { //when #expect(throws: Never.self) { try graph.find(method: method, path: pathToTest) } - let (handler, metadata) = try graph.find(method: method, path: pathToTest) + let (handler, metadata) = try graph.find(method: method, path: pathToTest ) #expect(metadata.count == 0) #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) } From f63beef1bb8bd863f4c577db4c7ae90c73b55ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Wed, 30 Oct 2024 18:18:31 +0100 Subject: [PATCH 2/2] minor comments and formatting changes --- Sources/Router/OpenAPILambdaRouterTrie.swift | 1 + .../Router/RouterGraphTest.swift | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Sources/Router/OpenAPILambdaRouterTrie.swift b/Sources/Router/OpenAPILambdaRouterTrie.swift index e7ebd8a..1d5ca2d 100644 --- a/Sources/Router/OpenAPILambdaRouterTrie.swift +++ b/Sources/Router/OpenAPILambdaRouterTrie.swift @@ -101,6 +101,7 @@ struct URIPath: URIPathCollection { } // search for each path component. If a component is not found, it might be a parameter + // stop at the start of the query string let pathComponents = path.prefix(while: { $0 != "?" }).split(separator: "/") var currentNode = nodeHTTP for component in pathComponents { diff --git a/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift b/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift index 6d88177..610c736 100644 --- a/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift +++ b/Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift @@ -323,7 +323,7 @@ struct RouterGraphTests { //when #expect(throws: Never.self) { try graph.find(method: method, path: pathToTest) } - let (handler, metadata) = try graph.find(method: method, path: pathToTest ) + let (handler, metadata) = try graph.find(method: method, path: pathToTest) #expect(metadata.count == 0) #expect(handler != nil) } @@ -333,7 +333,7 @@ struct RouterGraphTests { arguments: [ "/element3/value1/element4", "/element3/value2/element4", - "/element3/value1/element4?param1=value1" + "/element3/value1/element4?param1=value1", ] ) func testFindHandler2( @@ -345,10 +345,14 @@ struct RouterGraphTests { let graph = prepareGraphForFind(for: method) //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")