forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 typeorm#3534
- Loading branch information
1 parent
04b4f3e
commit 7dcd3c9
Showing
4 changed files
with
72 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Column} from "../../../../src/decorator/columns/Column"; | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import { RegExpStringTransformer } from './RegExpStringTransformer'; | ||
import { PrimaryGeneratedColumn } from '../../../../src'; | ||
|
||
@Entity() | ||
export class Foo { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ type: String, transformer: RegExpStringTransformer }) | ||
bar: RegExp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export namespace RegExpStringTransformer { | ||
export function to(value: RegExp): string { | ||
return value.toString(); | ||
} | ||
|
||
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`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import "reflect-metadata"; | ||
import {expect} from "chai"; | ||
import { Connection, PromiseUtils } from "../../../src"; | ||
import { Foo } from "./entity/Foo"; | ||
import { closeTestingConnections, createTestingConnections } from '../../utils/test-utils'; | ||
|
||
describe("github issues > #3534: store regexp", () => { | ||
let connections: Connection[]; | ||
before(async () => { | ||
connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
enabledDrivers: ["postgres"], | ||
dropSchema: true, | ||
}); | ||
}); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("allows entities with regexp columns", () => PromiseUtils.runInSequence(connections, async connection => { | ||
await connection.synchronize(); | ||
const repository = connection.getRepository(Foo); | ||
|
||
const foo = new Foo(); | ||
foo.bar = /foo/i; | ||
const savedFoo = await repository.save(foo); | ||
expect(savedFoo.bar).to.instanceOf(RegExp); | ||
expect(savedFoo.bar.toString()).to.eq(/foo/i.toString()); | ||
const storedFoo = await repository.findOneOrFail(foo.id); | ||
expect(storedFoo.bar).to.instanceOf(RegExp); | ||
expect(storedFoo.bar.toString()).to.eq(/foo/i.toString()); | ||
})); | ||
|
||
}); |