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(mariadb): do not automatically parse JSON fields #14800

Merged
merged 7 commits into from Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dialects/mariadb/query.js
Expand Up @@ -129,6 +129,7 @@ class Query extends AbstractQuery {

if (this.isSelectQuery()) {
this.handleJsonSelectQuery(data);

return this.handleSelectQuery(data);
}
if (this.isInsertQuery() || this.isUpdateQuery()) {
Expand Down Expand Up @@ -184,7 +185,7 @@ class Query extends AbstractQuery {
if (modelField.type instanceof DataTypes.JSON) {
// Value is returned as String, not JSON
rows = rows.map(row => {
if (row[modelField.fieldName] && typeof row[modelField.fieldName] === 'string') {
if (row[modelField.fieldName] && typeof row[modelField.fieldName] === 'string' && !this.connection.info.hasMinVersion(10, 5, 2)) {
lesion marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is not correct:

encrypt94@7e6c9f7

Copy link
Member

Choose a reason for hiding this comment

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

Do I understand correctly that the commit by @encrypt94 is more correct than the current code? If so; feel free to open a PR for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've investigated, that's true, that commit is more correct.
The type of field returned by mariadb does not depend on the version of mariadb, but on the version of mariadb WHEN the table was created!

https://mariadb.com/kb/en/result-set-packets/#field-types
For a JSON column, the column type field will be MYSQL_TYPE_STRING, but the extended type will indicate 'json'.

the connector use this field to decide to parse or not the response:
https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/59da962ce4ae6338bb14ed8854a70526b429911f/lib/cmd/decoder/text-decoder.js#L98

during a select the connector put fields details inside a "meta" key that could be used to check if returned value from the connector is already parsed or not:
https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/f25081715c9e1f24a10017e70787108d0d2ced01/documentation/promise-api.md#array-result-sets

a gist to test things:
https://gist.github.com/lesion/af0c7f1370ab6eb05cafdabd5b39ac33

row[modelField.fieldName] = JSON.parse(row[modelField.fieldName]);
}
if (DataTypes.JSON.parse) {
Expand Down
6 changes: 6 additions & 0 deletions test/integration/json.test.js
Expand Up @@ -196,6 +196,12 @@ describe('model', () => {
expect(user.username).to.equal('anna');
});

it('should be able to store strings', async function() {
await this.User.create({ username: 'swen', emergency_contact: 'joe' });
const user = await this.User.findOne({ where: { username: 'swen' } });
expect(user.emergency_contact).to.equal('joe');
});

it('should be able to store values that require JSON escaping', async function() {
const text = 'Multi-line \'$string\' needing "escaping" for $$ and $1 type values';

Expand Down