From d1594f5d41a35bac7bb99d4f00e76c2fb670ee17 Mon Sep 17 00:00:00 2001 From: Abhijeet Chakraborty <16278759+abhijeet1403@users.noreply.github.com> Date: Wed, 14 Aug 2019 14:01:48 +0530 Subject: [PATCH] fix: resolve issue with conversion string to simple-json (#4476) Closes: #4440 --- src/util/DateUtils.ts | 7 ++++- test/github-issues/4440/entity/Post.ts | 16 ++++++++++ test/github-issues/4440/issue-4440.ts | 43 ++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/github-issues/4440/entity/Post.ts create mode 100644 test/github-issues/4440/issue-4440.ts diff --git a/src/util/DateUtils.ts b/src/util/DateUtils.ts index 1f6b210ebb..9988c84c4f 100644 --- a/src/util/DateUtils.ts +++ b/src/util/DateUtils.ts @@ -170,7 +170,12 @@ export class DateUtils { } static stringToSimpleJson(value: any) { - return typeof value === "string" ? JSON.parse(value) : value; + try { + const simpleJSON = JSON.parse(value); + return (typeof simpleJSON === "object") ? simpleJSON : {}; + } catch (err) { + return {}; + } } static simpleEnumToString(value: any) { diff --git a/test/github-issues/4440/entity/Post.ts b/test/github-issues/4440/entity/Post.ts new file mode 100644 index 0000000000..5220e523cd --- /dev/null +++ b/test/github-issues/4440/entity/Post.ts @@ -0,0 +1,16 @@ +import { Column } from "../../../../src/decorator/columns/Column"; +import { PrimaryColumn } from "../../../../src/decorator/columns/PrimaryColumn"; +import { Entity } from "../../../../src/decorator/entity/Entity"; + +@Entity() +export class Post { + @PrimaryColumn() + id: number; + + @Column({ + type: "simple-json", + nullable: true + }) + jsonField: any; + +} diff --git a/test/github-issues/4440/issue-4440.ts b/test/github-issues/4440/issue-4440.ts new file mode 100644 index 0000000000..e42ca2eb30 --- /dev/null +++ b/test/github-issues/4440/issue-4440.ts @@ -0,0 +1,43 @@ +import "reflect-metadata"; +import { Connection } from "../../../src/connection/Connection"; +import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; +import { Post } from "./entity/Post"; + +describe("github issues > #4440 simple-json column type throws error for string with no value", () => { + + let connections: Connection[]; + before(async () => { + connections = await createTestingConnections({ + entities: [Post], + schemaCreate: true, + dropSchema: true + }); + }); + beforeEach(() => reloadTestingDatabases(connections)); + after(() => closeTestingConnections(connections)); + + it("should correctly add retrieve simple-json field with no value", () => + Promise.all(connections.map(async (connection) => { + const repo = connection.getRepository(Post); + const post = new Post(); + post.id = 1; + post.jsonField = ""; + await repo.save(post); + const postFound = await repo.findOne(1); + postFound!.id.should.eql(1); + postFound!.jsonField.should.eql({}); + }))); + + it("should correctly add retrieve simple-json field with some value", () => + Promise.all(connections.map(async (connection) => { + const repo = connection.getRepository(Post); + const post = new Post(); + post.id = 1; + post.jsonField = {"key": "value"}; + await repo.save(post); + const postFound = await repo.findOne(1); + postFound!.id.should.eql(1); + postFound!.jsonField.should.eql({"key": "value"}); + }))); + +});