fix: handle URL objects as column field values (#5771) · typeorm/typeorm@50a0641

3 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -1,4 +1,5 @@

11

import { ObjectLiteral } from "../common/ObjectLiteral";

2+

import { URL } from "url";

23
34

export class OrmUtils {

45

@@ -82,7 +83,8 @@ export class OrmUtils {

8283

&& !(value instanceof Set)

8384

&& !(value instanceof Date)

8485

&& !(value instanceof Buffer)

85-

&& !(value instanceof RegExp)) {

86+

&& !(value instanceof RegExp)

87+

&& !(value instanceof URL)) {

8688

if (!target[key])

8789

Object.assign(target, { [key]: Object.create(Object.getPrototypeOf(value)) });

8890

this.mergeDeep(target[key], value);

@@ -192,7 +194,8 @@ export class OrmUtils {

192194

(x instanceof Date && y instanceof Date) ||

193195

(x instanceof RegExp && y instanceof RegExp) ||

194196

(x instanceof String && y instanceof String) ||

195-

(x instanceof Number && y instanceof Number))

197+

(x instanceof Number && y instanceof Number) ||

198+

(x instanceof URL && y instanceof URL))

196199

return x.toString() === y.toString();

197200
198201

// At last checking prototypes as good as we can

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,24 @@

1+

import {Entity} from "../../../../src/decorator/entity/Entity";

2+

import {PrimaryColumn, Column} from "../../../../src";

3+

import { URL } from "url";

4+
5+

@Entity()

6+

export class User {

7+
8+

@PrimaryColumn()

9+

id: number;

10+
11+

@Column("varchar", {

12+

// marshall

13+

transformer: {

14+

from(value: string): URL {

15+

return new URL(value);

16+

},

17+

to(value: URL): string {

18+

return value.toString();

19+

},

20+

},

21+

})

22+

url: URL;

23+
24+

}

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,39 @@

1+

import "reflect-metadata";

2+

import {expect} from "chai";

3+

import {Connection} from "../../../src";

4+

import {User} from "./entity/User";

5+

import {createTestingConnections, reloadTestingDatabases, closeTestingConnections} from "../../utils/test-utils";

6+

import { URL } from "url";

7+
8+

describe("github issues > #5762 `Using URL as a rich column type breaks", () => {

9+
10+

let connections: Connection[];

11+
12+

before(async () => {

13+

connections = await createTestingConnections({

14+

entities: [User],

15+

schemaCreate: true,

16+

dropSchema: true

17+

});

18+

});

19+

beforeEach(() => reloadTestingDatabases(connections));

20+

after(() => closeTestingConnections(connections));

21+
22+

it("should allow assigning URL as a field value", () =>

23+

Promise.all(connections.map(async (connection) => {

24+

const userRepository = connection.getRepository(User);

25+
26+

const url = new URL("https://typeorm.io");

27+
28+

const user = new User();

29+

user.id = 1;

30+

user.url = url;

31+
32+

const promise = userRepository.save(user);

33+
34+

return expect(promise).to.eventually.be.deep.equal(user)

35+

.and.to.have.property("url").be.instanceOf(URL)

36+

.and.to.have.nested.property("href").equal(url.href);

37+

})));

38+
39+

});