Skip to content

fix(mariadb): Process JSON fields only if actually requested (#13027 & #12583 - #13273

Closed
das-peter wants to merge 1 commit into
sequelize:mainfrom
CandoImage:13027-fix-relations-handling-with-mariadb-json-fields
Closed

das-peter wants to merge 1 commit into
sequelize:mainfrom
CandoImage:13027-fix-relations-handling-with-mariadb-json-fields

Conversation

@das-peter

Copy link
Copy Markdown

Adding JSON fields to rows regardless if requested or not breaks AbstractQuery::_groupJoinData() for example.

Fixes #13027 & #12583

…ze#13027 & sequelize#12583)

Adding JSON fields to rows regardless if requested or not breaks AbstractQuery::_groupJoinData() for example.
@das-peter

Copy link
Copy Markdown
Author

Cross linking issue that sent me on this debugging journey ForestAdmin/forest-express-sequelize#723

@wbourne0 wbourne0 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please update this pull request to match the template and / or match contributing guidelines.

There's not much info here and I'm not sure what the issue is.

@wbourne0 wbourne0 added dialect: mariadb For issues and PRs. Things that involve MariaDB (and do not involve all dialects). docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference. labels May 26, 2021
@das-peter

Copy link
Copy Markdown
Author

@allawesome497 Thanks for the feedback. Updated the description.
Management summary:
MariaDB JSON field processing always adds JSON fields to result-set, even if fields weren't requested.
This breaks stuff that's working based on result-set structure.
Solution: Only process JSON field if it was requested / part of the result-set.

@wbourne0

Copy link
Copy Markdown
Member

@allawesome497 Thanks for the feedback. Updated the description.
Management summary:
MariaDB JSON field processing always adds JSON fields to result-set, even if fields weren't requested.
This breaks stuff that's working based on result-set structure.
Solution: Only process JSON field if it was requested / part of the result-set.

I'm still not sure what the issue is, can you give more info like:

  • When / why this would be an issue
  • An example of the query that you see this issue in
  • What your result was
  • What the result you expected is
  • An example model where this issue comes up in.

@das-peter

das-peter commented May 27, 2021 •

Copy link
Copy Markdown
Author

I sincerely am not sure if this now all about satisfying bureaucracy :|

However, here's a minimal example based on pseudo code:
Schema:

  const attributes = {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    whatever: {
      type: DataTypes.STRING,
      allowNull: false
    },
    evilJSONField: {
      type: DataTypes.JSON,
      allowNull: false,
      parse: true
    }
  };
  sequelize.define('Demos', attributes, { timestamps: true });

Query:

  await db.demo.sync({ alter: true });
  await db.demo.truncate({ logging: console.log });
  await db.demo.create({ whatever: 'foo', evilJSONField: JSON.stringify(['random', 'data']) });
  const records = await db.demo.findAll({ attributes: ['id', 'whatever'], raw: true });
  console.log(records);

This would trigger following select query: SELECT id, whatever FROM Demos AS Demos;

And it will return:

[ { id: 1, whatever: 'foo', evilJSONField: null } ]

See that evilJSONField? That's bad. Because it was not requested, it shouldn't be there.
And it's presence doesn't just violate the expectation you'd have from the generated query or the promise of raw: true to show only what the query returns, but that unexpected field will break the processing in AbstractQuery::_groupJoinData() and probably other locations.

@wbourne0 wbourne0 added type: bug DEPRECATED: replace with the "bug" issue type and removed docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference. labels May 27, 2021
wbourne0
wbourne0 previously approved these changes May 30, 2021

@wbourne0 wbourne0 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I edited your comment to use syntax highlighting. Thanks for adding code samples + the clarification.

Looks fine once linting is fixed. Can you also add a test for this to prevent regression?

if (DataTypes.JSON.parse) {
return DataTypes.JSON.parse(modelField, this.sequelize.options,
row[modelField.fieldName]);
if(row[modelField.fieldName] !== undefined) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh also: it looks like this doesn't fix #13027.

for that you should probably do something like:

Suggested change
if(row[modelField.fieldName] !== undefined) {
if(typeof row[modelField.fieldName] === 'string') {

@wbourne0
wbourne0 dismissed their stale review October 3, 2021 00:13

My comment has not been addressed - will approve once that's fixed.

@wbourne0 wbourne0 added the status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action label Oct 3, 2021
@github-actions github-actions Bot added the stale label Oct 27, 2021
@github-actions github-actions Bot closed this Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dialect: mariadb For issues and PRs. Things that involve MariaDB (and do not involve all dialects). stale status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug DEPRECATED: replace with the "bug" issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nothing returns when used attributes with JSON field in table on MariaDB dialect

2 participants