-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: calling EntityManager.insert() with an empty array of entities (#…
…5745) As described in issue #5734, the current behaviour seems like an oversight and inconsistent with save() and remove() which already have this handled as a special case. Test Plan: npm test, and more specifically npm test -- --grep='#5734' Closes: #5734
- Loading branch information
Lovro Puzar
authored
May 16, 2020
1 parent
7d8a1ca
commit f8c52f3
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {PrimaryColumn} from "../../../../src/decorator/columns/PrimaryColumn"; | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryColumn() | ||
id: number; | ||
|
||
constructor(id: number) { | ||
this.id = id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import "reflect-metadata"; | ||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils"; | ||
import {Connection} from "../../../src/connection/Connection"; | ||
import {Post} from "./entity/Post"; | ||
|
||
describe("github issues > #5734 insert([]) should not crash", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
subscribers: [__dirname + "/subscriber/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should not crash on insert([])", () => Promise.all(connections.map(async connection => { | ||
const repository = connection.getRepository(Post); | ||
await repository.insert([]); | ||
}))); | ||
|
||
it("should still work with a nonempty array", () => Promise.all(connections.map(async connection => { | ||
const repository = connection.getRepository(Post); | ||
await repository.insert([new Post(1)]); | ||
await repository.findOneOrFail({where: {id: 1}}); | ||
}))); | ||
}); |
22 changes: 22 additions & 0 deletions
22
test/github-issues/5734/subscriber/CheckValidEntitySubscriber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {Post} from "../entity/Post"; | ||
import {EntitySubscriberInterface, EventSubscriber, InsertEvent} from "../../../../src"; | ||
|
||
/** | ||
* Subscriber which checks the validity of the entity passed to beforeInsert(). | ||
* Tests the fix for issue #5734 in which we saw an empty array leak into | ||
* beforeInsert(). | ||
*/ | ||
@EventSubscriber() | ||
export class ValidEntityCheckSubscriber | ||
implements EntitySubscriberInterface<Post> { | ||
listenTo() { | ||
return Post; | ||
} | ||
|
||
beforeInsert(event: InsertEvent<Post>) { | ||
const entity = event.entity; | ||
if (Array.isArray(entity) || !entity.id) { | ||
throw new Error(`Subscriber saw invalid entity: ${JSON.stringify(entity)}`); | ||
} | ||
} | ||
} |