Skip to content
This repository was archived by the owner on Mar 18, 2022. It is now read-only.

Commit d1594f5

Browse files
abhijeet1403vlapo
authored andcommitted
fix: resolve issue with conversion string to simple-json (typeorm#4476)
Closes: typeorm#4440
1 parent c321562 commit d1594f5

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/util/DateUtils.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ export class DateUtils {
170170
}
171171

172172
static stringToSimpleJson(value: any) {
173-
return typeof value === "string" ? JSON.parse(value) : value;
173+
try {
174+
const simpleJSON = JSON.parse(value);
175+
return (typeof simpleJSON === "object") ? simpleJSON : {};
176+
} catch (err) {
177+
return {};
178+
}
174179
}
175180

176181
static simpleEnumToString(value: any) {
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Column } from "../../../../src/decorator/columns/Column";
2+
import { PrimaryColumn } from "../../../../src/decorator/columns/PrimaryColumn";
3+
import { Entity } from "../../../../src/decorator/entity/Entity";
4+
5+
@Entity()
6+
export class Post {
7+
@PrimaryColumn()
8+
id: number;
9+
10+
@Column({
11+
type: "simple-json",
12+
nullable: true
13+
})
14+
jsonField: any;
15+
16+
}

test/github-issues/4440/issue-4440.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import "reflect-metadata";
2+
import { Connection } from "../../../src/connection/Connection";
3+
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
4+
import { Post } from "./entity/Post";
5+
6+
describe("github issues > #4440 simple-json column type throws error for string with no value", () => {
7+
8+
let connections: Connection[];
9+
before(async () => {
10+
connections = await createTestingConnections({
11+
entities: [Post],
12+
schemaCreate: true,
13+
dropSchema: true
14+
});
15+
});
16+
beforeEach(() => reloadTestingDatabases(connections));
17+
after(() => closeTestingConnections(connections));
18+
19+
it("should correctly add retrieve simple-json field with no value", () =>
20+
Promise.all(connections.map(async (connection) => {
21+
const repo = connection.getRepository(Post);
22+
const post = new Post();
23+
post.id = 1;
24+
post.jsonField = "";
25+
await repo.save(post);
26+
const postFound = await repo.findOne(1);
27+
postFound!.id.should.eql(1);
28+
postFound!.jsonField.should.eql({});
29+
})));
30+
31+
it("should correctly add retrieve simple-json field with some value", () =>
32+
Promise.all(connections.map(async (connection) => {
33+
const repo = connection.getRepository(Post);
34+
const post = new Post();
35+
post.id = 1;
36+
post.jsonField = {"key": "value"};
37+
await repo.save(post);
38+
const postFound = await repo.findOne(1);
39+
postFound!.id.should.eql(1);
40+
postFound!.jsonField.should.eql({"key": "value"});
41+
})));
42+
43+
});

0 commit comments

Comments
 (0)