You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// The structure that would be encoded here looks like
822
+
//
823
+
// [[[Float.infinity]]]
824
+
//
825
+
// The wrapper asks for an unkeyed container ([^]), gets a super encoder, and creates a nested container into that ([[^]]).
826
+
// We then encode an array into that ([[[^]]]), which happens to be a value that causes us to throw an error.
827
+
//
828
+
// The issue at hand reproduces when you have a referencing encoder (superEncoder() creates one) that has a container on the stack (unkeyedContainer() adds one) that encodes a value going through box_() (Array does that) that encodes something which throws (Float.infinity does that).
829
+
// When reproducing, this will cause a test failure via fatalError().
// The container stack here starts as [[1,2,3]]. Attempting to decode as [String] matches the outer layer (Array), and begins decoding the array.
885
+
// Once Array decoding begins, 1 is pushed onto the container stack ([[1,2,3], 1]), and 1 is attempted to be decoded as String. This throws a .typeMismatch, but the container is not popped off the stack.
886
+
// When attempting to decode [Int], the container stack is still ([[1,2,3], 1]), and 1 fails to decode as [Int].
887
+
letjson="[1,2,3]".data(using:.utf8)!
888
+
let _ =try!JSONDecoder().decode(EitherDecodable<[String],[Int]>.self, from: json)
889
+
}
890
+
891
+
func testDecoderStateThrowOnDecodeCustomDate(){
892
+
// This test is identical to testDecoderStateThrowOnDecode, except we're going to fail because our closure throws an error, not because we hit a type mismatch.
893
+
letdecoder=JSONDecoder()
894
+
decoder.dateDecodingStrategy =.custom({ decoder in
895
+
enumCustomError:Error{case foo }
896
+
throwCustomError.foo
897
+
})
898
+
899
+
letjson="{\"value\": 1}".data(using:.utf8)!
900
+
let _ =try! decoder.decode(EitherDecodable<TopLevelWrapper<Date>,TopLevelWrapper<Int>>.self, from: json)
901
+
}
902
+
903
+
func testDecoderStateThrowOnDecodeCustomData(){
904
+
// This test is identical to testDecoderStateThrowOnDecode, except we're going to fail because our closure throws an error, not because we hit a type mismatch.
905
+
letdecoder=JSONDecoder()
906
+
decoder.dataDecodingStrategy =.custom({ decoder in
907
+
enumCustomError:Error{case foo }
908
+
throwCustomError.foo
909
+
})
910
+
911
+
letjson="{\"value\": 1}".data(using:.utf8)!
912
+
let _ =try! decoder.decode(EitherDecodable<TopLevelWrapper<Data>,TopLevelWrapper<Int>>.self, from: json)
0 commit comments