From d1ab72f2d9afe057583d3563e0eac2b50ba81e9c Mon Sep 17 00:00:00 2001 From: shazha Date: Thu, 28 Feb 2019 13:39:21 +0800 Subject: [PATCH 1/3] fix(repository): returns null if no related model instance found for HasOne relation fix #2472 --- .../acceptance/has-one.relation.acceptance.ts | 14 ++++++++------ .../src/relations/has-one/has-one.repository.ts | 9 +++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts b/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts index 0343a41443e4..3aea266d8fdf 100644 --- a/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts +++ b/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts @@ -75,8 +75,9 @@ describe('hasOne relation', () => { const foundAddress = await controller.findCustomerAddress( existingCustomerId, ); - expect(foundAddress).to.containEql(address); - expect(toJSON(foundAddress)).to.deepEqual(toJSON(address)); + expect(foundAddress).to.not.be.null(); + expect(foundAddress!).to.containEql(address); + expect(toJSON(foundAddress!)).to.deepEqual(toJSON(address)); const persisted = await addressRepo.find({ where: {customerId: existingCustomerId}, @@ -104,15 +105,16 @@ describe('hasOne relation', () => { expect(persisted[0]).to.deepEqual(foundAddress); }); - it('reports EntityNotFound error when related model is deleted', async () => { + it('returns null when related model is deleted', async () => { const address = await controller.createCustomerAddress(existingCustomerId, { street: '123 test avenue', }); await addressRepo.deleteById(address.zipcode); - await expect( - controller.findCustomerAddress(existingCustomerId), - ).to.be.rejectedWith(EntityNotFoundError); + const foundAddress = await controller.findCustomerAddress( + existingCustomerId, + ); + expect(foundAddress).to.be.null(); }); /*---------------- HELPERS -----------------*/ diff --git a/packages/repository/src/relations/has-one/has-one.repository.ts b/packages/repository/src/relations/has-one/has-one.repository.ts index 961e39e25f24..fc2a8f6251c8 100644 --- a/packages/repository/src/relations/has-one/has-one.repository.ts +++ b/packages/repository/src/relations/has-one/has-one.repository.ts @@ -12,7 +12,6 @@ import { constrainFilter, EntityCrudRepository, } from '../../repositories'; -import {EntityNotFoundError} from '../../errors'; /** * CRUD operations for a target repository of a HasMany relation @@ -38,7 +37,7 @@ export interface HasOneRepository { get( filter?: Pick, Exclude, 'where'>>, options?: Options, - ): Promise; + ): Promise; } export class DefaultHasOneRepository< @@ -74,16 +73,14 @@ export class DefaultHasOneRepository< Exclude, 'where'> >, options?: Options, - ): Promise { + ): Promise { const targetRepository = await this.getTargetRepository(); const found = await targetRepository.find( Object.assign({limit: 1}, constrainFilter(filter, this.constraint)), options, ); if (found.length < 1) { - // We don't have a direct access to the foreign key value here :( - const id = 'constraint ' + JSON.stringify(this.constraint); - throw new EntityNotFoundError(targetRepository.entityClass, id); + return null; } return found[0]; } From 94c6b07a8e340a8ba170012932676b4a3619dda1 Mon Sep 17 00:00:00 2001 From: shazha Date: Thu, 28 Feb 2019 14:19:04 +0800 Subject: [PATCH 2/3] fix(repository): fix todo-list example --- .../src/controllers/todo-list-image.controller.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/todo-list/src/controllers/todo-list-image.controller.ts b/examples/todo-list/src/controllers/todo-list-image.controller.ts index d909f4b156bc..e9ff4915f105 100644 --- a/examples/todo-list/src/controllers/todo-list-image.controller.ts +++ b/examples/todo-list/src/controllers/todo-list-image.controller.ts @@ -1,6 +1,6 @@ import {TodoListRepository} from '../repositories'; import {repository} from '@loopback/repository'; -import {param, post, requestBody, get} from '@loopback/rest'; +import {param, post, requestBody, get, HttpErrors} from '@loopback/rest'; import {TodoListImage} from '../models'; export class TodoListImageController { @@ -32,6 +32,10 @@ export class TodoListImageController { }, }) async find(@param.path.number('id') id: number): Promise { - return await this.todoListRepo.image(id).get(); + const foundImage = await this.todoListRepo.image(id).get(); + if (!foundImage) { + throw new HttpErrors.NotFound(); + } + return foundImage; } } From b7c01328d1f0d528f3a0d3dfcc59f14da8fd22cd Mon Sep 17 00:00:00 2001 From: shazha Date: Thu, 28 Feb 2019 15:05:10 +0800 Subject: [PATCH 3/3] fix(repository): remove unused EntityNotFoundError importing in tests --- .../src/__tests__/acceptance/has-one.relation.acceptance.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts b/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts index 3aea266d8fdf..d650daec4d92 100644 --- a/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts +++ b/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts @@ -11,7 +11,6 @@ import { repository, RepositoryMixin, Filter, - EntityNotFoundError, } from '../..'; import {Address} from '../fixtures/models'; import {CustomerRepository, AddressRepository} from '../fixtures/repositories';