Skip to content

Commit

Permalink
fix(flutter/datastore): Nullable custom type arrays
Browse files Browse the repository at this point in the history
Fixes aws-amplify/amplify-flutter#3575. When a custom type array is not required and `null` is returned from AppSync, deserialization will fail since `getAsJsonArray` will throw for `null` values.

This resolves the issue by checking if a field is `null` before attempting deserialization.
  • Loading branch information
Dillon Nys committed Aug 18, 2023
1 parent 8020948 commit cd0025e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,13 @@ public SerializedModel deserialize(JsonElement json, Type typeOfT, JsonDeseriali
continue;
}

JsonElement fieldValue = item.getValue();
String fieldName = field.getName();
JsonElement fieldValue = item.getValue();
if (fieldValue.isJsonNull()) {
LOGGER.verbose(String.format("Field %s is null", fieldName));
serializedData.put(fieldName, null);
continue;
}

boolean isModel = field.isModel();
boolean isCustomType = field.isCustomType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void setup() {
GsonTemporalAdapters.register(builder);
SerializedModelAdapter.register(builder);
SerializedCustomTypeAdapter.register(builder);
gson = builder.create();
gson = builder.serializeNulls().create();
}

/**
Expand Down Expand Up @@ -162,6 +162,7 @@ public void serdeForNestedCustomTypes() throws JSONException, AmplifyException {
contactSerializedData.put("email", "[email protected]");
contactSerializedData.put("phone", phone);
contactSerializedData.put("additionalPhoneNumbers", additionalPhoneNumbers);
contactSerializedData.put("aliases", null);
SerializedCustomType contact = SerializedCustomType.builder()
.serializedData(contactSerializedData)
.customTypeSchema(contactSchema)
Expand All @@ -170,6 +171,7 @@ public void serdeForNestedCustomTypes() throws JSONException, AmplifyException {
Map<String, Object> personSerializedData = new HashMap<>();
personSerializedData.put("fullName", "Tester Test");
personSerializedData.put("contact", contact);
personSerializedData.put("additionalContacts", null);
personSerializedData.put("id", "some-unique-id");
SerializedModel person = SerializedModel.builder()
.modelSchema(personSchema)
Expand Down Expand Up @@ -327,6 +329,14 @@ private CustomTypeSchema customTypeSchemaForContact() {
.isArray(true)
.isRequired(true)
.build());
contactFields.put("aliases", CustomTypeField.builder()
.name("aliases")
.targetType("Contact")
.javaClassForValue(Map.class)
.isCustomType(true)
.isArray(true)
.isRequired(false)
.build());
return CustomTypeSchema.builder()
.name("Contact")
.pluralName("Contacts")
Expand Down Expand Up @@ -355,6 +365,14 @@ private ModelSchema modelSchemaForPerson() {
.isRequired(true)
.isCustomType(true)
.build());
personFields.put("additionalContacts", ModelField.builder()
.name("additionalContacts")
.targetType("Contact")
.javaClassForValue(Map.class)
.isRequired(false)
.isCustomType(true)
.isArray(true)
.build());
return ModelSchema.builder()
.name("Person")
.pluralName("People")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,23 @@
"owner": {
"name": "BelongsTo",
"targetName": "blogOwnerId",
"targetNames": null,
"associatedName": null,
"associatedType": "BlogOwner"
},
"posts": {
"name": "HasMany",
"associatedName": "blog",
"associatedType": "Post"
"associatedType": "Post",
"targetNames": null,
"targetName": null
}
},
"indexes": {},
"modelClass": "com.amplifyframework.core.model.SerializedModel",
"modelSchemaVersion": 0
"listPluralName": null,
"modelType": null,
"syncPluralName": null
},
"serializedData": {
"owner": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"id": "970f1b78-8786-4762-a43a-17c70f966684",
"modelSchema": {
"name": "Meeting",
"modelSchemaVersion": 0,
"pluralName": "Meetings",
"authRules": [],
"listPluralName": null,
"modelType": null,
"fields": {
"date": {
"name": "date",
Expand Down Expand Up @@ -79,6 +80,7 @@
"authRules": []
}
},
"syncPluralName": null,
"associations": {},
"indexes": {},
"modelClass": "com.amplifyframework.core.model.SerializedModel",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
}
}
],
"aliases": null,
"phone": {
"serializedData": {
"number": "41555555555",
Expand Down Expand Up @@ -109,6 +110,15 @@
"targetType": "Phone",
"isArray": true
},
"aliases": {
"javaClassForValue": "java.util.Map",
"isRequired": false,
"isCustomType": true,
"name": "aliases",
"isEnum": false,
"targetType": "Contact",
"isArray": true
},
"phone": {
"javaClassForValue": "java.util.Map",
"isRequired": true,
Expand All @@ -130,18 +140,34 @@
}
}
},
"additionalContacts": null,
"fullName": "Tester Test",
"id": "some-unique-id"
},
"id": "some-unique-id",
"modelSchema": {
"associations": {},
"listPluralName": null,
"pluralName": "People",
"authRules": [],
"indexes": {},
"modelClass": null,
"name": "Person",
"modelSchemaVersion": 0,
"modelType": null,
"fields": {
"additionalContacts": {
"javaClassForValue": "java.util.Map",
"isRequired": false,
"isCustomType": true,
"isReadOnly": false,
"isModel": false,
"authRules": [],
"name": "additionalContacts",
"isEnum": false,
"targetType": "Contact",
"isArray": true
},
"contact": {
"javaClassForValue": "java.util.Map",
"isRequired": true,
Expand Down Expand Up @@ -178,6 +204,7 @@
"targetType": "ID",
"isArray": false
}
}
},
"syncPluralName": null
}
}

0 comments on commit cd0025e

Please sign in to comment.