Skip to content

Commit

Permalink
fix(postgres): escape array literal values containing backslash (#4797)
Browse files Browse the repository at this point in the history
Closes #4796
  • Loading branch information
Strengthless committed Oct 7, 2023
1 parent afba990 commit 20179ec
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/postgresql/src/PostgreSqlPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class PostgreSqlPlatform extends AbstractSqlPlatform {
}

marshallArray(values: string[]): string {
const quote = (v: string) => v === '' || v.match(/["{},]/) ? JSON.stringify(v) : v;
const quote = (v: string) => v === '' || v.match(/["{},\\]/) ? JSON.stringify(v) : v;
return `{${values.map(v => quote('' + v)).join(',')}}`;
}

Expand Down
55 changes: 55 additions & 0 deletions tests/issues/GH4796.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
Entity,
OptionalProps,
PrimaryKey,
Property,
SimpleLogger,
} from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/postgresql';
import { mockLogger } from '../helpers';

@Entity()
class User {

[OptionalProps]?: 'options';

@PrimaryKey()
id!: number;

@Property({ type: 'string[]', default: ['foo'] })
options = ['foo'];

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [User],
dbName: 'mikro_orm_test_gh_4796',
loggerFactory: options => new SimpleLogger(options),
});
await orm.schema.refreshDatabase();
});

afterAll(async () => {
await orm.close();
});

test('4796', async () => {
const mock = mockLogger(orm, ['query', 'query-params']);
const u1 = orm.em.create(User, { options: ['\\'] });
await orm.em.flush();

expect(mock.mock.calls).toEqual([
['[query] begin'],
[
'[query] insert into "user" ("options") values ( E\'{"\\\\\\\\"}\') returning "id", "options"',
],
['[query] commit'],
]);

const ud = await orm.em.fork().findOne(User, { id: u1.id });

expect(ud).toEqual({ id: 1, options: ['\\'] });
});

0 comments on commit 20179ec

Please sign in to comment.