Skip to content

A platform for community discussion. Free, open, simple.

FIX: Don't delete keys for unsuccessfully hydrated subtypes (#33615)

Lincac4e876fe2f68
diff --git a/app/assets/javascripts/discourse/app/services/store.js b/app/assets/javascripts/discourse/app/services/store.js
index 09f1639838b..bc7544a8410 100644
--- a/app/assets/javascripts/discourse/app/services/store.js
+++ b/app/assets/javascripts/discourse/app/services/store.js
@@ -360,12 +360,14 @@ export default class StoreService extends Service {
return;
}
- const hydrated = obj[k].map((id) =>
- this._lookupSubType(subType, type, id, root)
- );
+ const hydrated = obj[k]
+ .map((id) => this._lookupSubType(subType, type, id, root))
+ .compact();
- obj[this.pluralize(subType)] = hydrated || [];
- delete obj[k];
+ obj[this.pluralize(subType)] = hydrated;
+ if (hydrated.length !== 0) {
+ delete obj[k];
+ }
} else {
const hydrated = this._lookupSubType(subType, type, obj[k], root);
if (hydrated) {
diff --git a/app/assets/javascripts/discourse/tests/helpers/store-pretender.js b/app/assets/javascripts/discourse/tests/helpers/store-pretender.js
index 6094fa5ea5d..e6b6d3f0e11 100644
--- a/app/assets/javascripts/discourse/tests/helpers/store-pretender.js
+++ b/app/assets/javascripts/discourse/tests/helpers/store-pretender.js
@@ -135,4 +135,23 @@ export default function (helpers) {
});
this.delete("/widgets/:widget_id", success);
+
+ this.get("/complex_things", function () {
+ return response({
+ __rest_serializer: "1",
+ complex_things: [
+ {
+ id: 1,
+ foo_ids: [1, 2, 3, 4],
+ bar_ids: [5, 6, 7, 8],
+ },
+ ],
+ foos: [
+ { id: 1, name: "foo1" },
+ { id: 2, name: "foo2" },
+ { id: 3, name: "foo3" },
+ { id: 4, name: "foo4" },
+ ],
+ });
+ });
}
diff --git a/app/assets/javascripts/discourse/tests/unit/services/store-test.js b/app/assets/javascripts/discourse/tests/unit/services/store-test.js
index 9755620d087..3191bb5658f 100644
--- a/app/assets/javascripts/discourse/tests/unit/services/store-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/services/store-test.js
@@ -274,4 +274,22 @@ module("Unit | Service | store", function (hooks) {
"embedded record remains unhydrated"
);
});
+
+ test("hydrateEmbedded", async function (assert) {
+ const store = getOwner(this).lookup("service:store");
+ const things = await store.findAll("complex_thing");
+ const thing = things.content[0];
+
+ assert.propContains(
+ thing.foos[0],
+ { id: 1, name: "foo1" },
+ "it hydrates the embedded records"
+ );
+
+ assert.deepEqual(
+ thing.bar_ids,
+ [5, 6, 7, 8],
+ "it won't delete unhydrated ids"
+ );
+ });
});