Skip to content

Commit

Permalink
fix(ts-morph): fix validation of embedded polymorphic arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Jan 7, 2022
1 parent 08d8c3c commit b6a068a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/metadata/MetadataValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ export class MetadataValidator {
throw MetadataError.onlyAbstractEntitiesDiscovered();
}

const unwrap = (type: string) => type
.replace(/Array<(.*)>/, '$1') // unwrap array
.replace(/\[]$/, '') // remove array suffix
.replace(/\((.*)\)/, '$1'); // unwrap union types

// check for not discovered entities
discovered.forEach(meta => Object.values(meta.properties).forEach(prop => {
if (prop.reference !== ReferenceType.SCALAR && !prop.type.split(/ ?\| ?/).every(type => discovered.find(m => m.className === type))) {
if (prop.reference !== ReferenceType.SCALAR && !unwrap(prop.type).split(/ ?\| ?/).every(type => discovered.find(m => m.className === type))) {
throw MetadataError.fromUnknownEntity(prop.type, `${meta.className}.${prop.name}`);
}
}));
Expand Down
5 changes: 5 additions & 0 deletions packages/reflection/src/TsMorphMetadataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export class TsMorphMetadataProvider extends MetadataProvider {
const optional = property.hasQuestionToken?.() || union.includes('null') || union.includes('undefined');
type = union.filter(t => !['null', 'undefined'].includes(t)).join(' | ');

type = type
.replace(/Array<(.*)>/, '$1') // unwrap array
.replace(/\[]$/, '') // remove array suffix
.replace(/\((.*)\)/, '$1'); // unwrap union types

return { type, optional };
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ObjectHydrator } from '@mikro-orm/core';
import { Embeddable, Embedded, Entity, Enum, MikroORM, PrimaryKey, Property, wrap } from '@mikro-orm/core';
import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
import { mockLogger } from '../../helpers';

enum AnimalType {
Expand All @@ -10,7 +11,7 @@ enum AnimalType {
@Embeddable({ abstract: true, discriminatorColumn: 'type' })
abstract class Animal {

@Enum(() => AnimalType)
@Enum()
type!: AnimalType;

@Property()
Expand All @@ -21,8 +22,8 @@ abstract class Animal {
@Embeddable({ discriminatorValue: AnimalType.CAT })
class Cat extends Animal {

@Property({ nullable: true })
canMeow?: boolean = true;
@Property()
canMeow? = true;

constructor(name: string) {
super();
Expand All @@ -35,8 +36,8 @@ class Cat extends Animal {
@Embeddable({ discriminatorValue: AnimalType.DOG })
class Dog extends Animal {

@Property({ nullable: true })
canBark?: boolean = true;
@Property()
canBark? = true;

constructor(name: string) {
super();
Expand All @@ -55,13 +56,13 @@ class Owner {
@Property()
name: string;

@Embedded(() => [Cat, Dog])
@Embedded()
pet!: Cat | Dog;

@Embedded(() => [Cat, Dog], { object: true })
@Embedded({ object: true })
pet2!: Cat | Dog;

@Embedded(() => [Cat, Dog], { array: true })
@Embedded()
pets: (Cat | Dog)[] = [];

constructor(name: string) {
Expand All @@ -79,6 +80,7 @@ describe('polymorphic embeddables in sqlite', () => {
entities: [Dog, Cat, Owner],
dbName: ':memory:',
type: 'sqlite',
metadataProvider: TsMorphMetadataProvider,
});
await orm.getSchemaGenerator().createSchema();
});
Expand Down

0 comments on commit b6a068a

Please sign in to comment.