Skip to content

Commit

Permalink
Fix handling of "flag" URL query params (#3151)
Browse files Browse the repository at this point in the history
Fix handling of "flag" URL query params (e.g. `/foo?bar&baz`). Fix a Sendable warning.
  • Loading branch information
gwynne committed Feb 7, 2024
1 parent fa44af0 commit 664a063
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
12 changes: 10 additions & 2 deletions Sources/Vapor/Content/ContainerGetPathExecutor.swift
Expand Up @@ -11,13 +11,21 @@ internal struct ContainerGetPathExecutor<D: Decodable>: Decodable {
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Container getter couldn't find keypath to fetch (broken Decoder?)"))
}

self.result = try keypath.reduce(decoder) {
let lastDecoder = try keypath.dropLast().reduce(decoder) {
if let index = $1.intValue {
return try $0.unkeyedContainer(startingAt: index)._unsafe_inplace_superDecoder()
} else {
return try $0.container(keyedBy: BasicCodingKey.self).superDecoder(forKey: .key($1.stringValue))
}
}.singleValueContainer().decode(D.self)
}
if let index = keypath.last?.intValue {
var container = try lastDecoder.unkeyedContainer(startingAt: index)
self.result = try container.decode(D.self)
} else if let key = keypath.last?.stringValue {
self.result = try lastDecoder.container(keyedBy: BasicCodingKey.self).decode(D.self, forKey: .key(key))
} else {
self.result = try lastDecoder.singleValueContainer().decode(D.self)
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion Sources/Vapor/Utilities/RFC1123.swift
@@ -1,5 +1,13 @@
#if canImport(Glibc) && swift(>=5.10)
#if swift(>=5.10)
#if canImport(Darwin)
@preconcurrency import Darwin
#elseif canImport(Glibc)
@preconcurrency import Glibc
#elseif canImport(Musl)
@preconcurrency import Musl
#elseif canImport(WinSDK)
@preconcurrency import WinSDK
#endif
#endif
import Foundation
import NIOPosix
Expand Down
23 changes: 23 additions & 0 deletions Tests/VaporTests/QueryTests.swift
Expand Up @@ -300,4 +300,27 @@ final class QueryTests: XCTestCase {
XCTAssertThrowsError(try req.query.get(Int?.self, at: "page"))
}
}

func testValuelessParamGet() throws {
let app = Application()
defer { app.shutdown() }
let req = Request(
application: app,
method: .GET,
url: URI(string: "/"),
on: app.eventLoopGroup.next()
)

req.url = .init(path: "/foo?bar")
XCTAssertTrue(try req.query.get(Bool.self, at: "bar"))

req.url = .init(path: "/foo?bar&baz=bop")
XCTAssertTrue(try req.query.get(Bool.self, at: "bar"))

req.url = .init(path: "/foo")
XCTAssertFalse(try req.query.get(Bool.self, at: "bar"))

req.url = .init(path: "/foo?baz=bop")
XCTAssertFalse(try req.query.get(Bool.self, at: "bar"))
}
}

0 comments on commit 664a063

Please sign in to comment.