From 106d1578a2e0a8f47cdb547120bb3a591382fc25 Mon Sep 17 00:00:00 2001 From: Calvin Cestari Date: Wed, 18 Jan 2023 10:34:32 -0800 Subject: [PATCH 1/4] Fix optional scalar on iOS <= 14.4 --- Sources/ApolloAPI/DataDict.swift | 2 +- Tests/ApolloTests/SelectionSetTests.swift | 27 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Sources/ApolloAPI/DataDict.swift b/Sources/ApolloAPI/DataDict.swift index ae020f0cc2..1bcc276011 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 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() { From a071b14998bd1fc4bb0f3144ef4445778a221fca Mon Sep 17 00:00:00 2001 From: Calvin Cestari Date: Wed, 18 Jan 2023 11:03:09 -0800 Subject: [PATCH 2/4] Fix array crash on <= iOS 14.4 --- Sources/ApolloAPI/DataDict.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ApolloAPI/DataDict.swift b/Sources/ApolloAPI/DataDict.swift index 1bcc276011..0946a807bd 100644 --- a/Sources/ApolloAPI/DataDict.swift +++ b/Sources/ApolloAPI/DataDict.swift @@ -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) } From 869cb066fa693ab353cd574ecb9d59a4dc169d15 Mon Sep 17 00:00:00 2001 From: Calvin Cestari Date: Wed, 18 Jan 2023 16:26:40 -0800 Subject: [PATCH 3/4] Formatting changes --- Tests/ApolloTests/JSONTests.swift | 40 +++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Tests/ApolloTests/JSONTests.swift b/Tests/ApolloTests/JSONTests.swift index 701ca9a7be..20edd67423 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)) From 826966e9c3c5e830f1b718baba42f46080032140 Mon Sep 17 00:00:00 2001 From: Calvin Cestari Date: Wed, 18 Jan 2023 16:27:10 -0800 Subject: [PATCH 4/4] Adds types to test data --- Tests/ApolloTests/JSONTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/ApolloTests/JSONTests.swift b/Tests/ApolloTests/JSONTests.swift index 20edd67423..65713e115e 100644 --- a/Tests/ApolloTests/JSONTests.swift +++ b/Tests/ApolloTests/JSONTests.swift @@ -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))