CBORCoding icon indicating copy to clipboard operation
CBORCoding copied to clipboard

[BUG]: Decoding null inside an array does not work

Open antkont opened this issue 2 years ago • 1 comments

Expected Behavior

Decoding null values to nil should work inside an array.

Actual Behavior

Cannot decode and I get the following error:

valueNotFound(Swift.Optional<Swift.Array<Swift.Int>>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "m", intValue: nil), CodingKey(stringValue: "Index 1", intValue: 1), CodingKey(stringValue: "Index 1", intValue: 1)], debugDescription: "Expected Optional<Array<Int>> but found null instead.", underlyingError: nil))

Steps To Reproduce

Here is simple sample code:

struct CBORExample: Codable, Equatable {
    var m: [[Int]?]? = nil
}

func test() {
  let obj = CBORExample(m: [[1,2], nil, [3,4]])
  do {
      let encoder = CBOREncoder()
      let encodedData = try encoder.encode(obj)    // This works fine, checked the result with cbor.me
  
      let decoder = CBORDecoder()
      let decoded: CBORExample? = try decoder.decode(CBORExample.self, from: encodedData) // This fails
      print("Decoded: \(String(describing: decoded))")
  } catch {
      print("\(error)")
  }
}

Project Version

latest

The OS/Variant where this issue occurs.

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

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

14.3.1

What was the environment in which the issue appeared?

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

Additional Context

No response

antkont avatar Jun 26 '23 10:06 antkont