Skip to content

Commit

Permalink
fix(mariadb): do not automatically parse JSON fields (#15070)
Browse files Browse the repository at this point in the history
  • Loading branch information
WikiRik committed Oct 1, 2022
1 parent 5d7fde8 commit b4ebf2c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/dialects/mariadb/query.js
Expand Up @@ -188,7 +188,9 @@ export class MariaDbQuery 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') {
// 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]);
}

Expand Down
6 changes: 6 additions & 0 deletions test/integration/json.test.js
Expand Up @@ -200,6 +200,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

0 comments on commit b4ebf2c

Please sign in to comment.