Skip to content
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

Prevent a crash when decoding a single value from the query container #2163

Merged
merged 3 commits into from
Jan 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/Vapor/URLEncodedForm/URLEncodedFormDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ private struct _Decoder: Decoder {

/// See `Decoder`
func singleValueContainer() throws -> SingleValueDecodingContainer {
return SingleValueContainer(data: self.data!, codingPath: codingPath)
guard let data = self.data else {
throw DecodingError.valueNotFound(Any.self, at: codingPath)
}

return SingleValueContainer(data: data, codingPath: codingPath)
}

struct SingleValueContainer: SingleValueDecodingContainer {
Expand Down
25 changes: 23 additions & 2 deletions Tests/VaporTests/ApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,32 @@ final class ApplicationTests: XCTestCase {
let request = Request(application: app, on: app.eventLoopGroup.next())
request.headers.contentType = .json
request.url.path = "/foo"
print(request.url.string)
request.url.query = "hello=world"
print(request.url.string)
try XCTAssertEqual(request.query.get(String.self, at: "hello"), "world")
}

// https://github.com/vapor/vapor/pull/2163
func testWrappedSingleValueQueryDecoding() throws {
Joannis marked this conversation as resolved.
Show resolved Hide resolved
let app = Application()
defer { app.shutdown() }

let request = Request(application: app, on: app.eventLoopGroup.next())
request.headers.contentType = .json
request.url.path = "/foo"
request.url.query = ""

// Think of property wrappers, or MongoKitten's ObjectId
struct StringWrapper: Decodable {
let string: String

init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
string = try container.decode(String.self)
}
}

XCTAssertThrowsError(try request.query.get(StringWrapper.self, at: "hello"))
}

func testQueryGet() throws {
let app = Application()
Expand Down