V8 API: 2nd OR on same field gives only results for 2nd filter #4842

Open
opened 2026-02-20 16:24:53 -05:00 by deekerman · 6 comments
Owner

Originally created by @ebogaard on GitHub (Aug 30, 2022).

Issue

Expected Behavior

When using an OR more than once on the same field, you expect to get results that satisfy both filters.
For example: /V8/module/Accounts?filter[name][eq]=A&filter[operator]=or&filter[name][eq]=B

Should give all Accounts with name A or B.

Actual Behavior

When using this filter, we only get Accounts that satisfy the second filter. The first filter seems to be ignored.

When using an OR on two separate fields, the OR operator works as expected and gives results that satisfy both fitlers.

Possible Fix

n/a

Steps to Reproduce

  1. Make sure there is an account called "A" and one called "B"
  2. Try API V8 filter twice on same field: /V8/module/Accounts?filter[name][eq]=A&filter[operator]=or&filter[name][eq]=B
  3. See that you only get Account "B", while account "A" is expected in the results as well.

Context

Your Environment

  • SuiteCRM Version used: 7.12.6
  • Browser name and version (e.g. Chrome Version 51.0.2704.63 (64-bit)):
  • Environment name and version (e.g. MySQL, PHP 7): PHP 7.4, MariaDB 10.4
  • Operating System and version (e.g Ubuntu 16.04): EL 8
Originally created by @ebogaard on GitHub (Aug 30, 2022). <!--- Provide a general summary of the issue in the **Title** above --> <!--- Before you open an issue, please check if a similar issue already exists or has been closed before. ---> <!--- If you have discovered a security risk please report it by emailing security@suitecrm.com. This will be delivered to the product team who handle security issues. Please don't disclose security bugs publicly until they have been handled by the security team. ---> <!--- Please be aware that as of the 31st January 2022 we no longer support 7.10.x. New issues referring to 7.10.x will only be valid if applicable to 7.12.x and above. If your issue is still applicable in 7.12.x, please create the issue following the template below --> #### Issue <!--- Provide a more detailed introduction to the issue itself, and why you consider it to be a bug --> <!--- Ensure that all code ``` is surrounded ``` by triple back quotes. This can also be done over multiple lines --> #### Expected Behavior When using an OR more than once on the same field, you expect to get results that satisfy both filters. For example: /V8/module/Accounts?filter[name][eq]=A&filter[operator]=or&filter[name][eq]=B Should give all Accounts with name A or B. #### Actual Behavior When using this filter, we only get Accounts that satisfy the second filter. The first filter seems to be ignored. When using an OR on two separate fields, the OR operator works as expected and gives results that satisfy both fitlers. #### Possible Fix n/a #### Steps to Reproduce <!--- Provide a link to a live example, or an unambiguous set of steps to --> <!--- reproduce this bug include code to reproduce, if relevant --> 1. Make sure there is an account called "A" and one called "B" 2. Try API V8 filter twice on same field: /V8/module/Accounts?filter[name][eq]=A&filter[operator]=or&filter[name][eq]=B 3. See that you only get Account "B", while account "A" is expected in the results as well. #### Context <!--- How has this bug affected you? What were you trying to accomplish? --> <!--- If you feel this should be a low/medium/high priority then please state so --> #### Your Environment <!--- Include as many relevant details about the environment you experienced the bug in --> * SuiteCRM Version used: 7.12.6 * Browser name and version (e.g. Chrome Version 51.0.2704.63 (64-bit)): * Environment name and version (e.g. MySQL, PHP 7): PHP 7.4, MariaDB 10.4 * Operating System and version (e.g Ubuntu 16.04): EL 8
Author
Owner

@serfreeman1337 commented on GitHub (Aug 31, 2022):

Quick fix to allow array values for requests:

  • Replace Api\V8\JsonApi\Repository\Filter.php file with this.
  • Example request:
    Api/V8/module/Accounts?filter[operator]=or&filter[name][eq][]=A&filter[name][eq][]=B
    (note the [])
@serfreeman1337 commented on GitHub (Aug 31, 2022): Quick fix to allow array values for requests: * Replace Api\V8\JsonApi\Repository\Filter.php file with [this](https://gist.github.com/serfreeman1337/4edcc05da6a9ccfd26047421ffa88449). * Example request: `Api/V8/module/Accounts?filter[operator]=or&filter[name][eq][]=A&filter[name][eq][]=B` (note the [])
Author
Owner

@ebogaard commented on GitHub (Sep 1, 2022):

Ah, thanks. So this is just an omission in the documentation, not a functional issue.

@ebogaard commented on GitHub (Sep 1, 2022): Ah, thanks. So this is just an omission in the documentation, not a functional issue.
Author
Owner

@serfreeman1337 commented on GitHub (Sep 1, 2022):

Current API filter design doesn't allow to specify same key name twice.
Proposed quick fix should at least allow that, but it's not official and any such changes to files migh be overriten during suitecrm update.

@serfreeman1337 commented on GitHub (Sep 1, 2022): Current API filter design doesn't allow to specify same key name twice. Proposed quick fix should at least allow that, but it's not official and any such changes to files migh be overriten during suitecrm update.
Author
Owner

@ebogaard commented on GitHub (Sep 1, 2022):

Sorry, didn't read that correctly. Didn't see a patch is necessary. Seems like a proper way to fix this, though.

@ebogaard commented on GitHub (Sep 1, 2022): Sorry, didn't read that correctly. Didn't see a patch is necessary. Seems like a proper way to fix this, though.
Author
Owner

@SuiteBot commented on GitHub (Sep 2, 2022):

This issue has been mentioned on SuiteCRM. There might be relevant details there:

https://community.suitecrm.com/t/retrieve-api-v8-data-with-or-and-and-operator/86149/2

@SuiteBot commented on GitHub (Sep 2, 2022): This issue has been mentioned on **SuiteCRM**. There might be relevant details there: https://community.suitecrm.com/t/retrieve-api-v8-data-with-or-and-and-operator/86149/2
Author
Owner

@TwizzX17 commented on GitHub (Nov 9, 2023):

Since the link to the previous proposed solution doesn't work anymore, have i tried to recreate the solution from the description:

Api/V8/JsonApi/Repository/Filter.php:75

foreach ($expr as $op => $value) {
    /* NEW CODE BLOCK - if $value is an array */
    if (is_array($value)) {
	foreach ($value as $key => $embedded_value) {
		$this->checkOperator($op);
		$where[] = sprintf(
			'%s.%s %s %s',
			$tableName,
			$field,
			constant(sprintf('%s::OP_%s', self::class, strtoupper($op))),
			$this->db->quoted($embedded_value)
		);
	}
	continue;
    }

    $this->checkOperator($op);
    $where[] = sprintf(
        '%s.%s %s %s',
        $tableName,
        $field,
        constant(sprintf('%s::OP_%s', self::class, strtoupper($op))),
        $this->db->quoted($value)
    );
}

I've added code to the parseWhere function, into the original foreach loop of $expres, to handle it a different way, if the $value is of type array. Whether this is the correct solution for the project, am i not sure of, but if anyone else needs a quick fix for this issue, then here is an example.

As described earlier in this thread - design your params with empty brackets like this:

  • Api/V8/module/Accounts?filter[operator]=or&filter[name][eq][]=A&filter[name][eq][]=B
    (note the [])
@TwizzX17 commented on GitHub (Nov 9, 2023): Since the link to the previous proposed solution doesn't work anymore, have i tried to recreate the solution from the description: Api/V8/JsonApi/Repository/Filter.php:75 ``` foreach ($expr as $op => $value) { /* NEW CODE BLOCK - if $value is an array */ if (is_array($value)) { foreach ($value as $key => $embedded_value) { $this->checkOperator($op); $where[] = sprintf( '%s.%s %s %s', $tableName, $field, constant(sprintf('%s::OP_%s', self::class, strtoupper($op))), $this->db->quoted($embedded_value) ); } continue; } $this->checkOperator($op); $where[] = sprintf( '%s.%s %s %s', $tableName, $field, constant(sprintf('%s::OP_%s', self::class, strtoupper($op))), $this->db->quoted($value) ); } ``` I've added code to the parseWhere function, into the original foreach loop of $expres, to handle it a different way, if the $value is of type array. Whether this is the correct solution for the project, am i not sure of, but if anyone else needs a quick fix for this issue, then here is an example. As described earlier in this thread - design your params with empty brackets like this: - Api/V8/module/Accounts?filter[operator]=or&filter[name][eq][]=A&filter[name][eq][]=B (note the [])
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/SuiteCRM-SuiteCRM#4842
No description provided.