diff --git a/Sources/ApolloAPI/DataDict.swift b/Sources/ApolloAPI/DataDict.swift index ae020f0cc2..0946a807bd 100644 --- a/Sources/ApolloAPI/DataDict.swift +++ b/Sources/ApolloAPI/DataDict.swift @@ -13,7 +13,7 @@ public struct DataDict: Hashable { } @inlinable public subscript(_ key: String) -> T { - get { _data[key] as! T } + get { _data[key]?.base as! T } set { _data[key] = newValue } _modify { var value = _data[key] as! T @@ -76,7 +76,7 @@ extension Array: SelectionSetEntityValue where Element: SelectionSetEntityValue guard let fieldData = fieldData as? [AnyHashable?] else { fatalError("\(Self.self) expected list of data for entity.") } - self = fieldData.map { Element.init(fieldData:$0, variables: variables) } + self = fieldData.map { Element.init(fieldData:$0?.base as? AnyHashable, variables: variables) } } @inlinable public var _fieldData: AnyHashable { map(\._fieldData) } diff --git a/Tests/ApolloTests/JSONTests.swift b/Tests/ApolloTests/JSONTests.swift index 701ca9a7be..65713e115e 100644 --- a/Tests/ApolloTests/JSONTests.swift +++ b/Tests/ApolloTests/JSONTests.swift @@ -43,23 +43,23 @@ class JSONTests: XCTestCase { func testJSONDictionaryEncodingAndDecoding() throws { let jsonString = """ -{ - "a_dict": { - "a_bool": true, - "another_dict" : { - "a_double": 23.1, - "an_int": 8, - "a_string": "LOL wat" - }, - "an_array": [ - "one", - "two", - "three" - ], - "a_null": null - } -} -""" + { + "a_dict": { + "a_bool": true, + "another_dict" : { + "a_double": 23.1, + "an_int": 8, + "a_string": "LOL wat" + }, + "an_array": [ + "one", + "two", + "three" + ], + "a_null": null + } + } + """ let data = try XCTUnwrap(jsonString.data(using: .utf8)) let json = try JSONSerializationFormat.deserialize(data: data) XCTAssertNotNil(json) @@ -72,12 +72,12 @@ class JSONTests: XCTestCase { let stringFromReserialized = try XCTUnwrap(String(bytes: reserialized, encoding: .utf8)) XCTAssertEqual(stringFromReserialized, """ -{"a_dict":{"a_bool":true,"a_null":null,"an_array":["one","two","three"],"another_dict":{"a_double":23.100000000000001,"a_string":"LOL wat","an_int":8}}} -""") + {"a_dict":{"a_bool":true,"a_null":null,"an_array":["one","two","three"],"another_dict":{"a_double":23.100000000000001,"a_string":"LOL wat","an_int":8}}} + """) } func testEncodingNSNullDoesNotCrash() throws { - let nsNull = ["aWeirdNull": NSNull()] + let nsNull: JSONObject = ["aWeirdNull": NSNull()] let serialized = try JSONSerializationFormat.serialize(value: nsNull) let stringFromSerialized = try XCTUnwrap(String(data: serialized, encoding: .utf8)) @@ -85,7 +85,7 @@ class JSONTests: XCTestCase { } func testEncodingOptionalNSNullDoesNotCrash() throws { - let optionalNSNull = ["aWeirdNull": Optional.some(NSNull())] + let optionalNSNull: JSONObject = ["aWeirdNull": Optional.some(NSNull())] let serialized = try JSONSerializationFormat.serialize(value: optionalNSNull as JSONObject) let stringFromSerialized = try XCTUnwrap(String(data: serialized, encoding: .utf8)) @@ -93,7 +93,7 @@ class JSONTests: XCTestCase { } func testEncodingDoubleOptionalsDoesNotCrash() throws { - let doubleOptional = ["aWeirdNull": Optional.some(Optional.none)] + let doubleOptional: JSONObject = ["aWeirdNull": Optional.some(Optional.none)] let serialized = try JSONSerializationFormat.serialize(value: doubleOptional as JSONObject) let stringFromSerialized = try XCTUnwrap(String(data: serialized, encoding: .utf8)) diff --git a/Tests/ApolloTests/SelectionSetTests.swift b/Tests/ApolloTests/SelectionSetTests.swift index eebdb0906c..55c774e451 100644 --- a/Tests/ApolloTests/SelectionSetTests.swift +++ b/Tests/ApolloTests/SelectionSetTests.swift @@ -31,7 +31,7 @@ class SelectionSetTests: XCTestCase { expect(actual.name).to(equal("Johnny Tsunami")) } - func test__selection_givenOptionalField_givenNilValue__returnsNil() { + func test__selection_givenOptionalField_missingValue__returnsNil() { // given class Hero: MockSelectionSet, SelectionSet { typealias Schema = MockSchemaMetadata @@ -55,6 +55,31 @@ class SelectionSetTests: XCTestCase { expect(actual.name).to(beNil()) } + func test__selection_givenOptionalField_givenNilValue__returnsNil() { + // given + class Hero: MockSelectionSet, SelectionSet { + typealias Schema = MockSchemaMetadata + + override class var __selections: [Selection] {[ + .field("__typename", String.self), + .field("name", String?.self) + ]} + + var name: String? { __data["name"] } + } + + let object: JSONObject = [ + "__typename": "Human", + "name": String?.none + ] + + // when + let actual = Hero(data: DataDict(object, variables: nil)) + + // then + expect(actual.name).to(beNil()) + } + // MARK: Scalar - Nested Array Tests func test__selection__nestedArrayOfScalar_nonNull_givenValue__returnsValue() {