Skip to content

JSON string value will be parsed twice with mysql2 #8319

@fenying

Description

@fenying

Issue Description

While using @Column('json') with mysql2 package, it will parse the JSON twice, if the value of column is a string value in JSON format like "text".

See the hardcoded parser in mysql2: https://github.com/sidorares/node-mysql2/blob/247ce10a364f0dc42c0720ca42925890d5db07f0/lib/parsers/text_parser.js#L59-L63

And where the problem is https://github.com/typeorm/typeorm/blob/master/src/driver/mysql/MysqlDriver.ts#L565-L566

However, it's okay with mysql pakcage.

Expected Behavior

Get the parsed string value

Actual Behavior

$ node dist/app.js 
undefined:1
hello
^

SyntaxError: Unexpected token h in JSON at position 0
    at JSON.parse (<anonymous>)
    at MysqlDriver.prepareHydratedValue (/home/angus/projects/fenying/typeorm-mysql2-json-bug/node_modules/typeorm/driver/mysql/MysqlDriver.js:488:54)
    at /home/angus/projects/fenying/typeorm-mysql2-json-bug/node_modules/typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js:125:56
    at Array.forEach (<anonymous>)
    at RawSqlResultsToEntityTransformer.transformColumns (/home/angus/projects/fenying/typeorm-mysql2-json-bug/node_modules/typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js:114:26)
    at RawSqlResultsToEntityTransformer.transformRawResultsGroup (/home/angus/projects/fenying/typeorm-mysql2-json-bug/node_modules/typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js:94:31)
    at /home/angus/projects/fenying/typeorm-mysql2-json-bug/node_modules/typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js:34:32
    at Map.forEach (<anonymous>)
    at RawSqlResultsToEntityTransformer.transform (/home/angus/projects/fenying/typeorm-mysql2-json-bug/node_modules/typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js:33:15)
    at SelectQueryBuilder.<anonymous> (/home/angus/projects/fenying/typeorm-mysql2-json-bug/node_modules/typeorm/query-builder/SelectQueryBuilder.js:1553:48)

Throws an exception saying JSON format errror.

Steps to Reproduce

  1. Create a table with a JSON type column in mysql.

    create table json_test(
        id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
        jvalue JSON NOT NULL
    ) ENGINE=InnoDB;
    
    INSERT INTO json_test(jvalue) VALUES('"hello"');
  2. Read the table with an entity declared by @Column('json').

    import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
    
    @Entity()
    export class JsonTest {
        @PrimaryGeneratedColumn()
        public id!: number;
    
        @Column('json')
        public jvalue!: any;
    }

My Environment

Dependency Version
Operating System
Node.js version 16.13.0
Typescript version 4.4.4
TypeORM version 0.2.38

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql yes
nativescript no
oracle no
postgres no
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions