fix(db): the string sourceKeyValue in relation repository is being converted to a number (#6360)

This commit is contained in:
YANG QIA 2025-03-06 08:59:52 +08:00 committed by GitHub
parent 8d40210948
commit 4b2b1da8ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View File

@ -0,0 +1,43 @@
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { MockDatabase, mockDatabase } from '../../mock-database';
describe('relation repository', () => {
let db: MockDatabase;
beforeEach(() => {
db = mockDatabase();
});
afterEach(async () => {
await db.clean({ drop: true });
await db.close();
});
it('should not convert string source id to number', async () => {
db.collection({
name: 'tags',
fields: [{ type: 'string', name: 'name' }],
});
const User = db.collection({
name: 'users',
autoGenId: false,
fields: [
{ type: 'string', name: 'name' },
{ type: 'hasMany', name: 'tags', sourceKey: 'name', target: 'tags', foreignKey: 'userId' },
],
});
await db.sync();
await User.repository.create({
values: { name: '123', tags: [{ name: 'tag1' }] },
});
const repo = db.getRepository('users.tags', '123');
await expect(repo.find()).resolves.not.toThrow();
});
});

View File

@ -57,7 +57,8 @@ export abstract class RelationRepository {
decodeMultiTargetKey(str: string) {
try {
const decoded = decodeURIComponent(str);
return JSON.parse(decoded);
const parsed = JSON.parse(decoded);
return typeof parsed === 'object' && parsed !== null ? parsed : decoded;
} catch (e) {
return false;
}