Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

entitySkipConstructor not working #8444

Closed
francescosalvi opened this issue Dec 10, 2021 · 0 comments · Fixed by #8445
Closed

entitySkipConstructor not working #8444

francescosalvi opened this issue Dec 10, 2021 · 0 comments · Fixed by #8445

Comments

@francescosalvi
Copy link
Contributor

francescosalvi commented Dec 10, 2021

entitySkipConstructor not working as expected/documented

See #7770, #1772

Expected Behavior

By setting entitySkipConstructor connection option, one is able to define an Entity class with strict constructor property initialization, even using object-type destructuring, e.g.:

type SomeEntityConstructorArgs = {
  readonly foo: string;
};

@Entity()
export class SomeEntity {

  @Column()
  readonly foo: string;

  constructor(args: SomeEntityConstructorArgs) {
    const { foo } = args;

    this.foo = foo;
  }
}

Actual Behavior

Running migration:run, or bootstrapping a NestJS app leveraging TypeORM, breaks with:

TypeError: Cannot destructure property 'foo' of 'args' as it is undefined.
    at null.SomeEntity (/app/src/entity/SomeEntity.ts:57:13)
    at EntityMetadata.EntityMetadata.create (/app/node_modules/typeorm/metadata/src/metadata/EntityMetadata.ts:534:23)
    at EntityMetadataValidator.EntityMetadataValidator.validate (/app/node_modules/typeorm/metadata-builder/src/metadata-builder/EntityMetadataValidator.ts:118:47)
    at null.<anonymous> (/app/node_modules/typeorm/metadata-builder/src/metadata-builder/EntityMetadataValidator.ts:46:56)

Steps to Reproduce

(see above): simply define an Entity constructor with object destructuring assignment (I actually expect a failure even with strict undefined checks of scalar arguments).

My Environment

Dependency Version
Operating System Linux
Node.js version v16.13.1
Typescript version 4.5.2
TypeORM version 0.2.41
nestjs/typeorm 8.0.2

Was reproduced with a MySQL connection, but I believe the error is unrelated to the driver in use.

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Root cause / Possible fix

Locally I applied the patch below which seems to have solved the issue. Perhaps this point in code was an oversight? I see in fact than in most if not all instances where EntityMetadata.create is invoked, { fromDeserializer: true } is passed as well.

diff --git a/node_modules/typeorm/metadata-builder/EntityMetadataValidator.js b/node_modules/typeorm/metadata-builder/EntityMetadataValidator.js
index 9bb557e..d454d4b 100644
--- a/node_modules/typeorm/metadata-builder/EntityMetadataValidator.js
+++ b/node_modules/typeorm/metadata-builder/EntityMetadataValidator.js
@@ -102,7 +102,7 @@ var EntityMetadataValidator = /** @class */ (function () {
                 throw new error_1.TypeORMError("Character set specifying is not supported in Sql Server");
         }
         // check if relations are all without initialized properties
-        var entityInstance = entityMetadata.create();
+        var entityInstance = entityMetadata.create(undefined, { fromDeserializer: true });
         entityMetadata.relations.forEach(function (relation) {
             if (relation.isManyToMany || relation.isOneToMany) {
                 // we skip relations for which persistence is disabled since initialization in them cannot harm somehow

francescosalvi pushed a commit to francescosalvi/typeorm that referenced this issue Dec 10, 2021
francescosalvi pushed a commit to francescosalvi/typeorm that referenced this issue Dec 10, 2021
francescosalvi pushed a commit to francescosalvi/typeorm that referenced this issue Dec 13, 2021
francescosalvi pushed a commit to francescosalvi/typeorm that referenced this issue Dec 14, 2021
pleerock pushed a commit that referenced this issue Feb 16, 2022
…cover with test (#8445)

Closes #8444

Co-authored-by: Francesco Salvi <francesco.salvi@bitbull.it>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant