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

APIv4 - Fix CONTAINS operator to work with more types of serialized fields #26362

Merged
merged 1 commit into from
May 28, 2023

Conversation

colemanw
Copy link
Member

Overview

Fixes a bug in APIv4 where the CONTAINS operator would return false-positives for certain types of serialized fields.

Before

Given a group with parents [12, 35, 40] the CONTAINS match would return TRUE for 12 (correctly) but also return TRUE for 1 (incorrectly matching one of the digits instead of the complete number)

After

Fixed.

@civibot
Copy link

civibot bot commented May 27, 2023

(Standard links)

@civibot civibot bot added the master label May 27, 2023
switch ($field['serialize'] ?? NULL) {

case \CRM_Core_DAO::SERIALIZE_JSON:
$operator = 'LIKE';
$value = '%"' . $value . '"%';
Copy link
Contributor

Choose a reason for hiding this comment

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

MIN_INSTALL_MYSQL_VER is now 5.7, so maybe it makes sense to fix this while we're here? Would be good to improve the JSON search, since the current version has some obvious shortcomings.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I looked at that as well, and tried a few ways of searching JSON but couldn't get it working just right so I thought I'd go ahead and put up this PR without that so at least this can get merged.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough

@@ -592,8 +592,13 @@ protected function createSQLClause($fieldAlias, $operator, $value, $field, int $
}
return $sql ? implode(' AND ', $sql) : NULL;
}

// The CONTAINS operator matches a substring for strings. For arrays & serialized fields,
// it only matches a complete (not partial) string within the array.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe note that we don't match complete strings for PHP serialized?

Copy link
Member Author

@colemanw colemanw May 27, 2023

Choose a reason for hiding this comment

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

I'll be there's a way to support that type of serialization too! Probably something like:

        case \CRM_Core_DAO::SERIALIZE_PHP:
          $operator = 'LIKE';
          $value = '%' . serialize($value) . '%';
          break;

But I haven't tested that. Best to merge this PR first.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, I assumed it was a don't bother to support it because there aren't many of them situation.

@colemanw
Copy link
Member Author

Thanks for reviewing @larssandergreen - are you satisfied with this PR getting merged as-is before iterating further on it?

@larssandergreen
Copy link
Contributor

Code looks good, I will give it an r-run in a bit.

@larssandergreen
Copy link
Contributor

No issues on r-run, good to merge from my perspective.

@colemanw colemanw merged commit c1ff89f into civicrm:master May 28, 2023
@colemanw colemanw deleted the api4Contains branch May 28, 2023 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants