mirror of
https://github.com/n8n-io/n8n.git
synced 2026-10-17 13:39:44 +02:00
fix(editor): Sorting data table rows by unknown column returns raw database error (#39733)
Co-authored-by: n8n-cat-bot[bot] <n8n-cat-bot[bot]@users.noreply.github.com>
This commit is contained in:
parent
194f13d30f
commit
b8cda55e57
|
|
@ -3475,6 +3475,40 @@ describe('dataTable', () => {
|
|||
});
|
||||
|
||||
describe('getManyRowsAndCount', () => {
|
||||
it('rejects a sort column that does not exist', async () => {
|
||||
const { id: dataTableId } = await dataTableService.createDataTable(project1.id, {
|
||||
name: 'dataTable',
|
||||
columns: [{ name: 'name', type: 'string' }],
|
||||
});
|
||||
await dataTableService.insertRows(dataTableId, project1.id, [{ name: 'Alice' }]);
|
||||
|
||||
const result = dataTableService.getManyRowsAndCount(dataTableId, project1.id, {
|
||||
sortBy: ['bogusCol', 'ASC'],
|
||||
});
|
||||
|
||||
await expect(result).rejects.toThrow(DataTableValidationError);
|
||||
await expect(result).rejects.toThrow(
|
||||
"Validation error with data table request: unknown column name 'bogusCol'",
|
||||
);
|
||||
});
|
||||
|
||||
it.each(['name', 'id', 'createdAt', 'updatedAt'])(
|
||||
'sorts by existing column %s',
|
||||
async (columnName) => {
|
||||
const { id: dataTableId } = await dataTableService.createDataTable(project1.id, {
|
||||
name: 'dataTable',
|
||||
columns: [{ name: 'name', type: 'string' }],
|
||||
});
|
||||
await dataTableService.insertRows(dataTableId, project1.id, [{ name: 'Alice' }]);
|
||||
|
||||
const result = await dataTableService.getManyRowsAndCount(dataTableId, project1.id, {
|
||||
sortBy: [columnName, 'ASC'],
|
||||
});
|
||||
expect(result.count).toBe(1);
|
||||
expect(result.data[0].name).toBe('Alice');
|
||||
},
|
||||
);
|
||||
|
||||
it('retrieves rows correctly', async () => {
|
||||
// ARRANGE
|
||||
const { id: dataTableId } = await dataTableService.createDataTable(project1.id, {
|
||||
|
|
|
|||
|
|
@ -335,6 +335,15 @@ export class DataTableService {
|
|||
|
||||
return await this.dataTableColumnRepository.manager.transaction(async (em) => {
|
||||
const columns = await this.dataTableColumnRepository.getColumns(dataTableId, em);
|
||||
const sortColumn = dto.sortBy?.[0];
|
||||
if (
|
||||
sortColumn &&
|
||||
!Object.hasOwn(DATA_TABLE_SYSTEM_COLUMN_TYPE_MAP, sortColumn) &&
|
||||
!columns.some((column) => column.name === sortColumn)
|
||||
) {
|
||||
throw new DataTableValidationError(`unknown column name '${sortColumn}'`);
|
||||
}
|
||||
|
||||
const transformedDto = dto.filter
|
||||
? { ...dto, filter: this.validateAndTransformFilters(dto.filter, columns) }
|
||||
: dto;
|
||||
|
|
|
|||
|
|
@ -963,6 +963,22 @@ describe('GET /data-tables/:dataTableId/rows', () => {
|
|||
expect(response.body.message).toBe('Invalid sort direction');
|
||||
});
|
||||
|
||||
test('should reject an unknown sort column', async () => {
|
||||
const dataTable = await createDataTable(ownerPersonalProject, {
|
||||
columns: [{ name: 'name', type: 'string' }],
|
||||
data: [{ name: 'Alice' }],
|
||||
});
|
||||
|
||||
const response = await authOwnerAgent
|
||||
.get(`/data-tables/${dataTable.id}/rows`)
|
||||
.query({ sortBy: 'bogusCol:asc' });
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.body.message).toBe(
|
||||
"Validation error with data table request: unknown column name 'bogusCol'",
|
||||
);
|
||||
});
|
||||
|
||||
test('should sort by system column (id)', async () => {
|
||||
const dataTable = await createDataTable(ownerPersonalProject, {
|
||||
columns: [{ name: 'name', type: 'string' }],
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user