Skip to content

Commit 65ace78

Browse files
authored
Fix URLEncodedForm errors (#716)
1 parent 2c51494 commit 65ace78

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

Sources/Hummingbird/Codable/URLEncodedForm/URLEncodedFormDecoder.swift

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private class _URLEncodedFormDecoder: Decoder {
118118
case .empty:
119119
KeyedDecodingContainer(KDC(container: .init(values: [:]), decoder: self))
120120
default:
121-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
121+
throw DecodingError.typeMismatch([String: Any].self, .init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
122122
}
123123
}
124124

@@ -129,7 +129,7 @@ private class _URLEncodedFormDecoder: Decoder {
129129
case .empty:
130130
UKDC(container: .init(values: []), decoder: self)
131131
default:
132-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected an array"))
132+
throw DecodingError.typeMismatch([Any].self, .init(codingPath: self.codingPath, debugDescription: "Expected an array"))
133133
}
134134
}
135135

@@ -278,7 +278,7 @@ private class _URLEncodedFormDecoder: Decoder {
278278
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
279279
}
280280
guard case .map(let map) = node else {
281-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
281+
throw DecodingError.typeMismatch([String: Any].self, .init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
282282
}
283283
let container = KDC<NestedKey>(container: map, decoder: self.decoder)
284284
return KeyedDecodingContainer(container)
@@ -292,7 +292,7 @@ private class _URLEncodedFormDecoder: Decoder {
292292
throw DecodingError.keyNotFound(key, .init(codingPath: self.codingPath, debugDescription: ""))
293293
}
294294
guard case .array(let array) = node else {
295-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
295+
throw DecodingError.typeMismatch([String: Any].self, .init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
296296
}
297297
return UKDC(container: array, decoder: self.decoder)
298298
}
@@ -407,7 +407,7 @@ private class _URLEncodedFormDecoder: Decoder {
407407
let node = container.values[self.currentIndex]
408408
self.currentIndex += 1
409409
guard case .map(let map) = node else {
410-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
410+
throw DecodingError.typeMismatch([String: Any].self, .init(codingPath: self.codingPath, debugDescription: "Expected a dictionary"))
411411
}
412412
let container = KDC<NestedKey>(container: map, decoder: self.decoder)
413413
return KeyedDecodingContainer(container)
@@ -420,7 +420,7 @@ private class _URLEncodedFormDecoder: Decoder {
420420
let node = self.container.values[self.currentIndex]
421421
self.currentIndex += 1
422422
guard case .array(let array) = node else {
423-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected an array"))
423+
throw DecodingError.typeMismatch([String: Any].self, .init(codingPath: self.codingPath, debugDescription: "Expected an array"))
424424
}
425425
return UKDC(container: array, decoder: self.decoder)
426426
}
@@ -511,13 +511,13 @@ extension _URLEncodedFormDecoder {
511511

512512
func unbox(_ node: URLEncodedFormNode, as type: Bool.Type) throws -> Bool {
513513
guard case .leaf(let value) = node else {
514-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expect value not array or dictionary"))
514+
throw DecodingError.typeMismatch(Bool.self, .init(codingPath: self.codingPath, debugDescription: "Expect value not array or dictionary"))
515515
}
516516
if let value2 = value {
517517
if let unboxValue = Bool(value2.value) {
518518
return unboxValue
519519
} else {
520-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected Bool"))
520+
throw DecodingError.typeMismatch(Bool.self, .init(codingPath: self.codingPath, debugDescription: "Expected Boolean value"))
521521
}
522522
} else {
523523
return false
@@ -526,107 +526,110 @@ extension _URLEncodedFormDecoder {
526526

527527
func unbox(_ node: URLEncodedFormNode, as type: String.Type) throws -> String {
528528
guard case .leaf(let value) = node else {
529-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expect value not array or dictionary"))
529+
throw DecodingError.typeMismatch(
530+
String.self,
531+
.init(codingPath: self.codingPath, debugDescription: "Expect value not array or dictionary")
532+
)
530533
}
531534
guard let value2 = value else {
532-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected value not empty string"))
535+
throw DecodingError.typeMismatch(String.self, .init(codingPath: self.codingPath, debugDescription: "Expected value not empty string"))
533536
}
534537
return value2.value
535538
}
536539

537540
func unbox(_ node: URLEncodedFormNode, as type: URL.Type) throws -> URL {
538541
guard case .leaf(let value) = node else {
539-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expect value not array or dictionary"))
542+
throw DecodingError.typeMismatch(URL.self, .init(codingPath: self.codingPath, debugDescription: "Expect value not array or dictionary"))
540543
}
541544
guard let value2 = value else {
542-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected value not empty string"))
545+
throw DecodingError.typeMismatch(URL.self, .init(codingPath: self.codingPath, debugDescription: "Expected value not empty string"))
543546
}
544547
guard let url = URL(string: value2.value) else {
545-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Invalid URL String"))
548+
throw DecodingError.typeMismatch(URL.self, .init(codingPath: self.codingPath, debugDescription: "Invalid URL String"))
546549
}
547550
return url
548551
}
549552

550553
func unbox(_ node: URLEncodedFormNode, as type: Double.Type) throws -> Double {
551554
guard let unboxValue = try Double(unbox(node, as: String.self)) else {
552-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected Double"))
555+
throw DecodingError.typeMismatch(Double.self, .init(codingPath: self.codingPath, debugDescription: "Expected Double"))
553556
}
554557
return unboxValue
555558
}
556559

557560
func unbox(_ node: URLEncodedFormNode, as type: Float.Type) throws -> Float {
558561
guard let unboxValue = try Float(unbox(node, as: String.self)) else {
559-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected Float"))
562+
throw DecodingError.typeMismatch(Float.self, .init(codingPath: self.codingPath, debugDescription: "Expected Float"))
560563
}
561564
return unboxValue
562565
}
563566

564567
func unbox(_ node: URLEncodedFormNode, as type: Int.Type) throws -> Int {
565568
guard let unboxValue = try Int(unbox(node, as: String.self)) else {
566-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected Int"))
569+
throw DecodingError.typeMismatch(Int.self, .init(codingPath: self.codingPath, debugDescription: "Expected Int"))
567570
}
568571
return unboxValue
569572
}
570573

571574
func unbox(_ node: URLEncodedFormNode, as type: Int8.Type) throws -> Int8 {
572575
guard let unboxValue = try Int8(unbox(node, as: String.self)) else {
573-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected Int8"))
576+
throw DecodingError.typeMismatch(Int8.self, .init(codingPath: self.codingPath, debugDescription: "Expected Int8"))
574577
}
575578
return unboxValue
576579
}
577580

578581
func unbox(_ node: URLEncodedFormNode, as type: Int16.Type) throws -> Int16 {
579582
guard let unboxValue = try Int16(unbox(node, as: String.self)) else {
580-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected Int16"))
583+
throw DecodingError.typeMismatch(Int16.self, .init(codingPath: self.codingPath, debugDescription: "Expected Int16"))
581584
}
582585
return unboxValue
583586
}
584587

585588
func unbox(_ node: URLEncodedFormNode, as type: Int32.Type) throws -> Int32 {
586589
guard let unboxValue = try Int32(unbox(node, as: String.self)) else {
587-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected Int32"))
590+
throw DecodingError.typeMismatch(Int32.self, .init(codingPath: self.codingPath, debugDescription: "Expected Int32"))
588591
}
589592
return unboxValue
590593
}
591594

592595
func unbox(_ node: URLEncodedFormNode, as type: Int64.Type) throws -> Int64 {
593596
guard let unboxValue = try Int64(unbox(node, as: String.self)) else {
594-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected Int64"))
597+
throw DecodingError.typeMismatch(Int64.self, .init(codingPath: self.codingPath, debugDescription: "Expected Int64"))
595598
}
596599
return unboxValue
597600
}
598601

599602
func unbox(_ node: URLEncodedFormNode, as type: UInt.Type) throws -> UInt {
600603
guard let unboxValue = try UInt(unbox(node, as: String.self)) else {
601-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected UInt"))
604+
throw DecodingError.typeMismatch(UInt.self, .init(codingPath: self.codingPath, debugDescription: "Expected UInt"))
602605
}
603606
return unboxValue
604607
}
605608

606609
func unbox(_ node: URLEncodedFormNode, as type: UInt8.Type) throws -> UInt8 {
607610
guard let unboxValue = try UInt8(unbox(node, as: String.self)) else {
608-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected UInt8"))
611+
throw DecodingError.typeMismatch(UInt8.self, .init(codingPath: self.codingPath, debugDescription: "Expected UInt8"))
609612
}
610613
return unboxValue
611614
}
612615

613616
func unbox(_ node: URLEncodedFormNode, as type: UInt16.Type) throws -> UInt16 {
614617
guard let unboxValue = try UInt16(unbox(node, as: String.self)) else {
615-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected UInt16"))
618+
throw DecodingError.typeMismatch(UInt16.self, .init(codingPath: self.codingPath, debugDescription: "Expected UInt16"))
616619
}
617620
return unboxValue
618621
}
619622

620623
func unbox(_ node: URLEncodedFormNode, as type: UInt32.Type) throws -> UInt32 {
621624
guard let unboxValue = try UInt32(unbox(node, as: String.self)) else {
622-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected UInt32"))
625+
throw DecodingError.typeMismatch(UInt32.self, .init(codingPath: self.codingPath, debugDescription: "Expected UInt32"))
623626
}
624627
return unboxValue
625628
}
626629

627630
func unbox(_ node: URLEncodedFormNode, as type: UInt64.Type) throws -> UInt64 {
628631
guard let unboxValue = try UInt64(unbox(node, as: String.self)) else {
629-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Expected UInt64"))
632+
throw DecodingError.typeMismatch(UInt64.self, .init(codingPath: self.codingPath, debugDescription: "Expected UInt64"))
630633
}
631634
return unboxValue
632635
}
@@ -647,11 +650,15 @@ extension _URLEncodedFormDecoder {
647650
let dateString = try unbox(node, as: String.self)
648651
#if compiler(>=6.0)
649652
guard let date = try? Date(dateString, strategy: .iso8601) else {
650-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Invalid date format"))
653+
throw DecodingError.dataCorrupted(
654+
.init(codingPath: self.codingPath, debugDescription: "Expected date string to be ISO8601-formatted.")
655+
)
651656
}
652657
#else
653658
guard let date = URLEncodedForm.iso8601Formatter.date(from: dateString) else {
654-
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Invalid date format"))
659+
throw DecodingError.dataCorrupted(
660+
.init(codingPath: self.codingPath, debugDescription: "Expected date string to be ISO8601-formatted.")
661+
)
655662
}
656663
#endif
657664
return date

Sources/Hummingbird/Server/Request.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ extension Request {
4141
public func decode<Type: Decodable>(as type: Type.Type, context: some RequestContext) async throws -> Type {
4242
do {
4343
return try await context.requestDecoder.decode(type, from: self, context: context)
44-
} catch DecodingError.dataCorrupted(_) {
45-
let message = "The given data was not valid input."
44+
} catch DecodingError.dataCorrupted(let context) {
45+
let message = "The given data was not valid input: \(context.debugDescription)"
4646
throw HTTPError(.badRequest, message: message)
4747
} catch DecodingError.keyNotFound(let key, _) {
4848
let path = key.pathKeyValue

0 commit comments

Comments
 (0)