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 by checking meta #15704

Merged
merged 4 commits into from
Mar 27, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/core/src/dialects/mariadb/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,19 @@ export class MariaDbQuery extends AbstractQuery {
return;
}

for (const _field of Object.keys(this.model.fieldRawAttributesMap)) {
const meta = rows.meta;
for (const [i, _field] of Object.keys(this.model.fieldRawAttributesMap).entries()) {
const modelField = this.model.fieldRawAttributesMap[_field];
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)) {
// JSON fields for MariaDB server 10.5.2+ already results in JSON format so we can skip JSON.parse
// In this case the column type field will be MYSQL_TYPE_STRING, but the extended type will indicate 'json'
if (
row[modelField.fieldName]
&& typeof row[modelField.fieldName] === 'string'
&& (!meta[i] || meta[i].dataTypeFormat !== 'json')
) {
row[modelField.fieldName] = JSON.parse(row[modelField.fieldName]);
}

Expand Down
26 changes: 26 additions & 0 deletions packages/core/test/integration/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ describe('JSON Querying', () => {
});
}

it('should be able to retrieve json value as object for json fields created in every mariadb release', async () => {
// MariaDB does not support native JSON type, it uses longtext instead
// MariaDB >=10.5.2 adds a CHECK(json_valid(field)) validator that uses to return a different dataFormat to clients
// mariadb connector use this to decide to parse or not a JSON field before sequelize
if (dialectName !== 'mariadb') {
return;
}

await sequelize.query(`CREATE TABLE Posts (id INTEGER AUTO_INCREMENT PRIMARY KEY,
metaOldJSONtype longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
metaNewJSONtype longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK(json_valid(metaNewJSONtype)))`);

const Posts = sequelize.define('Posts', {
metaOldJSONtype: DataTypes.JSON,
metaNewJSONtype: DataTypes.JSON,
}, {
freezeTableName: true,
timestamps: false,
});

await Posts.create({ metaOldJSONtype: 'some text', metaNewJSONtype: 'some text' });

const posts = await Posts.findAll({ raw: true });
expect(posts[0].metaOldJSONtype).to.equal(posts[0].metaNewJSONtype);
});

if (dialect.supports.jsonOperations) {
it('should be able to retrieve element of array by index', async () => {
const user = await vars.User.findOne({
Expand Down