Skip to content

Commit

Permalink
fix(reflection): detect complex runtime types and don't use them as c…
Browse files Browse the repository at this point in the history
…olumn types

Closes #5601
  • Loading branch information
B4nan committed May 31, 2024
1 parent c4ed4d5 commit 0c8a587
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/core/src/metadata/MetadataDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,14 @@ export class MetadataDiscovery {
const mappedType = this.getMappedType(prop);
const SCALAR_TYPES = ['string', 'number', 'boolean', 'bigint', 'Date', 'Buffer', 'RegExp', 'any', 'unknown'];

if (mappedType instanceof UnknownType && !prop.columnTypes && !SCALAR_TYPES.includes(prop.type)) {
if (
mappedType instanceof UnknownType
&& !prop.columnTypes
// it could be a runtime type from reflect-metadata
&& !SCALAR_TYPES.includes(prop.type)
// or it might be inferred via ts-morph to some generic type alias
&& !prop.type.match(/[<>:"';{}]/)
) {
prop.columnTypes = [prop.type];
} else {
prop.columnTypes = [mappedType.getColumnType(prop, this.platform)];
Expand Down
30 changes: 28 additions & 2 deletions tests/features/reflection/GH3720.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MikroORM } from '@mikro-orm/postgresql';
import { TsMorphMetadataProvider } from '@mikro-orm/reflection';

@Entity()
export class A {
class A {

@PrimaryKey()
id!: number;
Expand All @@ -13,6 +13,32 @@ export class A {

}

type ValueOf<T> = T[keyof T];

const UserType = Object.freeze({
ADMIN: 'admin',
CUSTOMER: 'customer',
} as const);

type Props = {
type: ValueOf<typeof UserType>;
};

@Entity()
class User {

constructor(props: Props) {
this.type = props.type;
}

@PrimaryKey()
id!: number;

@Property()
type: ValueOf<typeof UserType>;

}

let orm: MikroORM;

beforeAll(async () => {
Expand All @@ -21,7 +47,7 @@ beforeAll(async () => {
metadataCache: { enabled: false },
logger,
debug: true,
entities: [A],
entities: [A, User],
dbName: 'mikro_orm_test_3720',
metadataProvider: TsMorphMetadataProvider,
discovery: { tsConfigPath: 'foobar.json' },
Expand Down

0 comments on commit 0c8a587

Please sign in to comment.