From d543ba965610927566b2921d810aa51004827a7d Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Mon, 13 Dec 2021 12:34:51 -0500 Subject: [PATCH 1/2] Make sure `this.data.attributes` is initialized. `Object.assign` is not type-safe, and does ensure that the property being assigned to is not undefined. --- framework/core/js/src/common/Model.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/core/js/src/common/Model.ts b/framework/core/js/src/common/Model.ts index 9cea14335..338b66207 100644 --- a/framework/core/js/src/common/Model.ts +++ b/framework/core/js/src/common/Model.ts @@ -110,6 +110,7 @@ export default abstract class Model { } if ('attributes' in data) { + this.data.attributes ||= {}; Object.assign(this.data.attributes, data.attributes); } From d494a6a7d078945d863dc31ae29e69b7099d26f0 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Mon, 13 Dec 2021 15:07:30 -0500 Subject: [PATCH 2/2] Don't throw errors for undefined relationships --- framework/core/js/src/common/Model.ts | 28 ++++++++++++--------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/framework/core/js/src/common/Model.ts b/framework/core/js/src/common/Model.ts index 338b66207..1bc29431e 100644 --- a/framework/core/js/src/common/Model.ts +++ b/framework/core/js/src/common/Model.ts @@ -296,16 +296,14 @@ export default abstract class Model { static hasOne(name: string): () => M | null | false; static hasOne(name: string): () => M | false { return function (this: Model) { - if (this.data.relationships) { - const relationshipData = this.data.relationships[name]?.data; + const relationshipData = this.data.relationships?.[name]?.data; - if (relationshipData instanceof Array) { - throw new Error(`Relationship ${name} on model ${this.data.type} is plural, so the hasOne method cannot be used to access it.`); - } + if (relationshipData && relationshipData instanceof Array) { + throw new Error(`Relationship ${name} on model ${this.data.type} is plural, so the hasOne method cannot be used to access it.`); + } - if (relationshipData) { - return this.store.getById(relationshipData.type, relationshipData.id) as M; - } + if (relationshipData) { + return this.store.getById(relationshipData.type, relationshipData.id) as M; } return false; @@ -322,16 +320,14 @@ export default abstract class Model { */ static hasMany(name: string): () => (M | undefined)[] | false { return function (this: Model) { - if (this.data.relationships) { - const relationshipData = this.data.relationships[name]?.data; + const relationshipData = this.data.relationships?.[name]?.data; - if (!(relationshipData instanceof Array)) { - throw new Error(`Relationship ${name} on model ${this.data.type} is singular, so the hasMany method cannot be used to access it.`); - } + if (relationshipData && !(relationshipData instanceof Array)) { + throw new Error(`Relationship ${name} on model ${this.data.type} is singular, so the hasMany method cannot be used to access it.`); + } - if (relationshipData) { - return relationshipData.map((data) => this.store.getById(data.type, data.id)); - } + if (relationshipData) { + return relationshipData.map((data) => this.store.getById(data.type, data.id)); } return false;