Skip to content

Commit

Permalink
fix: the coding of the attachment should be snake case
Browse files Browse the repository at this point in the history
  • Loading branch information
beatt83 committed Apr 29, 2024
1 parent 657a7fd commit e17057d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions Sources/DIDCommSwift/Helper/JSONEncoder+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Foundation
extension JSONEncoder {
static var didcomm: JSONEncoder {
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
encoder.outputFormatting = [.withoutEscapingSlashes, .sortedKeys]
} else {
Expand Down
14 changes: 7 additions & 7 deletions Sources/DIDCommSwift/Models/Message/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ extension Attachment {
self.data = try attachmentDataFromJson(data)
self.format = fromJson["format"] as? String
self.filename = fromJson["filename"] as? String
self.mediaType = fromJson["mediaType"] as? String
self.byteCount = (fromJson["byteCount"] as? NSNumber)?.intValue
self.lastModTime = (fromJson["lastModTime"] as? String)
.flatMap { Date(fromDidcommString: $0) }
self.mediaType = fromJson["media_type"] as? String
self.byteCount = (fromJson["byte_count"] as? NSNumber)?.intValue
self.lastModTime = (fromJson["lastmod_time"] as? NSNumber)
.flatMap { Date(fromDidcommInt: $0) }
self.description = fromJson["description"] as? String
}

Expand All @@ -334,10 +334,10 @@ extension Attachment {

description.map { jsonDic["description"] = $0 }
filename.map { jsonDic["filename"] = $0 }
mediaType.map { jsonDic["mediaType"] = $0 }
mediaType.map { jsonDic["media_type"] = $0 }
format.map { jsonDic["format"] = $0 }
lastModTime.map { jsonDic["lastModTime"] = $0.formattedForDidcommPack()}
byteCount.map { jsonDic["byteCount"] = $0 }
lastModTime.map { jsonDic["lastmod_time"] = $0.formattedForDidcommPack()}
byteCount.map { jsonDic["byte_count"] = $0 }

return jsonDic
}
Expand Down
27 changes: 27 additions & 0 deletions Tests/DIDCommSwiftTests/DIDComm/MessageCodingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,31 @@ final class MessageCodingTest: XCTestCase {
XCTAssertEqual(message.pthid, parsed.pthid)
XCTAssertEqual(message.customHeaders, parsed.customHeaders)
}

func testAttachment() throws {
let attachment = Attachment(
id: "test",
data: JsonAttachmentData(json: "{}"),
description: "testDescription",
filename: "testFilename",
mediaType: "testMediat",
format: "testFormat",
lastModTime: Date(),
byteCount: 10
)

let jsonData = try attachment.didcommJsonDic()

let parsed = try Attachment(fromJson: jsonData)

XCTAssertEqual(attachment.id, parsed.id)
XCTAssertEqual(attachment.description, parsed.description)
XCTAssertEqual(attachment.filename, parsed.filename)
XCTAssertEqual(attachment.mediaType, parsed.mediaType)
XCTAssertEqual(attachment.format, parsed.format)
XCTAssertEqual(attachment.byteCount, parsed.byteCount)
XCTAssertNotNil(jsonData["media_type"])
XCTAssertNotNil(jsonData["lastmod_time"])
XCTAssertNotNil(jsonData["byte_count"])
}
}

0 comments on commit e17057d

Please sign in to comment.