From bfb2f08b9e5909321fa44cf86cb6de9d8d6e115d Mon Sep 17 00:00:00 2001 From: lesion Date: Mon, 25 Jul 2022 17:53:34 +0200 Subject: [PATCH 1/5] fix(mariadb): do not automatically parse JSON fields --- src/dialects/mariadb/query.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/dialects/mariadb/query.js b/src/dialects/mariadb/query.js index 12d8f9c9d5b3..fd48d3f2a341 100644 --- a/src/dialects/mariadb/query.js +++ b/src/dialects/mariadb/query.js @@ -128,7 +128,10 @@ class Query extends AbstractQuery { } if (this.isSelectQuery()) { - this.handleJsonSelectQuery(data); + if (this.sequelize.config.dialectOptions && !this.sequelize.config.dialectOptions.autoJsonMap) { + this.handleJsonSelectQuery(data); + } + return this.handleSelectQuery(data); } if (this.isInsertQuery() || this.isUpdateQuery()) { From 83f87d843db9243f678403cb33f6b3b8755eb7f0 Mon Sep 17 00:00:00 2001 From: lesion Date: Mon, 25 Jul 2022 18:25:40 +0200 Subject: [PATCH 2/5] fix(mariadb): do not automatically parse JSON fields --- src/dialects/mariadb/query.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/dialects/mariadb/query.js b/src/dialects/mariadb/query.js index 12d8f9c9d5b3..42a88bb73b5d 100644 --- a/src/dialects/mariadb/query.js +++ b/src/dialects/mariadb/query.js @@ -128,7 +128,10 @@ class Query extends AbstractQuery { } if (this.isSelectQuery()) { - this.handleJsonSelectQuery(data); + if (this.sequelize.config.dialectOptions && this.sequelize.config.dialectOptions.autoJsonMap === false) { + this.handleJsonSelectQuery(data); + } + return this.handleSelectQuery(data); } if (this.isInsertQuery() || this.isUpdateQuery()) { From 424535447d8af679ae8c1d91d0c5c89542428501 Mon Sep 17 00:00:00 2001 From: lesion Date: Mon, 25 Jul 2022 18:49:45 +0200 Subject: [PATCH 3/5] fix(mariadb): passing tests --- src/dialects/mariadb/query.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/dialects/mariadb/query.js b/src/dialects/mariadb/query.js index 42a88bb73b5d..4f4bedb467df 100644 --- a/src/dialects/mariadb/query.js +++ b/src/dialects/mariadb/query.js @@ -128,9 +128,7 @@ class Query extends AbstractQuery { } if (this.isSelectQuery()) { - if (this.sequelize.config.dialectOptions && this.sequelize.config.dialectOptions.autoJsonMap === false) { - this.handleJsonSelectQuery(data); - } + this.handleJsonSelectQuery(data); return this.handleSelectQuery(data); } @@ -187,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.sequelize.config.dialectOptions && this.sequelize.config.dialectOptions.autoJsonMap === false) { row[modelField.fieldName] = JSON.parse(row[modelField.fieldName]); } if (DataTypes.JSON.parse) { From 307fe9a692f4059fde85859198b6e51a97e64ae2 Mon Sep 17 00:00:00 2001 From: lesion Date: Wed, 27 Jul 2022 15:04:33 +0200 Subject: [PATCH 4/5] fix(mariadb): json fields should be able to store strings too --- src/dialects/mariadb/query.js | 2 +- test/integration/json.test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/dialects/mariadb/query.js b/src/dialects/mariadb/query.js index 4f4bedb467df..552eb6d6afa3 100644 --- a/src/dialects/mariadb/query.js +++ b/src/dialects/mariadb/query.js @@ -185,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' && this.sequelize.config.dialectOptions && this.sequelize.config.dialectOptions.autoJsonMap === false) { + if (row[modelField.fieldName] && typeof row[modelField.fieldName] === 'string' && !this.connection.info.hasMinVersion(10, 5, 2)) { row[modelField.fieldName] = JSON.parse(row[modelField.fieldName]); } if (DataTypes.JSON.parse) { diff --git a/test/integration/json.test.js b/test/integration/json.test.js index 37a511e5d4db..80dbb3b8bfe0 100644 --- a/test/integration/json.test.js +++ b/test/integration/json.test.js @@ -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'; From c7ed0495471358fce8a0e20785117ccf0e25b6f3 Mon Sep 17 00:00:00 2001 From: lesion Date: Mon, 5 Sep 2022 17:00:12 +0200 Subject: [PATCH 5/5] fix(mariadb): comment that MariaDB 10.5.2 already results in JSON format --- src/dialects/mariadb/query.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/dialects/mariadb/query.js b/src/dialects/mariadb/query.js index 552eb6d6afa3..2f05d444f563 100644 --- a/src/dialects/mariadb/query.js +++ b/src/dialects/mariadb/query.js @@ -185,6 +185,8 @@ class Query extends AbstractQuery { if (modelField.type instanceof DataTypes.JSON) { // Value is returned as String, not JSON rows = rows.map(row => { + // JSON fields for MariaDB server 10.5.2+ already results in JSON format, skip JSON.parse + // this is due to this https://jira.mariadb.org/browse/MDEV-17832 and how mysql2 connector interacts with MariaDB and JSON fields if (row[modelField.fieldName] && typeof row[modelField.fieldName] === 'string' && !this.connection.info.hasMinVersion(10, 5, 2)) { row[modelField.fieldName] = JSON.parse(row[modelField.fieldName]); }