Skip to content

Commit 8ccb280

Browse files
fix: properly support flutter online scenarios (#961)
Previous work to support online-sync mode from Flutter had been vetted with the HybridCloudSyncInstrumentationTest. This test was using ModelSchema.fromClass(), which will still store a Class<T> in the modelClass. As a result, the test was giving false optimism as to the functionality in the code base. A change is made to pull ModelSchema from JSON files, in the ./assets directory, instead. Driven by this updated tests, a number of additional classes are updated to become aware of ModelSchema and SerializedModel.
1 parent 4a82b6c commit 8ccb280

File tree

43 files changed

+806
-388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+806
-388
lines changed

aws-api-appsync/src/main/java/com/amplifyframework/datastore/appsync/ModelWithMetadataAdapter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import androidx.annotation.NonNull;
1919

2020
import com.amplifyframework.core.model.Model;
21+
import com.amplifyframework.util.GsonObjectConverter;
2122

2223
import com.google.gson.GsonBuilder;
2324
import com.google.gson.JsonDeserializationContext;
@@ -60,7 +61,15 @@ public ModelWithMetadata<? extends Model> deserialize(
6061
throw new JsonParseException("Expected a parameterized type during ModelWithMetadata deserialization.");
6162
}
6263

63-
Model model = context.deserialize(json, modelClassType);
64+
final Model model;
65+
if (modelClassType == SerializedModel.class) {
66+
model = SerializedModel.builder()
67+
.serializedData(GsonObjectConverter.toMap((JsonObject) json))
68+
.modelSchema(null)
69+
.build();
70+
} else {
71+
model = context.deserialize(json, modelClassType);
72+
}
6473
ModelMetadata metadata = context.deserialize(json, ModelMetadata.class);
6574
return new ModelWithMetadata<>(model, metadata);
6675
}

aws-api-appsync/src/test/java/com/amplifyframework/datastore/appsync/SerializedModelAdapterTest.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
package com.amplifyframework.datastore.appsync;
1717

18-
import com.amplifyframework.core.model.Model;
1918
import com.amplifyframework.core.model.ModelAssociation;
2019
import com.amplifyframework.core.model.ModelField;
2120
import com.amplifyframework.core.model.ModelSchema;
@@ -27,6 +26,7 @@
2726
import com.google.gson.Gson;
2827
import com.google.gson.GsonBuilder;
2928
import org.json.JSONException;
29+
import org.json.JSONObject;
3030
import org.junit.Assert;
3131
import org.junit.Before;
3232
import org.junit.Test;
@@ -75,14 +75,10 @@ public void serdeForSerializedModelWithTemporals() throws JSONException {
7575
.modelSchema(modelSchemaForMeeting())
7676
.build();
7777

78-
String expectedJson =
79-
Resources.readAsString("serde-for-meeting-in-serialized-model.json");
80-
81-
JSONAssert.assertEquals(
82-
expectedJson,
83-
gson.toJson(serializedModel),
84-
true
85-
);
78+
String expectedResourcePath = "serde-for-meeting-in-serialized-model.json";
79+
String expectedJson = Resources.readAsJson(expectedResourcePath).toString(2);
80+
String actualJson = new JSONObject(gson.toJson(serializedModel)).toString(2);
81+
JSONAssert.assertEquals(expectedJson, actualJson, true);
8682

8783
SerializedModel recovered = gson.fromJson(expectedJson, SerializedModel.class);
8884
Assert.assertEquals(serializedModel, recovered);
@@ -112,14 +108,11 @@ public void serdeForNestedSerializedModels() throws JSONException {
112108
.modelSchema(modelSchemaForBlog())
113109
.build();
114110

115-
String expectedJson =
116-
Resources.readAsString("serde-for-blog-in-serialized-model.json");
111+
String resourcePath = "serde-for-blog-in-serialized-model.json";
112+
String expectedJson = new JSONObject(Resources.readAsString(resourcePath)).toString(2);
113+
String actualJson = new JSONObject(gson.toJson(blogAsSerializedModel)).toString(2);
117114

118-
JSONAssert.assertEquals(
119-
expectedJson,
120-
gson.toJson(blogAsSerializedModel),
121-
true
122-
);
115+
Assert.assertEquals(expectedJson, actualJson);
123116

124117
SerializedModel recovered = gson.fromJson(expectedJson, SerializedModel.class);
125118
Assert.assertEquals(blogAsSerializedModel, recovered);
@@ -166,7 +159,7 @@ private ModelSchema modelSchemaForMeeting() {
166159
.fields(fields)
167160
.associations(Collections.emptyMap())
168161
.indexes(Collections.emptyMap())
169-
.modelClass(Model.class)
162+
.modelClass(SerializedModel.class)
170163
.build();
171164
}
172165

@@ -186,7 +179,7 @@ private ModelSchema modelSchemaForBlog() {
186179
.build());
187180
fields.put("owner", ModelField.builder()
188181
.name("owner")
189-
.javaClassForValue(Model.class)
182+
.javaClassForValue(SerializedModel.class)
190183
.targetType("BlogOwner")
191184
.isRequired(true)
192185
.isModel(true)
@@ -218,7 +211,7 @@ private ModelSchema modelSchemaForBlog() {
218211
.fields(fields)
219212
.associations(associations)
220213
.indexes(Collections.emptyMap())
221-
.modelClass(Model.class)
214+
.modelClass(SerializedModel.class)
222215
.build();
223216
}
224217
}

aws-api-appsync/src/test/resources/serde-for-blog-in-serialized-model.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"owner": {
2929
"name": "owner",
30-
"javaClassForValue": "com.amplifyframework.core.model.Model",
30+
"javaClassForValue": "com.amplifyframework.datastore.appsync.SerializedModel",
3131
"targetType": "BlogOwner",
3232
"isRequired": true,
3333
"isArray": false,
@@ -59,7 +59,7 @@
5959
}
6060
},
6161
"indexes": {},
62-
"modelClass": "com.amplifyframework.core.model.Model"
62+
"modelClass": "com.amplifyframework.datastore.appsync.SerializedModel"
6363
},
6464
"serializedData": {
6565
"owner": "2cb080ce-bc93-44c6-aa77-f985af311afa",

aws-api-appsync/src/test/resources/serde-for-meeting-in-serialized-model.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
},
6969
"associations": {},
7070
"indexes": {},
71-
"modelClass": "com.amplifyframework.core.model.Model"
71+
"modelClass": "com.amplifyframework.datastore.appsync.SerializedModel"
7272
},
7373
"serializedData": {
7474
"date": "2020-11-05Z",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"associations": {
3+
"posts": {
4+
"associatedName": "author",
5+
"associatedType": "PostAuthorJoin",
6+
"name": "HasMany"
7+
}
8+
},
9+
"authRules": [],
10+
"fields": {
11+
"id": {
12+
"authRules": [],
13+
"isArray": false,
14+
"isEnum": false,
15+
"isModel": false,
16+
"isRequired": true,
17+
"javaClassForValue": "java.lang.String",
18+
"name": "id",
19+
"targetType": "ID"
20+
},
21+
"name": {
22+
"authRules": [],
23+
"isArray": false,
24+
"isEnum": false,
25+
"isModel": false,
26+
"isRequired": true,
27+
"javaClassForValue": "java.lang.String",
28+
"name": "name",
29+
"targetType": "String"
30+
},
31+
"posts": {
32+
"authRules": [],
33+
"isArray": true,
34+
"isEnum": false,
35+
"isModel": false,
36+
"isRequired": false,
37+
"javaClassForValue": "java.util.List",
38+
"name": "posts",
39+
"targetType": "PostAuthorJoin"
40+
}
41+
},
42+
"indexes": {},
43+
"modelClass": "com.amplifyframework.datastore.appsync.SerializedModel",
44+
"name": "Author",
45+
"pluralName": "Authors"
46+
}
47+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"associations": {
3+
"blog": {
4+
"associatedName": "owner",
5+
"associatedType": "Blog",
6+
"name": "HasOne"
7+
}
8+
},
9+
"authRules": [],
10+
"fields": {
11+
"blog": {
12+
"authRules": [],
13+
"isArray": false,
14+
"isEnum": false,
15+
"isModel": true,
16+
"isRequired": false,
17+
"javaClassForValue": "com.amplifyframework.datastore.appsync.SerializedModel",
18+
"name": "blog",
19+
"targetType": "Blog"
20+
},
21+
"id": {
22+
"authRules": [],
23+
"isArray": false,
24+
"isEnum": false,
25+
"isModel": false,
26+
"isRequired": true,
27+
"javaClassForValue": "java.lang.String",
28+
"name": "id",
29+
"targetType": "ID"
30+
},
31+
"name": {
32+
"authRules": [],
33+
"isArray": false,
34+
"isEnum": false,
35+
"isModel": false,
36+
"isRequired": true,
37+
"javaClassForValue": "java.lang.String",
38+
"name": "name",
39+
"targetType": "String"
40+
},
41+
"wea": {
42+
"authRules": [],
43+
"isArray": false,
44+
"isEnum": false,
45+
"isModel": false,
46+
"isRequired": false,
47+
"javaClassForValue": "java.lang.String",
48+
"name": "wea",
49+
"targetType": "String"
50+
}
51+
},
52+
"indexes": {},
53+
"modelClass": "com.amplifyframework.datastore.appsync.SerializedModel",
54+
"name": "BlogOwner",
55+
"pluralName": "BlogOwners"
56+
}
57+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"associations": {
3+
"owner": {
4+
"associatedType": "BlogOwner",
5+
"name": "BelongsTo",
6+
"targetName": "blogOwnerId"
7+
},
8+
"posts": {
9+
"associatedName": "blog",
10+
"associatedType": "Post",
11+
"name": "HasMany"
12+
}
13+
},
14+
"authRules": [],
15+
"fields": {
16+
"id": {
17+
"authRules": [],
18+
"isArray": false,
19+
"isEnum": false,
20+
"isModel": false,
21+
"isRequired": true,
22+
"javaClassForValue": "java.lang.String",
23+
"name": "id",
24+
"targetType": "ID"
25+
},
26+
"name": {
27+
"authRules": [],
28+
"isArray": false,
29+
"isEnum": false,
30+
"isModel": false,
31+
"isRequired": true,
32+
"javaClassForValue": "java.lang.String",
33+
"name": "name",
34+
"targetType": "String"
35+
},
36+
"owner": {
37+
"authRules": [],
38+
"isArray": false,
39+
"isEnum": false,
40+
"isModel": true,
41+
"isRequired": true,
42+
"javaClassForValue": "com.amplifyframework.datastore.appsync.SerializedModel",
43+
"name": "owner",
44+
"targetType": "BlogOwner"
45+
},
46+
"posts": {
47+
"authRules": [],
48+
"isArray": true,
49+
"isEnum": false,
50+
"isModel": true,
51+
"isRequired": false,
52+
"javaClassForValue": "com.amplifyframework.datastore.appsync.SerializedModel",
53+
"name": "posts",
54+
"targetType": "Post"
55+
}
56+
},
57+
"indexes": {},
58+
"modelClass": "com.amplifyframework.datastore.appsync.SerializedModel",
59+
"name": "Blog",
60+
"pluralName": "Blogs"
61+
}
62+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"associations": {
3+
"post": {
4+
"associatedType": "Post",
5+
"name": "BelongsTo",
6+
"targetName": "commentPostId"
7+
}
8+
},
9+
"authRules": [],
10+
"fields": {
11+
"content": {
12+
"authRules": [],
13+
"isArray": false,
14+
"isEnum": false,
15+
"isModel": false,
16+
"isRequired": false,
17+
"javaClassForValue": "java.lang.String",
18+
"name": "content",
19+
"targetType": "String"
20+
},
21+
"id": {
22+
"authRules": [],
23+
"isArray": false,
24+
"isEnum": false,
25+
"isModel": false,
26+
"isRequired": true,
27+
"javaClassForValue": "java.lang.String",
28+
"name": "id",
29+
"targetType": "ID"
30+
},
31+
"post": {
32+
"authRules": [],
33+
"isArray": false,
34+
"isEnum": false,
35+
"isModel": true,
36+
"isRequired": false,
37+
"javaClassForValue": "com.amplifyframework.datastore.appsync.SerializedModel",
38+
"name": "post",
39+
"targetType": "Post"
40+
}
41+
},
42+
"indexes": {},
43+
"modelClass": "com.amplifyframework.datastore.appsync.SerializedModel",
44+
"name": "Comment",
45+
"pluralName": "Comments"
46+
}
47+

0 commit comments

Comments
 (0)