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

fix: typeCast string() parameters for JSON #1662

Merged
merged 4 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/parsers/text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ function compile(fields, options, config) {
db: field.schema,
table: field.table,
name: field.name,
string: function() {
return _this.packet.readLengthCodedString(field.encoding);
string: function(encoding = field.encoding) {
if (field.columnType === Types.JSON && encoding === field.encoding) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you also check that encoding is not already utf8? I have a case where field.encoding is, and passing utf8 explicitly still raises the warning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My current workaround for whoever is interested is to pass utf-8 instead of utf8 to get rid of the message.

// Since for JSON columns mysql always returns charset 63 (BINARY),
// we have to handle it according to JSON specs and use "utf8",
// see https://github.com/sidorares/node-mysql2/issues/1661
console.warn(`typeCast: JSON column "${field.name}" is interpreted as BINARY by default, recommended to manually set utf8 encoding: \`field.string("utf8")\``);
}

return _this.packet.readLengthCodedString(encoding);
},
buffer: function() {
return _this.packet.readLengthCodedBuffer();
Expand Down Expand Up @@ -172,7 +179,7 @@ function compile(fields, options, config) {
} else {
parserFn(`${lvalue} = ${readCode};`);
}
}
}
}

parserFn('return result;');
Expand All @@ -191,7 +198,7 @@ function compile(fields, options, config) {
}
if (typeof options.typeCast === 'function') {
return parserFn.toFunction({wrap});
}
}
return parserFn.toFunction();
}

Expand Down
38 changes: 38 additions & 0 deletions test/unit/parsers/test-text-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const assert = require('assert');
const common = require('../../common');

const typeCastWrapper = function (...args) {
return function (field, next) {
if (field.type === 'JSON') {
return JSON.parse(field.string(...args));
}

return next();
}
}

const connection = common.createConnection();
connection.query('CREATE TEMPORARY TABLE t (i JSON)');
connection.query('INSERT INTO t values(\'{ "test": "😀" }\')');

// JSON without encoding options - should result in unexpected behaviors
connection.query({
sql: 'SELECT * FROM t',
typeCast: typeCastWrapper()
}, (err, rows) => {
assert.ifError(err);
assert.notEqual(rows[0].i.test, "😀");
});

// JSON with encoding explicitly set to utf8
connection.query({
sql: 'SELECT * FROM t',
typeCast: typeCastWrapper("utf8")
}, (err, rows) => {
assert.ifError(err);
assert.equal(rows[0].i.test, "😀");
});

connection.end();