Skip to content
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

Incorrect data loading from JSON string for column type 'simple-json'. Why? #5501

Closed
MWarCZ opened this issue Feb 10, 2020 · 3 comments · Fixed by #6574
Closed

Incorrect data loading from JSON string for column type 'simple-json'. Why? #5501

MWarCZ opened this issue Feb 10, 2020 · 3 comments · Fixed by #6574

Comments

@MWarCZ
Copy link

MWarCZ commented Feb 10, 2020

Issue type:

[x] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Why are some correct JSON values returned as Object when typeorm loading JSON string (simple-json)?

JSON.parse('{"foo": "bar"}') // { foo: 'bar' }
JSON.parse('[ 1, 2, 3]') // [1, 2, 3]
JSON.parse('true') // true => But typeorm return: { }
JSON.parse('123') // 123 => But typeorm return: { }
JSON.parse('"message"') // 'message' => But typeorm return: { }

In my project, I noticed this strange behavior.

  • Data inside entity is correct before saving.
  • Inside database, data is also correct.
  • But loaded entity has incorrect data.

Example - Part of my problem:

@Entity()
export class Foo {
  @Column('simple-json')
  returnValue: Json = false
} 

// ...

let foo = await connection.manager.save(new Foo())
// foo => Foo { returnValue: false }
// Inside DB => select 'foo'.'returnValue' from 'foo' => returnValue: "false"
let findFoo = await connection.getRepository(Foo).findOneOrFail()
// findFoo => Foo { returnValue: {} }

I looked into typeorm code and I found explicit test on type Object before returning parsed values.

Why is this behavior chosen?

@zhangyy91
Copy link

I met the same issue. According to the standards like RFC 7159, ECMA-404, ECMA-262, any JSON value is valid JSON text, including simple strings and numbers. But current behavior is against the standards and the official document,

simple-json which can store any values which can be stored in database via JSON.stringify

Hope this issue can be fixed quickly.

@wodka
Copy link
Contributor

wodka commented May 26, 2020

The error problem was introduced in pull request #4476

@zhangyy91
Copy link

@wodka Thank you for the information. I just got the pull request and found the cause. I think this issue can be fixed without much effort. According to the standards and documents, when retrieving simple-json columns, we can just use JSON.parse() to get the JSON value, if it fails, null should be returned.

static stringToSimpleJson(value: any) {
  try {
    return JSON.parse(value);
  } catch (err) {
    return null;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants