Skip to content

Commit

Permalink
fix(core): ensure correct handling of empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Nov 7, 2020
1 parent 83bd6b3 commit 1c4ba75
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/platforms/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ export abstract class Platform {
}

unmarshallArray(value: string): string[] {
if (value === '') {
return [];
}

return value.split(',') as string[];
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/ArrayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ArrayType<T extends string | number = string> extends Type<T[] | nu
}

convertToJSValue(value: T[] | string | null, platform: Platform): T[] | null {
if (!value) {
if (!Utils.isDefined(value, true)) {
return value as null;
}

Expand Down
8 changes: 5 additions & 3 deletions tests/EntityManager.mongo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1988,27 +1988,29 @@ describe('EntityManagerMongo', () => {
test('custom types', async () => {
const bar = FooBar.create('b1');
bar.blob = Buffer.from([1, 2, 3, 4, 5]);
bar.array = [1, 2, 3, 4, 5];
bar.array = [];
bar.object = { foo: 'bar', bar: 3 };
await orm.em.persistAndFlush(bar);
orm.em.clear();

const b1 = await orm.em.findOneOrFail(FooBar, bar.id);
expect(b1.blob).toEqual(Buffer.from([1, 2, 3, 4, 5]));
expect(b1.blob).toBeInstanceOf(Buffer);
expect(b1.array).toEqual([1, 2, 3, 4, 5]);
expect(b1.array![2]).toBe(3);
expect(b1.array).toEqual([]);
expect(b1.array).toBeInstanceOf(Array);
expect(b1.object).toEqual({ foo: 'bar', bar: 3 });
expect(b1.object).toBeInstanceOf(Object);
expect(b1.object!.bar).toBe(3);

b1.object = 'foo';
b1.array = [1, 2, 3, 4, 5];
await orm.em.flush();
orm.em.clear();

const b2 = await orm.em.findOneOrFail(FooBar, bar.id);
expect(b2.object).toBe('foo');
expect(b2.array).toEqual([1, 2, 3, 4, 5]);
expect(b2.array![2]).toBe(3);

b2.object = [1, 2, '3'];
await orm.em.flush();
Expand Down
8 changes: 5 additions & 3 deletions tests/EntityManager.mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2339,26 +2339,28 @@ describe('EntityManagerMySql', () => {
test('custom types', async () => {
const bar = FooBar2.create('b1');
bar.blob = Buffer.from([1, 2, 3, 4, 5]);
bar.array = [1, 2, 3, 4, 5];
bar.array = [];
bar.object = { foo: 'bar', bar: 3 };
await orm.em.persistAndFlush(bar);
orm.em.clear();

const b1 = await orm.em.findOneOrFail(FooBar2, bar.id);
expect(b1.blob).toEqual(Buffer.from([1, 2, 3, 4, 5]));
expect(b1.blob).toBeInstanceOf(Buffer);
expect(b1.array).toEqual([1, 2, 3, 4, 5]);
expect(b1.array![2]).toBe(3);
expect(b1.array).toEqual([]);
expect(b1.array).toBeInstanceOf(Array);
expect(b1.object).toEqual({ foo: 'bar', bar: 3 });
expect(b1.object).toBeInstanceOf(Object);
expect(b1.object!.bar).toBe(3);

b1.object = 'foo';
b1.array = [1, 2, 3, 4, 5];
await orm.em.flush();
orm.em.clear();

const b2 = await orm.em.findOneOrFail(FooBar2, bar.id);
expect(b2.array).toEqual([1, 2, 3, 4, 5]);
expect(b2.array![2]).toBe(3);
expect(b2.object).toBe('foo');

b2.object = [1, 2, '3'];
Expand Down
8 changes: 5 additions & 3 deletions tests/EntityManager.postgre.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1458,27 +1458,29 @@ describe('EntityManagerPostgre', () => {
test('custom types', async () => {
const bar = FooBar2.create('b1 "b" \'1\'');
bar.blob = Buffer.from([1, 2, 3, 4, 5]);
bar.array = [1, 2, 3, 4, 5];
bar.array = [];
bar.object = { foo: `bar 'lol' baz "foo"`, bar: 3 };
await orm.em.persistAndFlush(bar);
orm.em.clear();

const b1 = await orm.em.findOneOrFail(FooBar2, bar.id);
expect(b1.blob).toEqual(Buffer.from([1, 2, 3, 4, 5]));
expect(b1.blob).toBeInstanceOf(Buffer);
expect(b1.array).toEqual([1, 2, 3, 4, 5]);
expect(b1.array![2]).toBe(3);
expect(b1.array).toEqual([]);
expect(b1.array).toBeInstanceOf(Array);
expect(b1.object).toEqual({ foo: `bar 'lol' baz "foo"`, bar: 3 });
expect(b1.object).toBeInstanceOf(Object);
expect(b1.object!.bar).toBe(3);

b1.object = 'foo';
b1.array = [1, 2, 3, 4, 5];
await orm.em.flush();
orm.em.clear();

const b2 = await orm.em.findOneOrFail(FooBar2, bar.id);
expect(b2.object).toBe('foo');
expect(b2.array).toEqual([1, 2, 3, 4, 5]);
expect(b2.array![2]).toBe(3);

b2.object = [1, 2, '3'];
await orm.em.flush();
Expand Down
8 changes: 5 additions & 3 deletions tests/EntityManager.sqlite2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,27 +836,29 @@ describe('EntityManagerSqlite2', () => {
test('custom types', async () => {
const bar = orm.em.create(FooBar4, { name: 'b1 \'the bad\' lol' });
bar.blob = Buffer.from([1, 2, 3, 4, 5]);
bar.array = [1, 2, 3, 4, 5];
bar.array = [];
bar.object = { foo: 'bar "lol" \'wut\' escaped', bar: 3 };
await orm.em.persistAndFlush(bar);
orm.em.clear();

const b1 = await orm.em.findOneOrFail(FooBar4, bar.id);
expect(b1.blob).toEqual(Buffer.from([1, 2, 3, 4, 5]));
expect(b1.blob).toBeInstanceOf(Buffer);
expect(b1.array).toEqual([1, 2, 3, 4, 5]);
expect(b1.array![2]).toBe(3);
expect(b1.array).toEqual([]);
expect(b1.array).toBeInstanceOf(Array);
expect(b1.object).toEqual({ foo: 'bar "lol" \'wut\' escaped', bar: 3 });
expect(b1.object).toBeInstanceOf(Object);
expect(b1.object!.bar).toBe(3);

b1.object = 'foo';
b1.array = [1, 2, 3, 4, 5];
await orm.em.flush();
orm.em.clear();

const b2 = await orm.em.findOneOrFail(FooBar4, bar.id);
expect(b2.object).toBe('foo');
expect(b2.array).toEqual([1, 2, 3, 4, 5]);
expect(b2.array![2]).toBe(3);

b2.object = [1, 2, '3'];
await orm.em.flush();
Expand Down

0 comments on commit 1c4ba75

Please sign in to comment.