Merge pull request #607 from atmire/allow-optional-links

Allow optional HALLinks
This commit is contained in:
Tim Donohue
2020-03-03 10:30:04 -06:00
committed by GitHub
2 changed files with 58 additions and 8 deletions

View File

@@ -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 */

View File

@@ -55,7 +55,9 @@ 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) {
@@ -67,6 +69,7 @@ export class LinkService {
throw new Error(`Something went wrong when using @dataService(${matchingLinkDef.resourceType.value}) ${hasValue(service) ? '' : '(undefined) '}to resolve link ${linkToFollow.name} from ${href}`);
}
}
}
return model;
}