Skip to content

Commit 7ee4691

Browse files
committed
Fix encoding of dates in Query
1 parent 2614aa4 commit 7ee4691

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

Sources/SotoCore/Encoder/CodableProperties/DateCoders.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,8 @@ public struct UnixEpochDateCoder: CustomDecoder, CustomEncoder {
126126
var container = encoder.singleValueContainer()
127127
try container.encode(value.timeIntervalSince1970)
128128
}
129+
130+
public static func string(from value: Date) -> String? {
131+
Int(value.timeIntervalSince1970).description
132+
}
129133
}

Sources/SotoCore/Encoder/RequestContainer.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class RequestEncodingContainer {
6363
if queryParams.count > 0 {
6464
let urlQueryString =
6565
queryParams
66-
.map { (key: $0.key, value: "\($0.value)") }
66+
.map { (key: $0.key, value: $0.value) }
6767
.sorted {
6868
// sort by key. if key are equal then sort by value
6969
if $0.key < $1.key { return true }
@@ -113,12 +113,20 @@ public class RequestEncodingContainer {
113113
}
114114
}
115115

116-
/// Write dictionary key value pairs to headers with prefix added to the keys
116+
/// Write date to headers
117117
@inlinable
118118
public func encodeHeader(_ value: Date, key: String) {
119119
self.encodeHeader(HTTPHeaderDateCoder.string(from: value), key: key)
120120
}
121121

122+
/// Write date to headers
123+
@inlinable
124+
public func encodeHeader(_ value: Date?, key: String) {
125+
if let value {
126+
self.encodeHeader(value, key: key)
127+
}
128+
}
129+
122130
/// Write dictionary key value pairs to headers with prefix added to the keys
123131
@inlinable
124132
public func encodeHeader(_ value: [String: some Any], key prefix: String) {
@@ -167,6 +175,20 @@ public class RequestEncodingContainer {
167175
}
168176
}
169177

178+
/// Write date to query
179+
@inlinable
180+
public func encodeQuery(_ value: Date, key: String) {
181+
self.encodeQuery(UnixEpochDateCoder.string(from: value), key: key)
182+
}
183+
184+
/// Write optional date to query
185+
@inlinable
186+
public func encodeQuery(_ value: Date?, key: String) {
187+
if let value {
188+
self.encodeQuery(value, key: key)
189+
}
190+
}
191+
170192
/// Write array as a series of query values
171193
@inlinable
172194
public func encodeQuery(_ value: [some Any], key: String) {
@@ -199,6 +221,8 @@ public class RequestEncodingContainer {
199221
}
200222
}
201223

224+
// MARK: Path encoding
225+
202226
/// Write value into URI path
203227
@inlinable
204228
public func encodePath(_ value: some Any, key: String) {

Tests/SotoCoreTests/AWSRequestTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,24 @@ class AWSRequestTests: XCTestCase {
328328
XCTAssertEqual(request?.url.absoluteString, "https://myservice.us-east-2.amazonaws.com/?one=1&two=2")
329329
}
330330

331+
func testQueryDate() {
332+
struct Input: AWSEncodableShape {
333+
let d: Date?
334+
func encode(to encoder: Encoder) throws {
335+
_ = encoder.container(keyedBy: CodingKeys.self)
336+
let requestContainer = encoder.userInfo[.awsRequest]! as! RequestEncodingContainer
337+
requestContainer.encodeQuery(self.d, key: "d")
338+
}
339+
340+
private enum CodingKeys: CodingKey {}
341+
}
342+
let input = Input(d: Date(timeIntervalSince1970: 1_000_000))
343+
let config = createServiceConfig(region: .useast2, service: "myservice")
344+
var request: AWSHTTPRequest?
345+
XCTAssertNoThrow(request = try AWSHTTPRequest(operation: "Test", path: "/", method: .GET, input: input, configuration: config))
346+
XCTAssertEqual(request?.url.absoluteString, "https://myservice.us-east-2.amazonaws.com/?d=1000000")
347+
}
348+
331349
func testQueryInPath() {
332350
struct Input: AWSEncodableShape {
333351
let q: String

0 commit comments

Comments
 (0)