Skip to content

Fix Query String Parameters Being Ignored #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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