Skip to content

Commit

Permalink
fix: make EntityMetadataValidator comply with entitySkipConstructor, …
Browse files Browse the repository at this point in the history
…cover with test (#8445)

Closes #8444

Co-authored-by: Francesco Salvi <francesco.salvi@bitbull.it>
  • Loading branch information
francescosalvi and Francesco Salvi committed Feb 16, 2022
1 parent 09f54e0 commit 3d6c5da
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/metadata-builder/EntityMetadataValidator.ts
Expand Up @@ -116,7 +116,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) {

Expand Down
18 changes: 18 additions & 0 deletions 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;
}
}
45 changes: 45 additions & 0 deletions 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<Connection[]> {
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,
});
});
});
});

0 comments on commit 3d6c5da

Please sign in to comment.