Skip to content

Commit eec0a05

Browse files
emlynmusebsto
andauthored
Fix Query String Parameters Being Ignored (#11)
* Put query string into the path for OpenAPIGenerator code * minor comments and formatting changes --------- Co-authored-by: Sébastien Stormacq <[email protected]>
1 parent a59a572 commit eec0a05

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Sources/HttpApi/APIGatewayV2+HTTPRequest.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ import OpenAPIRuntime
1818

1919
extension APIGatewayV2Request {
2020

21+
// OpenAPIGenerator expects the path to include the query string
22+
var pathWithQueryString: String {
23+
rawPath + (rawQueryString.isEmpty ? "" : "?\(rawQueryString)")
24+
}
25+
2126
/// Return an `HTTPRequest` for this `APIGatewayV2Request`
2227
public func httpRequest() throws -> HTTPRequest {
2328
HTTPRequest(
2429
method: self.context.http.method,
2530
scheme: "https",
2631
authority: "",
27-
path: self.rawPath,
32+
path: pathWithQueryString,
2833
headerFields: self.headers.httpFields()
2934
)
3035
}

Sources/Router/OpenAPILambdaRouterTrie.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ struct URIPath: URIPathCollection {
101101
}
102102

103103
// search for each path component. If a component is not found, it might be a parameter
104-
let pathComponents = path.split(separator: "/")
104+
// stop at the start of the query string
105+
let pathComponents = path.prefix(while: { $0 != "?" }).split(separator: "/")
105106
var currentNode = nodeHTTP
106107
for component in pathComponents {
107108
if let child = currentNode.child(with: component) {

Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift

+19-7
Original file line numberDiff line numberDiff line change
@@ -328,19 +328,31 @@ struct RouterGraphTests {
328328
#expect(handler != nil)
329329
}
330330

331-
@Test("Find handler 2")
332-
func testFindHandler2() throws {
331+
@Test(
332+
"Find handler 2",
333+
arguments: [
334+
"/element3/value1/element4",
335+
"/element3/value2/element4",
336+
"/element3/value1/element4?param1=value1",
337+
]
338+
)
339+
func testFindHandler2(
340+
pathToTest: String
341+
) throws {
333342
// given
334343
let strMethod = "GET"
335344
let method = HTTPRequest.Method(strMethod)!
336345
let graph = prepareGraphForFind(for: method)
337-
let pathToTest = "/element3/value1/element4"
338346

339347
//when
340-
#expect(throws: Never.self) { try graph.find(method: method, path: pathToTest) }
341-
let (handler, metadata) = try graph.find(method: method, path: pathToTest)
342-
#expect(metadata.count == 1)
343-
#expect(handler != nil)
348+
#expect(throws: Never.self) {
349+
let (handler, metadata) = try graph.find(method: method, path: pathToTest)
350+
351+
// then (we can not test if the query string param have been decoded, that's the job of the openapi runtime.)
352+
#expect(metadata.count == 1)
353+
#expect(handler != nil)
354+
}
355+
344356
}
345357

346358
@Test("Find handler 3")

0 commit comments

Comments
 (0)