From 8ddc62168ef82f32d00a89ce387003c49ad21af1 Mon Sep 17 00:00:00 2001 From: Alexandre Vryghem Date: Mon, 19 Feb 2024 18:56:43 +0100 Subject: [PATCH] 108915: Prevent self links & unresolved followLinks to be added as dependencies --- .../core/data/base/base-data.service.spec.ts | 6 ++-- src/app/core/data/base/base-data.service.ts | 30 +++++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/app/core/data/base/base-data.service.spec.ts b/src/app/core/data/base/base-data.service.spec.ts index c10f52d8a50..1ea35756d33 100644 --- a/src/app/core/data/base/base-data.service.spec.ts +++ b/src/app/core/data/base/base-data.service.spec.ts @@ -78,6 +78,8 @@ describe('BaseDataService', () => { const msToLive = 15 * 60 * 1000; const payload = { foo: 'bar', + followLink1: {}, + followLink2: {}, _links: { self: Object.assign(new HALLink(), { href: 'self-test-link', @@ -407,7 +409,7 @@ describe('BaseDataService', () => { expectObservable(service.findByHref(selfLink, false, false, ...linksToFollow)).toBe(expected, values); flush(); - expect(objectCache.addDependency).toHaveBeenCalledTimes(4); + expect(objectCache.addDependency).toHaveBeenCalledTimes(3); }); }); }); @@ -610,7 +612,7 @@ describe('BaseDataService', () => { expectObservable(service.findListByHref(selfLink, findListOptions, false, false, ...linksToFollow)).toBe(expected, values); flush(); - expect(objectCache.addDependency).toHaveBeenCalledTimes(4); + expect(objectCache.addDependency).toHaveBeenCalledTimes(3); }); }); }); diff --git a/src/app/core/data/base/base-data.service.ts b/src/app/core/data/base/base-data.service.ts index 473dbae0c73..aef680578b2 100644 --- a/src/app/core/data/base/base-data.service.ts +++ b/src/app/core/data/base/base-data.service.ts @@ -282,12 +282,15 @@ export class BaseDataService implements HALDataServic // Ensure all followLinks from the cached object are automatically invalidated when invalidating the cached object tap((remoteDataObject: RemoteData) => { if (hasValue(remoteDataObject?.payload?._links)) { - for (const followLink of Object.values(remoteDataObject.payload._links)) { - // followLink can be either an individual HALLink or a HALLink[] - const followLinksList: HALLink[] = [].concat(followLink); - for (const individualFollowLink of followLinksList) { - if (hasValue(individualFollowLink?.href)) { - this.addDependency(response$, individualFollowLink.href); + for (const followLinkName of Object.keys(remoteDataObject.payload._links)) { + // only add the followLinks if they are embedded + if (hasValue(remoteDataObject.payload[followLinkName]) && followLinkName !== 'self') { + // followLink can be either an individual HALLink or a HALLink[] + const followLinksList: HALLink[] = [].concat(remoteDataObject.payload._links[followLinkName]); + for (const individualFollowLink of followLinksList) { + if (hasValue(individualFollowLink?.href)) { + this.addDependency(response$, individualFollowLink.href); + } } } } @@ -334,12 +337,15 @@ export class BaseDataService implements HALDataServic if (hasValue(remoteDataObject?.payload?.page)) { for (const object of remoteDataObject.payload.page) { if (hasValue(object?._links)) { - for (const followLink of Object.values(object._links)) { - // followLink can be either an individual HALLink or a HALLink[] - const followLinksList: HALLink[] = [].concat(followLink); - for (const individualFollowLink of followLinksList) { - if (hasValue(individualFollowLink?.href)) { - this.addDependency(response$, individualFollowLink.href); + for (const followLinkName of Object.keys(object._links)) { + // only add the followLinks if they are embedded + if (hasValue(object[followLinkName]) && followLinkName !== 'self') { + // followLink can be either an individual HALLink or a HALLink[] + const followLinksList: HALLink[] = [].concat(object._links[followLinkName]); + for (const individualFollowLink of followLinksList) { + if (hasValue(individualFollowLink?.href)) { + this.addDependency(response$, individualFollowLink.href); + } } } }