From 5a959eaf6d0771c66bbdbcd311c7185536c455a0 Mon Sep 17 00:00:00 2001 From: Francesco Salvi Date: Fri, 10 Dec 2021 09:14:05 +0100 Subject: [PATCH] fix: make EntityMetadataValidator comply with entitySkipConstructor, cover with test Closes #8444 --- .../EntityMetadataValidator.ts | 2 +- .../8444/entity/StrictlyInitializedEntity.ts | 18 ++++++++ test/github-issues/8444/issue-8444.ts | 45 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/github-issues/8444/entity/StrictlyInitializedEntity.ts create mode 100644 test/github-issues/8444/issue-8444.ts diff --git a/src/metadata-builder/EntityMetadataValidator.ts b/src/metadata-builder/EntityMetadataValidator.ts index d1d376662f..46efab7ae1 100644 --- a/src/metadata-builder/EntityMetadataValidator.ts +++ b/src/metadata-builder/EntityMetadataValidator.ts @@ -115,7 +115,7 @@ export class EntityMetadataValidator { } // check if relations are all without initialized properties - const entityInstance = entityMetadata.create(); + const entityInstance = entityMetadata.create(undefined, { fromDeserializer: true }); entityMetadata.relations.forEach(relation => { if (relation.isManyToMany || relation.isOneToMany) { diff --git a/test/github-issues/8444/entity/StrictlyInitializedEntity.ts b/test/github-issues/8444/entity/StrictlyInitializedEntity.ts new file mode 100644 index 0000000000..e5d785279b --- /dev/null +++ b/test/github-issues/8444/entity/StrictlyInitializedEntity.ts @@ -0,0 +1,18 @@ +import {Column, Entity, PrimaryGeneratedColumn} from "../../../../src"; + +@Entity() +export class StrictlyInitializedEntity { + @PrimaryGeneratedColumn() + id?: number; + + @Column() + readonly someColumn: string; + + constructor(someColumn: string) { + if (someColumn === undefined) { + throw new Error("someColumn cannot be undefined."); + } + + this.someColumn = someColumn; + } +} diff --git a/test/github-issues/8444/issue-8444.ts b/test/github-issues/8444/issue-8444.ts new file mode 100644 index 0000000000..976ac798a0 --- /dev/null +++ b/test/github-issues/8444/issue-8444.ts @@ -0,0 +1,45 @@ +import "reflect-metadata"; +import { expect } from "chai"; +import {closeTestingConnections, createTestingConnections} from "../../utils/test-utils"; +import {StrictlyInitializedEntity} from "./entity/StrictlyInitializedEntity"; +import { Connection } from "../../../src/connection/Connection"; + +describe("github issues > #8444 entitySkipConstructor not working", () => { + + describe("without entitySkipConstructor", () => { + + it("createTestingConnections should fail with 'someColumn cannot be undefined.'", async () => { + async function bootstrapWithoutEntitySkipConstructor(): Promise { + return await createTestingConnections({ + driverSpecific: { + entitySkipConstructor: false, + }, + entities: [ StrictlyInitializedEntity ], + schemaCreate: true, + dropSchema: true, + }); + } + + await expect(bootstrapWithoutEntitySkipConstructor()) + .to.be.rejectedWith("someColumn cannot be undefined"); + }); + }); + + + describe("with entitySkipConstructor", () => { + + let connections: Connection[] = []; + afterEach(() => closeTestingConnections(connections)); + + it("createTestingConnections should succeed", async () => { + connections = await createTestingConnections({ + driverSpecific: { + entitySkipConstructor: true, + }, + entities: [ StrictlyInitializedEntity ], + schemaCreate: true, + dropSchema: true, + }); + }); + }); +});