CBORCoding icon indicating copy to clipboard operation
CBORCoding copied to clipboard

Empty dictionaries of certain types throw an error when decoding

Open mkeiser opened this issue 1 year ago • 0 comments

Expected Behavior

Some empty dictionaries that have been encoded with CBOREncoder throw a type mismatch error when the data is decoded again with CBORDecoder. To me this seems like a relatively severe bug.

Actual Behavior

See provided test methods.

Steps To Reproduce

Test methods I added to CBORDecoderTests. The first one currently fails, the second one succeeds.

    /// Fails!
    func testEmptyDictionaryRoundTripUInt64Key() throws {
        let dict = [UInt64: String]()
        let data = try CBOREncoder().encode(dict)
        let dict2: [UInt64: String] = try CBORDecoder().decode(from: data)
        XCTAssertEqual(dict, dict2)
    }

    /// Succeeds
    func testEmptyDictionaryRoundTripIntKey() throws {
        let dict = [Int: String]()
        let data = try CBOREncoder().encode(dict)
        let dict2: [Int: String] = try CBORDecoder().decode(from: data)
        XCTAssertEqual(dict, dict2)
    }

Other types fail too, for example dictionaries with Int32 keys.

Project Version

6fbd643c64cf6ef7a98391820c372d05fa9ed9ab

The OS/Variant where this issue occurs.

  • [ ] iOS
  • [X] macOS
  • [ ] tvOS
  • [ ] watchOS
  • [ ] Mac Catalyst
  • [ ] Other (Please specify in "Additional Context" section)

The version of Xcode that was used when this issue appeared.

15.0.1 (15A507)

What was the environment in which the issue appeared?

  • [X] CBORCoding.xcodeproj
  • [X] Package.swift
  • [ ] Carthage
  • [ ] CocoaPods
  • [ ] Swift Package Manager
  • [ ] Other (Please specify in "Additional Context" section)

Additional Context

No response

mkeiser avatar Jan 08 '24 12:01 mkeiser