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

Polymorphic Embeddables in combination with embedded properties #2987

Open
dehrhard opened this issue Apr 5, 2022 · 0 comments
Open

Polymorphic Embeddables in combination with embedded properties #2987

dehrhard opened this issue Apr 5, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@dehrhard
Copy link

dehrhard commented Apr 5, 2022

Describe the bug
I have an issue when using Polymorphic Embeddables in combination with nested embedded properties. When two Polymorphs have a property with the same name their type is conflicting and ending mixed up. I think I traced the issue to this spot in

polymorphs.forEach(meta => {

polymorphs.forEach(meta => {
  Object.values(meta.properties).forEach(prop => properties[prop.name] = prop);
  processExtensions(meta);
});

The properties Object receives the properties from both polymorhps. If multiple polymorphs have the same property name, the latter overwrites the former.

Example
Consider the shortened example from the Docs

export enum AnimalType {
  CAT,
  DOG,
}

@Embeddable()
export class CatFood {

  @Property()
  mice = 2;
}

@Embeddable()
export class DogFood {

  @Property()
  cats = 10;
}

@Embeddable({ abstract: true, discriminatorColumn: 'type' })
export abstract class Animal {
  @Enum()
  type!: AnimalType;

  @Property()
  name!: string;
}

@Embeddable({ discriminatorValue: AnimalType.CAT })
export class Cat extends Animal {

  @Property()
  canMeow? = true;

  @Embedded()
  food: CatFood;
}

@Embeddable({ discriminatorValue: AnimalType.DOG })
export class Dog extends Animal {

  @Property()
  canBark? = true;

  @Embedded()
  food: DogFood;
}

@Entity()
export class Owner {
  @PrimaryKey()
  id!: number;

  @Property()
  name!: string;

  @Embedded()
  pet!: Cat | Dog;
}

In this case the food property of class Cat/Dog is ambiguos and depending on which polymorph is processed first, food might receive type Dogfood for Polymorph Cat or receive type Catfood for Polymorph Dog

Expected behavior
Depeding on the used polymorph, the property should receive the associated type

Versions

Dependency Version
node 16
typescript 4.6.2
mikro-orm 5.0.4
@mikro-orm/postgresql 5.0.4
@B4nan B4nan added the bug Something isn't working label Apr 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants