diff --git a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/HybridOfflineInstrumentationTest.java b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/HybridOfflineInstrumentationTest.java index 3dba2c00c5..9b26920755 100644 --- a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/HybridOfflineInstrumentationTest.java +++ b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/HybridOfflineInstrumentationTest.java @@ -165,8 +165,8 @@ public void queryModelsInSerializedForm() throws DataStoreException { serializedBlogData.put("id", blog.getId()); serializedBlogData.put("name", blog.getName()); serializedBlogData.put("owner", SerializedModel.builder() - .serializedData(Collections.singletonMap("id", blogOwner.getId())) - .modelSchema(null) + .serializedData(serializedBlogOwnerData) + .modelSchema(blogOwnerSchema) .build() ); assertEquals( diff --git a/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteStorageAdapter.java b/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteStorageAdapter.java index e0f8507c3a..05bf6bfe49 100644 --- a/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteStorageAdapter.java +++ b/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteStorageAdapter.java @@ -28,6 +28,7 @@ import com.amplifyframework.core.Consumer; import com.amplifyframework.core.async.Cancelable; import com.amplifyframework.core.model.Model; +import com.amplifyframework.core.model.ModelAssociation; import com.amplifyframework.core.model.ModelField; import com.amplifyframework.core.model.ModelProvider; import com.amplifyframework.core.model.ModelSchema; @@ -416,7 +417,6 @@ public void query( /** * {@inheritDoc} */ - @SuppressWarnings("unchecked") @Override public void query( @NonNull String modelName, @@ -447,26 +447,8 @@ public void query( if (cursor.moveToFirst()) { do { - final Map serializedData = new HashMap<>(); - for (Map.Entry entry : converter.buildMapForModel(cursor).entrySet()) { - ModelField field = modelSchema.getFields().get(entry.getKey()); - if (field == null || entry.getValue() == null) { - // Skip it - } else if (field.isModel()) { - String id = (String) ((Map) entry.getValue()).get("id"); - serializedData.put(entry.getKey(), SerializedModel.builder() - .serializedData(Collections.singletonMap("id", id)) - .modelSchema(null) - .build() - ); - } else { - serializedData.put(entry.getKey(), entry.getValue()); - } - } - SerializedModel model = SerializedModel.builder() - .serializedData(serializedData) - .modelSchema(modelSchema) - .build(); + final Map data = converter.buildMapForModel(cursor); + final SerializedModel model = createSerializedModel(modelSchema, data); models.add(model); } while (cursor.moveToNext()); } @@ -831,4 +813,36 @@ private Completable updateModels() { return PersistentModelVersion.saveToLocalStorage(this, persistentModelVersion); }).ignoreElement(); } + + /** + * recursively creates nested SerializedModels from raw data. + */ + private SerializedModel createSerializedModel(ModelSchema modelSchema, Map data) { + final Map serializedData = new HashMap<>(); + for (Map.Entry entry : data.entrySet()) { + ModelField field = modelSchema.getFields().get(entry.getKey()); + if (field != null && entry.getValue() != null) { + if (field.isModel()) { + ModelAssociation association = modelSchema.getAssociations().get(entry.getKey()); + if (association != null) { + String associatedType = association.getAssociatedType(); + final ModelSchema nestedModelSchema = modelSchemaRegistry.getModelSchemaForModelClass( + associatedType + ); + @SuppressWarnings("unchecked") + SerializedModel model = createSerializedModel( + nestedModelSchema, (Map) entry.getValue() + ); + serializedData.put(entry.getKey(), model); + } + } else { + serializedData.put(entry.getKey(), entry.getValue()); + } + } + } + return SerializedModel.builder() + .serializedData(serializedData) + .modelSchema(modelSchema) + .build(); + } }