From 1e5d754eb483f59aaa7d5d576428e089761e6a43 Mon Sep 17 00:00:00 2001 From: Elijah Quartey Date: Mon, 26 Aug 2024 15:27:54 -0500 Subject: [PATCH] fix(datastore): FlutterSerializedModel.extractJsonValue returns `.some(nil)` instead of `nil` (#5370) --- .../ios/unit_tests/AmplifySerializedModelUnitTests.swift | 9 +++++++++ .../resources/FlutterSerializedModelData.swift | 6 ++++++ .../ios/Classes/types/model/FlutterSerializedModel.swift | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/amplify_datastore/example/ios/unit_tests/AmplifySerializedModelUnitTests.swift b/packages/amplify_datastore/example/ios/unit_tests/AmplifySerializedModelUnitTests.swift index 4e418b25a3..01d0b9411f 100644 --- a/packages/amplify_datastore/example/ios/unit_tests/AmplifySerializedModelUnitTests.swift +++ b/packages/amplify_datastore/example/ios/unit_tests/AmplifySerializedModelUnitTests.swift @@ -192,4 +192,13 @@ class AmplifySerializedModelUnitTests: XCTestCase { } } } + + func test_extracts_some_nil() throws { + let output = try FlutterSerializedModelData.BlogWithNullSerializedModel.jsonValue(for: "post") + + // This ensures if a property has a `null` json value, it gets returned as `.some(nil)` + // Per https://github.com/aws-amplify/amplify-swift/blob/cb80b91c38d99932af28df6be07633ee0563be08/Amplify/Categories/DataStore/Model/JSONHelper/JSONValueHolder.swift#L33-L34 + XCTAssertNotNil(output) + XCTAssertNil(output!) + } } diff --git a/packages/amplify_datastore/example/ios/unit_tests/resources/FlutterSerializedModelData.swift b/packages/amplify_datastore/example/ios/unit_tests/resources/FlutterSerializedModelData.swift index 9e3b969c19..dd2c44c8ba 100644 --- a/packages/amplify_datastore/example/ios/unit_tests/resources/FlutterSerializedModelData.swift +++ b/packages/amplify_datastore/example/ios/unit_tests/resources/FlutterSerializedModelData.swift @@ -9,6 +9,12 @@ struct FlutterSerializedModelData { "id": JSONValue.string("999"), "name": JSONValue.string("blog name"), ], modelName: "Blog") + static var BlogWithNullSerializedModel: FlutterSerializedModel = + .init(map: [ + "id": JSONValue.string("999"), + "name": JSONValue.string("blog name"), + "post": JSONValue.null, + ], modelName: "Blog") static var CommentSerializedModel: FlutterSerializedModel = .init(map: [ "id": JSONValue.string("999"), diff --git a/packages/amplify_datastore/ios/Classes/types/model/FlutterSerializedModel.swift b/packages/amplify_datastore/ios/Classes/types/model/FlutterSerializedModel.swift index 914400b2d5..f78a1c298b 100644 --- a/packages/amplify_datastore/ios/Classes/types/model/FlutterSerializedModel.swift +++ b/packages/amplify_datastore/ios/Classes/types/model/FlutterSerializedModel.swift @@ -97,7 +97,7 @@ public struct FlutterSerializedModel: Model, ModelIdentifiable, JSONValueHolder case .string(let deserializedValue): return deserializedValue case .null: - return nil + return .some(nil) } }