Skip to content

Commit 65afb4d

Browse files
authored
fix(datastore): FlutterSerializedModel.extractJsonValue returns .some(nil) instead of nil (#5370)
1 parent 6a37755 commit 65afb4d

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

packages/amplify_datastore/example/ios/unit_tests/AmplifySerializedModelUnitTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,13 @@ class AmplifySerializedModelUnitTests: XCTestCase {
192192
}
193193
}
194194
}
195+
196+
func test_extracts_some_nil() throws {
197+
let output = try FlutterSerializedModelData.BlogWithNullSerializedModel.jsonValue(for: "post")
198+
199+
// This ensures if a property has a `null` json value, it gets returned as `.some(nil)`
200+
// Per https://github.com/aws-amplify/amplify-swift/blob/cb80b91c38d99932af28df6be07633ee0563be08/Amplify/Categories/DataStore/Model/JSONHelper/JSONValueHolder.swift#L33-L34
201+
XCTAssertNotNil(output)
202+
XCTAssertNil(output!)
203+
}
195204
}

packages/amplify_datastore/example/ios/unit_tests/resources/FlutterSerializedModelData.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ struct FlutterSerializedModelData {
99
"id": JSONValue.string("999"),
1010
"name": JSONValue.string("blog name"),
1111
], modelName: "Blog")
12+
static var BlogWithNullSerializedModel: FlutterSerializedModel =
13+
.init(map: [
14+
"id": JSONValue.string("999"),
15+
"name": JSONValue.string("blog name"),
16+
"post": JSONValue.null,
17+
], modelName: "Blog")
1218
static var CommentSerializedModel: FlutterSerializedModel =
1319
.init(map: [
1420
"id": JSONValue.string("999"),

packages/amplify_datastore/ios/Classes/types/model/FlutterSerializedModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public struct FlutterSerializedModel: Model, ModelIdentifiable, JSONValueHolder
9797
case .string(let deserializedValue):
9898
return deserializedValue
9999
case .null:
100-
return nil
100+
return .some(nil)
101101
}
102102
}
103103

0 commit comments

Comments
 (0)