@@ -869,6 +869,39 @@ class TestJSONEncoder : XCTestCase {
869
869
XCTAssertEqual ( JSONEncoder . OutputFormatting. withoutEscapingSlashes. rawValue, 8 )
870
870
}
871
871
872
+ func test_SR17581_codingEmptyDictionaryWithNonstringKeyDoesRoundtrip( ) throws {
873
+ struct Something : Codable {
874
+ struct Key : Codable , Hashable {
875
+ var x : String
876
+ }
877
+
878
+ var dict : [ Key : String ]
879
+
880
+ enum CodingKeys : String , CodingKey {
881
+ case dict
882
+ }
883
+
884
+ init ( from decoder: Decoder ) throws {
885
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
886
+ self . dict = try container. decode ( [ Key : String ] . self, forKey: . dict)
887
+ }
888
+
889
+ func encode( to encoder: Encoder ) throws {
890
+ var container = encoder. container ( keyedBy: CodingKeys . self)
891
+ try container. encode ( dict, forKey: . dict)
892
+ }
893
+
894
+ init ( dict: [ Key : String ] ) {
895
+ self . dict = dict
896
+ }
897
+ }
898
+
899
+ let toEncode = Something ( dict: [ : ] )
900
+ let data = try JSONEncoder ( ) . encode ( toEncode)
901
+ let result = try JSONDecoder ( ) . decode ( Something . self, from: data)
902
+ XCTAssertEqual ( result. dict. count, 0 )
903
+ }
904
+
872
905
// MARK: - Helper Functions
873
906
private var _jsonEmptyDictionary : Data {
874
907
return " {} " . data ( using: . utf8) !
@@ -1471,6 +1504,7 @@ extension TestJSONEncoder {
1471
1504
( " test_dictionary_snake_case_decoding " , test_dictionary_snake_case_decoding) ,
1472
1505
( " test_dictionary_snake_case_encoding " , test_dictionary_snake_case_encoding) ,
1473
1506
( " test_OutputFormattingValues " , test_OutputFormattingValues) ,
1507
+ ( " test_SR17581_codingEmptyDictionaryWithNonstringKeyDoesRoundtrip " , test_SR17581_codingEmptyDictionaryWithNonstringKeyDoesRoundtrip) ,
1474
1508
]
1475
1509
}
1476
1510
}
0 commit comments