From 6606520c2f30d1d4f0d441852ab5a10cd7bb9991 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Tue, 25 Feb 2020 15:54:01 +0100 Subject: [PATCH] 68729: Resolve missing links to undefined --- .../core/cache/builders/link.service.spec.ts | 47 +++++++++++++++++++ src/app/core/cache/builders/link.service.ts | 19 ++++---- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/app/core/cache/builders/link.service.spec.ts b/src/app/core/cache/builders/link.service.spec.ts index b34aea320a..dbd65eefb5 100644 --- a/src/app/core/cache/builders/link.service.spec.ts +++ b/src/app/core/cache/builders/link.service.spec.ts @@ -218,5 +218,52 @@ describe('LinkService', () => { }); }); + describe('when a link is missing', () => { + beforeEach(() => { + testModel = Object.assign(new TestModel(), { + value: 'a test value', + _links: { + self: { + href: 'http://self.link' + }, + predecessor: { + href: 'http://predecessor.link' + } + } + }); + spyOnFunction(decorators, 'getDataServiceFor').and.returnValue(TestDataService); + }); + + describe('resolving the available link', () => { + beforeEach(() => { + spyOnFunction(decorators, 'getLinkDefinition').and.returnValue({ + resourceType: TEST_MODEL, + linkName: 'predecessor', + propertyName: 'predecessor' + }); + result = service.resolveLinks(testModel, followLink('predecessor')); + }); + + it('should return the model with the resolved link', () => { + expect(result.predecessor).toBe('findByHref'); + }); + }); + + describe('resolving the missing link', () => { + beforeEach(() => { + spyOnFunction(decorators, 'getLinkDefinition').and.returnValue({ + resourceType: TEST_MODEL, + linkName: 'successor', + propertyName: 'successor' + }); + result = service.resolveLinks(testModel, followLink('successor')); + }); + + it('should return the model with no resolved link', () => { + expect(result.successor).toBeUndefined(); + }); + }); + }); + }); /* tslint:enable:max-classes-per-file */ diff --git a/src/app/core/cache/builders/link.service.ts b/src/app/core/cache/builders/link.service.ts index c41a5484a1..dc65eab68f 100644 --- a/src/app/core/cache/builders/link.service.ts +++ b/src/app/core/cache/builders/link.service.ts @@ -55,16 +55,19 @@ export class LinkService { parent: this.parentInjector }).get(provider); - const href = model._links[matchingLinkDef.linkName].href; + const link = model._links[matchingLinkDef.linkName]; + if (hasValue(link)) { + const href = link.href; - try { - if (matchingLinkDef.isList) { - model[linkToFollow.name] = service.findAllByHref(href, linkToFollow.findListOptions, ...linkToFollow.linksToFollow); - } else { - model[linkToFollow.name] = service.findByHref(href, ...linkToFollow.linksToFollow); + try { + if (matchingLinkDef.isList) { + model[linkToFollow.name] = service.findAllByHref(href, linkToFollow.findListOptions, ...linkToFollow.linksToFollow); + } else { + model[linkToFollow.name] = service.findByHref(href, ...linkToFollow.linksToFollow); + } + } catch (e) { + throw new Error(`Something went wrong when using @dataService(${matchingLinkDef.resourceType.value}) ${hasValue(service) ? '' : '(undefined) '}to resolve link ${linkToFollow.name} from ${href}`); } - } catch (e) { - throw new Error(`Something went wrong when using @dataService(${matchingLinkDef.resourceType.value}) ${hasValue(service) ? '' : '(undefined) '}to resolve link ${linkToFollow.name} from ${href}`); } } return model;