sidorares / sidorares/node-mysql2

re escaping special chars in json values

Open
#2,575 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

Hi, i've found a weird behaviour,
when querying a table and converting it to a JSON_TABLE, if a textual row inside of it contain a \n, it will be re escaped (so it will be given as "\\n"), something that it don't do when querying non JSON_TABLE textual values

an example:

rawQuery(
SELECT * FROM JSON_TABLE(
(
SELECT content as json
FROM logs_related_files
WHERE log_id = ?
LIMIT 1
),
'$[*]' COLUMNS (
name VARCHAR(255) PATH '$.name',
content LONGTEXT PATH '$.content',
contentType VARCHAR(255) PATH '$.contentType'
)
) as files
WHERE name = ?
LIMIT 1 , [id, name])
.then((res) => {
if (res[0].length == 0)
return response.status(404).send("Not found");
return res[0][0];
})
.catch((e) => {
console.log(e.message);
return response.status(400).send(e.message);
});

in this case, even if in "content" the value is "this is a\ntest" (notice the \n)
and i can confirm it by running the exact same sql query using a mysql cli client

the driver will give me a content containing "this is a \\ntest" (notice the \\n)

this issue doesn't appear if i query a non JSON_TABLE value (i get a correct "this is a \ntest" using the driver)

so for now i'm running a replace on the result to reset the correct antislash value but i would prefer not to ducktape it :')

thanks for your hardwork!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the JSON_TABLE query from the issue through the node-mysql2 driver and compare its returned content with the MySQL CLI and a non-JSON_TABLE query. Trace the result decoding path used for this query; done means backslashes in JSON_TABLE text values match the server and CLI output without a caller-side replacement.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, nodejs, typescript
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.