|
28 | 28 | import com.amplifyframework.core.Consumer; |
29 | 29 | import com.amplifyframework.core.async.Cancelable; |
30 | 30 | import com.amplifyframework.core.model.Model; |
| 31 | +import com.amplifyframework.core.model.ModelAssociation; |
31 | 32 | import com.amplifyframework.core.model.ModelField; |
32 | 33 | import com.amplifyframework.core.model.ModelProvider; |
33 | 34 | import com.amplifyframework.core.model.ModelSchema; |
@@ -416,7 +417,6 @@ public <T extends Model> void query( |
416 | 417 | /** |
417 | 418 | * {@inheritDoc} |
418 | 419 | */ |
419 | | - @SuppressWarnings("unchecked") |
420 | 420 | @Override |
421 | 421 | public void query( |
422 | 422 | @NonNull String modelName, |
@@ -447,26 +447,8 @@ public void query( |
447 | 447 |
|
448 | 448 | if (cursor.moveToFirst()) { |
449 | 449 | do { |
450 | | - final Map<String, Object> serializedData = new HashMap<>(); |
451 | | - for (Map.Entry<String, Object> entry : converter.buildMapForModel(cursor).entrySet()) { |
452 | | - ModelField field = modelSchema.getFields().get(entry.getKey()); |
453 | | - if (field == null || entry.getValue() == null) { |
454 | | - // Skip it |
455 | | - } else if (field.isModel()) { |
456 | | - String id = (String) ((Map<String, Object>) entry.getValue()).get("id"); |
457 | | - serializedData.put(entry.getKey(), SerializedModel.builder() |
458 | | - .serializedData(Collections.singletonMap("id", id)) |
459 | | - .modelSchema(null) |
460 | | - .build() |
461 | | - ); |
462 | | - } else { |
463 | | - serializedData.put(entry.getKey(), entry.getValue()); |
464 | | - } |
465 | | - } |
466 | | - SerializedModel model = SerializedModel.builder() |
467 | | - .serializedData(serializedData) |
468 | | - .modelSchema(modelSchema) |
469 | | - .build(); |
| 450 | + final Map<String, Object> data = converter.buildMapForModel(cursor); |
| 451 | + final SerializedModel model = createSerializedModel(modelSchema, data); |
470 | 452 | models.add(model); |
471 | 453 | } while (cursor.moveToNext()); |
472 | 454 | } |
@@ -831,4 +813,36 @@ private Completable updateModels() { |
831 | 813 | return PersistentModelVersion.saveToLocalStorage(this, persistentModelVersion); |
832 | 814 | }).ignoreElement(); |
833 | 815 | } |
| 816 | + |
| 817 | + /** |
| 818 | + * recursively creates nested SerializedModels from raw data. |
| 819 | + */ |
| 820 | + private SerializedModel createSerializedModel(ModelSchema modelSchema, Map<String, Object> data) { |
| 821 | + final Map<String, Object> serializedData = new HashMap<>(); |
| 822 | + for (Map.Entry<String, Object> entry : data.entrySet()) { |
| 823 | + ModelField field = modelSchema.getFields().get(entry.getKey()); |
| 824 | + if (field != null && entry.getValue() != null) { |
| 825 | + if (field.isModel()) { |
| 826 | + ModelAssociation association = modelSchema.getAssociations().get(entry.getKey()); |
| 827 | + if (association != null) { |
| 828 | + String associatedType = association.getAssociatedType(); |
| 829 | + final ModelSchema nestedModelSchema = modelSchemaRegistry.getModelSchemaForModelClass( |
| 830 | + associatedType |
| 831 | + ); |
| 832 | + @SuppressWarnings("unchecked") |
| 833 | + SerializedModel model = createSerializedModel( |
| 834 | + nestedModelSchema, (Map<String, Object>) entry.getValue() |
| 835 | + ); |
| 836 | + serializedData.put(entry.getKey(), model); |
| 837 | + } |
| 838 | + } else { |
| 839 | + serializedData.put(entry.getKey(), entry.getValue()); |
| 840 | + } |
| 841 | + } |
| 842 | + } |
| 843 | + return SerializedModel.builder() |
| 844 | + .serializedData(serializedData) |
| 845 | + .modelSchema(modelSchema) |
| 846 | + .build(); |
| 847 | + } |
834 | 848 | } |
0 commit comments