Skip to content

Commit

Permalink
Fix Query String Parameters Being Ignored (#11)
Browse files Browse the repository at this point in the history
* Put query string into the path for OpenAPIGenerator code

* minor comments and formatting changes

---------

Co-authored-by: Sébastien Stormacq <[email protected]>
  • Loading branch information
emlynmu and sebsto authored Oct 30, 2024
1 parent a59a572 commit eec0a05
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
7 changes: 6 additions & 1 deletion Sources/HttpApi/APIGatewayV2+HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Router/OpenAPILambdaRouterTrie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 19 additions & 7 deletions Tests/OpenAPILambdaTests/Router/RouterGraphTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit eec0a05

Please sign in to comment.