-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
RegExp.prototype.source getter called on non-RegExp object #3534
Comments
Yeah, I think we would need some way to reproduce that :) |
Just had this throw in |
@Kononnable I found a way to reproduce the error: Try to persist an object with a RegExp in one of the columns. Note that I do use a transformer to transform a regular expression into a string. import { Entity, PrimaryGeneratedColumn } from "typeorm";
import { RegExpColumn } from "../../src/RegExpColumn";
@Entity()
export class Thing {
@PrimaryGeneratedColumn()
public id: number;
@RegExpColumn()
public foo: RegExp;
} import { Column, ColumnOptions } from "typeorm";
import { RegExpStringTransformer } from "./RegExpStringTransformer";
export function RegExpColumn(opts: ColumnOptions = {}) {
opts.type = String;
opts.transformer = RegExpStringTransformer;
return Column(opts)
} export namespace RegExpStringTransformer {
export function to(value: RegExp): string {
return value.toString(); // <<< error is thrown here, since `value` is no longer a RegExp
}
export function from(value: string): RegExp {
const match = value.match(/^\/(.*)\/(.*)$/);
if (match) {
const [, pattern, flags] = match;
return new RegExp(pattern, flags);
} else {
throw new Error(`"${value}" is not a regular expression`);
}
}
} test("entity with RegExpColumn stores regular expression", async function() {
const thing = new Thing();
thing.foo = /foo/i;
const savedThing = await thingRepo.save(thing); // <<< Error here
expect(savedThing.foo).toEqual(/foo/i);
const storedThing = await thingRepo.findOne(savedThing.id);
expect(storedThing.foo).toEqual(/foo/i);
}); Repo (exact commit): https://github.com/tobyhinloopen/bonaroo-reg-exp-column/tree/65f591bffe62cbf9bdabd84cf4fc6352a5ae6eb5 I've done hours of traversing through typeorm and SOMEWHERE inside TypeORM something replaces the native $ node
> foo = /foo/i
/foo/i
> bar = Object.create(foo);
RegExp {}
> bar.toString()
TypeError: RegExp.prototype.source getter called on non-RegExp object
at RegExp.get source [as source] (<anonymous>)
at RegExp.toString (<anonymous>)
$ node -v
v10.15.1 |
Found the origin: |
Before entity column values are transformed, changes are deeply merged using OrmUtils.mergeDeep. This mergeDeep function attempts to merge objects, but wrongly attempted to merge RegExp objects. This merging of RegExp objects breaks the object, rendering them unusable. This commit changes mergeDeep to not merge RegExp objects but overwrite them instead. Closes typeorm#3534
Before entity column values are transformed, changes are deeply merged using OrmUtils.mergeDeep. This mergeDeep function attempts to merge objects, but wrongly attempted to merge RegExp objects. This merging of RegExp objects breaks the object, rendering them unusable. This commit changes mergeDeep to not merge RegExp objects but overwrite them instead. Closes typeorm#3534
* fix: change OrmUtils.mergeDeep to not merge RegExp objects Before entity column values are transformed, changes are deeply merged using OrmUtils.mergeDeep. This mergeDeep function attempts to merge objects, but wrongly attempted to merge RegExp objects. This merging of RegExp objects breaks the object, rendering them unusable. This commit changes mergeDeep to not merge RegExp objects but overwrite them instead. Closes #3534 * fixing tests... * fixing tslint * Include import properly * Fix TSLint Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
Issue type:
[ ] question
[ X ] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[ ]
mssql
[ ]
mysql
/mariadb
[ ]
oracle
[ X ]
postgres
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
[ ]
expo
TypeORM version:
[ ]
latest
[ ]
@next
[ X ]
typeorm@0.2.9
(or put your version here)Error doesnt appear in typeorm@0.2.8 and is thrown in versions typeorm@0.2.9 through to typeorm@0.2.12
Steps to reproduce or a small repository showing the problem:
Gladly tell me if you'd need more info - tbh I would be hard pressed to produce a minimal reproduction repo, but if you have any specific questions just shoot :-)
The text was updated successfully, but these errors were encountered: