Skip to content

Commit

Permalink
Fix regression that broke falling back to Codable (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
siemensikkema authored Jun 11, 2021
1 parent 70025fc commit c9ea040
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
7 changes: 6 additions & 1 deletion Sources/MultipartKit/FormDataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,12 @@ private extension MultipartFormData {
private extension MultipartPart {
func decode<T>(_ type: T.Type, at codingPath: [CodingKey]) throws -> T where T: Decodable {
guard
let Convertible = T.self as? MultipartPartConvertible.Type,
let Convertible = T.self as? MultipartPartConvertible.Type
else {
return try T(from: _FormDataDecoder(codingPath: codingPath, data: .single(self)))
}

guard
let decoded = Convertible.init(multipart: self) as? T
else {
let path = codingPath.map(\.stringValue).joined(separator: ".")
Expand Down
23 changes: 5 additions & 18 deletions Sources/MultipartKit/MultipartPartConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension String: MultipartPartConvertible {
public var multipart: MultipartPart? {
return MultipartPart(body: self)
}

public init?(multipart: MultipartPart) {
self.init(decoding: multipart.body.readableBytesView, as: UTF8.self)
}
Expand All @@ -30,7 +30,7 @@ extension FixedWidthInteger {
public var multipart: MultipartPart? {
return MultipartPart(body: self.description)
}

public init?(multipart: MultipartPart) {
guard let string = String(multipart: multipart) else {
return nil
Expand All @@ -54,7 +54,7 @@ extension Float: MultipartPartConvertible {
public var multipart: MultipartPart? {
return MultipartPart(body: self.description)
}

public init?(multipart: MultipartPart) {
guard let string = String(multipart: multipart) else {
return nil
Expand All @@ -67,7 +67,7 @@ extension Double: MultipartPartConvertible {
public var multipart: MultipartPart? {
return MultipartPart(body: self.description)
}

public init?(multipart: MultipartPart) {
guard let string = String(multipart: multipart) else {
return nil
Expand All @@ -80,7 +80,7 @@ extension Bool: MultipartPartConvertible {
public var multipart: MultipartPart? {
return MultipartPart(body: self.description)
}

public init?(multipart: MultipartPart) {
guard let string = String(multipart: multipart) else {
return nil
Expand All @@ -98,16 +98,3 @@ extension Data: MultipartPartConvertible {
self.init(multipart.body.readableBytesView)
}
}

extension UUID: MultipartPartConvertible {
public var multipart: MultipartPart? {
.init(body: uuidString)
}

public init?(multipart: MultipartPart) {
guard let string = String(multipart: multipart) else {
return nil
}
self.init(uuidString: string)
}
}
21 changes: 19 additions & 2 deletions Tests/MultipartKitTests/MultipartKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ class MultipartTests: XCTestCase {
}

XCTAssertThrowsError(try FormDataDecoder().decode(Foo.self, from: data, boundary: "hello")) { error in
guard case let DecodingError.dataCorrupted(context) = error else {
XCTFail("Was expecting an error of type DecodingError.dataCorrupted")
guard case let DecodingError.typeMismatch(_, context) = error else {
XCTFail("Was expecting an error of type DecodingError.typeMismatch")
return
}
XCTAssertEqual(context.codingPath.map(\.stringValue), ["link"])
Expand Down Expand Up @@ -559,6 +559,23 @@ class MultipartTests: XCTestCase {
XCTAssertEqual(try FormDataEncoder().encode(uuid, boundary: "-"), multipart)
XCTAssertEqual(try FormDataDecoder().decode(UUID.self, from: multipart, boundary: "-"), uuid)
}

// https://github.com/vapor/multipart-kit/issues/65
func testEncodingAndDecodingNonMultipartPartConvertibleCodableTypes() throws {
enum License: String, Codable, CaseIterable, Equatable {
case dme1
}
let license = License.dme1
let multipart = """
---\r
Content-Disposition: form-data\r
\r
\(license.rawValue)\r
-----\r\n
"""
XCTAssertEqual(try FormDataEncoder().encode(license, boundary: "-"), multipart)
XCTAssertEqual(try FormDataDecoder().decode(License.self, from: multipart, boundary: "-"), license)
}
}

// https://stackoverflow.com/a/54524110/1041105
Expand Down

0 comments on commit c9ea040

Please sign in to comment.