diff --git a/packages/core/actions/package.json b/packages/core/actions/package.json index 4e28454b1..ffb36b198 100644 --- a/packages/core/actions/package.json +++ b/packages/core/actions/package.json @@ -10,6 +10,9 @@ "@nocobase/database": "0.9.1-alpha.1", "@nocobase/resourcer": "0.9.1-alpha.1" }, + "devDependencies": { + "@nocobase/test": "0.9.1-alpha.1" + }, "repository": { "type": "git", "url": "git+https://github.com/nocobase/nocobase.git", diff --git a/packages/core/actions/src/__tests__/get-action.test.ts b/packages/core/actions/src/__tests__/get-action.test.ts index 1ed07a76b..6318d1a64 100644 --- a/packages/core/actions/src/__tests__/get-action.test.ts +++ b/packages/core/actions/src/__tests__/get-action.test.ts @@ -1,4 +1,4 @@ -import { MockServer, mockServer } from './index'; +import { MockServer, mockServer } from '@nocobase/test'; import { registerActions } from '@nocobase/actions'; describe('get action', () => { @@ -10,7 +10,9 @@ describe('get action', () => { let Profile; beforeEach(async () => { - app = mockServer(); + app = mockServer({ + dataWrapping: false, + }); registerActions(app); PostTag = app.collection({ @@ -120,11 +122,13 @@ describe('get action', () => { const postProfile = await Profile.repository.findOne(); - const response = await app - .agent() - .resource('posts.profile', p1.get('id')) - .get(); + const response = await app.agent().resource('posts.profile', p1.get('id')).get(); expect(response.body['id']).toEqual(postProfile.get('id')); }); + + it('should return null when source model not found', async () => { + const response = await app.agent().resource('posts.profile', 999).get(); + expect(response.status).toEqual(200); + }); }); diff --git a/packages/core/actions/src/__tests__/list-action.test.ts b/packages/core/actions/src/__tests__/list-action.test.ts index 10a3e1171..a8d8d4595 100644 --- a/packages/core/actions/src/__tests__/list-action.test.ts +++ b/packages/core/actions/src/__tests__/list-action.test.ts @@ -118,4 +118,14 @@ describe('list action', () => { expect(body.count).toEqual(2); expect(body.rows).toEqual([{ id: 1 }, { id: 2 }]); }); + + it('should return empty error when relation not exists', async () => { + const response = await app + .agent() + .resource('posts.tags', 999) + .list({ fields: ['id', 'postsTags.createdAt'], sort: ['id'] }); + + expect(response.status).toEqual(200); + expect(response.body.count).toEqual(0); + }); }); diff --git a/packages/core/actions/src/actions/get.ts b/packages/core/actions/src/actions/get.ts index 3db4db6ce..37a300bc6 100644 --- a/packages/core/actions/src/actions/get.ts +++ b/packages/core/actions/src/actions/get.ts @@ -16,6 +16,7 @@ export async function get(ctx: Context, next) { }); ctx.body = instance || null; + ctx.status = 200; await next(); } diff --git a/packages/core/database/src/relation-repository/multiple-relation-repository.ts b/packages/core/database/src/relation-repository/multiple-relation-repository.ts index cf72a29e4..eabf749bd 100644 --- a/packages/core/database/src/relation-repository/multiple-relation-repository.ts +++ b/packages/core/database/src/relation-repository/multiple-relation-repository.ts @@ -42,6 +42,8 @@ export abstract class MultipleRelationRepository extends RelationRepository { const getAccessor = this.accessors().get; const sourceModel = await this.getSourceModel(transaction); + if (!sourceModel) return []; + if (findOptions.include && findOptions.include.length > 0) { const ids = ( await sourceModel[getAccessor]({ @@ -103,6 +105,8 @@ export abstract class MultipleRelationRepository extends RelationRepository { const transaction = await this.getTransaction(options); const sourceModel = await this.getSourceModel(transaction); + if (!sourceModel) return 0; + const queryOptions = this.buildQueryOptions(options); const count = await sourceModel[this.accessors().get]({ diff --git a/packages/core/database/src/relation-repository/single-relation-repository.ts b/packages/core/database/src/relation-repository/single-relation-repository.ts index 588cb7d6b..ca99176b9 100644 --- a/packages/core/database/src/relation-repository/single-relation-repository.ts +++ b/packages/core/database/src/relation-repository/single-relation-repository.ts @@ -54,6 +54,8 @@ export abstract class SingleRelationRepository extends RelationRepository { const getAccessor = this.accessors().get; const sourceModel = await this.getSourceModel(transaction); + if (!sourceModel) return null; + if (findOptions?.include?.length > 0) { const templateModel = await sourceModel[getAccessor]({ ...findOptions, diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index 739cb4e67..08dffc3b9 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -320,6 +320,7 @@ export class Application exten const handleRequest = (req, res) => { const ctx = this.createContext(req, res); + // @ts-ignore return this.handleRequest(ctx, fn); };