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

Turning validate off on EntityManager Population should not trigger invalidPropertyName Error #5043

Closed
5 tasks done
ThatOneAwkwardGuy opened this issue Dec 22, 2023 · 5 comments

Comments

@ThatOneAwkwardGuy
Copy link

ThatOneAwkwardGuy commented Dec 22, 2023

Describe the bug

I have a polymorphic schema, which may or may not have a property i'd like to be populated. I've turned validation off when using this.em.populate however I'm still receiving invalidPropertyName errors.

Reproduction

@Entity({
  discriminatorColumn: 'type',
  abstract: true,
  discriminatorMap: {
	'child1': 'Child1',
	'child2': 'Child2'
  },
})
export abstract class Base  {
  @Property({ nullable: true })
  type? string;
}

export class Child1 extends Base {
  @Property()
  type: "child1";
}

export class Child2 extends Base {
  @Property()
  type: "child2";

  @ManyToOne(() => BankingAccount, { ref: true })
  account: Ref<BankingAccount>;	
}

// This will throw a invalidPropertyName error even though the entity could have account as a property. 
// I think it's fine that it does this and will prevent others making mistakes, but I need a way to turn it off.

await this.em.populate(entity, ["account"], {validate: false})

What driver are you using?

@mikro-orm/mongodb

MikroORM version

6.0.0-dev.284

Node.js version

20

Operating system

Mac

Validations

@B4nan
Copy link
Member

B4nan commented Dec 22, 2023

await this.em.populate(entity, ["account"], {validate: false})

What is entity here? What error do you get? Include the stack trace so it's clear where it's thrown from.

My guess is its coming from the EM, not entity loader. FWIW this options is rather internal.

@ThatOneAwkwardGuy
Copy link
Author

Actual stack trace

[2] [0] [Nest] 71051  - 22/12/2023, 18:11:41   ERROR [ExceptionsHandler] Entity 'BusinessAssetManualOther' does not have property 'ticker'
[2] [0] ValidationError: Entity 'BusinessAssetManualOther' does not have property 'ticker'
[2] [0]     at Function.invalidPropertyName (test/node_modules/.pnpm/@mikro-orm+core@6.0.0-dev.284_patch_hash=pip6vhquwj3ln44si6axk5oway/node_modules/@mikro-orm/core/errors.js:58:16)
[2] [0]     at MongoEntityManager.preparePopulate (test/node_modules/.pnpm/@mikro-orm+core@6.0.0-dev.284_patch_hash=pip6vhquwj3ln44si6axk5oway/node_modules/@mikro-orm/core/EntityManager.js:1517:44)
[2] [0]     at MongoEntityManager.populate (test/node_modules/.pnpm/@mikro-orm+core@6.0.0-dev.284_patch_hash=pip6vhquwj3ln44si6axk5oway/node_modules/@mikro-orm/core/EntityManager.js:1303:37)
[2] [0]     at test/dist/apps/api/main.js:48434:81
[2] [0]     at Array.map (<anonymous>)
[2] [0]     at AssetService.getPopulatedAssets (test/dist/apps/api/main.js:48434:58)
[2] [0]     at processTicksAndRejections (node:internal/process/task_queues:95:5)
[2] [0]     at AssetController.getAssets (test/dist/apps/api/main.js:48238:24)
[2] [0]

modified to suite example:

[2] [0] [Nest] 71051  - 22/12/2023, 18:11:41   ERROR [ExceptionsHandler] Entity 'Child1' does not have property 'ticker'
[2] [0] ValidationError: Entity 'Child1' does not have property 'account'
[2] [0]     at Function.invalidPropertyName (test/node_modules/.pnpm/@mikro-orm+core@6.0.0-dev.284_patch_hash=pip6vhquwj3ln44si6axk5oway/node_modules/@mikro-orm/core/errors.js:58:16)
[2] [0]     at MongoEntityManager.preparePopulate (test/node_modules/.pnpm/@mikro-orm+core@6.0.0-dev.284_patch_hash=pip6vhquwj3ln44si6axk5oway/node_modules/@mikro-orm/core/EntityManager.js:1517:44)
[2] [0]     at MongoEntityManager.populate (test/node_modules/.pnpm/@mikro-orm+core@6.0.0-dev.284_patch_hash=pip6vhquwj3ln44si6axk5oway/node_modules/@mikro-orm/core/EntityManager.js:1303:37)
[2] [0]     at test/dist/apps/api/main.js:48434:81
[2] [0]     at Array.map (<anonymous>)
[2] [0]     at AssetService.getPopulatedAssets (test/dist/apps/api/main.js:48434:58)
[2] [0]     at processTicksAndRejections (node:internal/process/task_queues:95:5)
[2] [0]     at AssetController.getAssets (test/dist/apps/api/main.js:48238:24)
[2] [0]

@ThatOneAwkwardGuy
Copy link
Author

Sorry, i also realised i forgot to answer what entity would be. entity could be either Child1 or Child2

@B4nan
Copy link
Member

B4nan commented Dec 22, 2023

Got it, I think we could allow that in general even without the flag, it's just about checking the root entity metadata in there.

@ThatOneAwkwardGuy
Copy link
Author

Got it, I think we could allow that in general even without the flag, it's just about checking the root entity metadata in there.

As in expanding the names it checks for by starting from the Base entity getting all possible property names?

@B4nan B4nan closed this as completed in 2b7c5ba Dec 22, 2023
B4nan added a commit that referenced this issue Dec 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants