Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): Flutter: fail to serialize list of custom type values #2462

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -316,14 +318,26 @@ public Object convertValueFromTarget(Model model, ModelField field) throws DataS
// We don't want to store entire SerializedCustomType along with schema into
// Database but only its value.
if (field.isCustomType() && fieldValue != null) {
fieldValue = ((SerializedCustomType) fieldValue).getFlatSerializedData();
if (field.isArray()) {
@SuppressWarnings("unchecked")
List<SerializedCustomType> listOfItems = (List<SerializedCustomType>) fieldValue;
List<Map<String, Object>> listOfValues = new ArrayList<>();

for (SerializedCustomType item : listOfItems) {
listOfValues.add(item.getFlatSerializedData());
}
fieldValue = listOfValues;
} else {
fieldValue = ((SerializedCustomType) fieldValue).getFlatSerializedData();
}
}
} else {
fieldValue = ModelHelper.getValue(model, field);
}
if (fieldValue == null) {
return null;
}

final JavaFieldType javaFieldType = TypeConverter.getJavaFieldType(field);
return convertRawValueToTarget(fieldValue, javaFieldType, gson);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@
package com.amplifyframework.datastore.storage.sqlite;

import com.amplifyframework.AmplifyException;
import com.amplifyframework.core.model.CustomTypeField;
import com.amplifyframework.core.model.CustomTypeSchema;
import com.amplifyframework.core.model.Model;
import com.amplifyframework.core.model.ModelField;
import com.amplifyframework.core.model.ModelSchema;
import com.amplifyframework.core.model.SchemaRegistry;
import com.amplifyframework.core.model.SerializedCustomType;
import com.amplifyframework.core.model.SerializedModel;
import com.amplifyframework.core.model.types.JavaFieldType;
import com.amplifyframework.datastore.DataStoreException;
import com.amplifyframework.util.GsonFactory;
import com.amplifyframework.util.UserAgent;

import com.google.gson.Gson;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -100,4 +111,100 @@ public void testConvertRawValueToTargetDateTimeFlutter() throws AmplifyException
final String expected = "2020-01-01T16:00:00.050020000";
assertEquals(expected, actual);
}

/**
* Test the convertValueFromTarget getting serialized value from a CustomType field in Flutter user cases.
* @throws DataStoreException Not expected.
*/
@Test
public void testConvertFromCustomTypeTargetValueFlutter() throws DataStoreException {
SerializedModel model = getTestSerializedModel();
ModelField testField = ModelField.builder()
.name("customTypeField")
.javaClassForValue(Map.class)
.targetType("DummyCustomType")
.isRequired(true)
.isCustomType(true)
.build();

SQLiteModelFieldTypeConverter converter = new SQLiteModelFieldTypeConverter(
ModelSchema.builder().name("DummySchemaNotUsedInTest").modelType(Model.Type.USER).build(),
SchemaRegistry.instance(),
new Gson()
);

Object result = converter.convertValueFromTarget(model, testField);
assertEquals(result, "{\"phone\":\"4155555555\",\"countryCode\":\"+1\"}");
}

/**
* Test the convertValueFromTarget getting serialized value from a list of CustomType field in Flutter user cases.
* @throws DataStoreException Not expected.
*/
@Test
public void testConvertFromListOfCustomTypeTargetValuesFlutter() throws DataStoreException {
SerializedModel model = getTestSerializedModel();
ModelField testField = ModelField.builder()
.name("listCustomTypeField")
.javaClassForValue(List.class)
.targetType("DummyCustomType")
.isRequired(true)
.isCustomType(true)
.isArray(true)
.build();

SQLiteModelFieldTypeConverter converter = new SQLiteModelFieldTypeConverter(
ModelSchema.builder().name("DummySchemaNotUsedInTest").modelType(Model.Type.USER).build(),
SchemaRegistry.instance(),
new Gson()
);

Object result = converter.convertValueFromTarget(model, testField);
assertEquals(result, "[{\"phone\":\"4155555555\",\"countryCode\":\"+1\"},{\"phone\":\"4155555555\"," +
"\"countryCode\":\"+1\"}]");
}

private SerializedModel getTestSerializedModel() {
Map<String, Object> serializedCustomTypeData = new HashMap<>();
serializedCustomTypeData.put("phone", "4155555555");
serializedCustomTypeData.put("countryCode", "+1");

Map<String, CustomTypeField> customTypeFields = new HashMap<>();
customTypeFields.put("phone", CustomTypeField.builder()
.name("phone")
.targetType("String")
.javaClassForValue(String.class)
.build());
customTypeFields.put("countryCode", CustomTypeField.builder()
.name("countryCode")
.targetType("String")
.javaClassForValue(String.class)
.build());

SerializedCustomType testCustomType = SerializedCustomType.builder()
.serializedData(serializedCustomTypeData)
.customTypeSchema(CustomTypeSchema.builder()
.name("Phone")
.pluralName("Phones")
.fields(customTypeFields)
.build())
.build();

List<SerializedCustomType> testCustomTypeList = new ArrayList<>();
testCustomTypeList.add(testCustomType);
testCustomTypeList.add(testCustomType);

Map<String, Object> serializedModelData = new HashMap<>();
serializedModelData.put("id", "dummy-id");
serializedModelData.put("customTypeField", testCustomType);
serializedModelData.put("listCustomTypeField", testCustomTypeList);

return SerializedModel.builder()
.modelSchema(ModelSchema.builder()
.name("DummySchemaNotUsed")
.modelClass(SerializedModel.class)
.build())
.serializedData(serializedModelData)
.build();
}
}