techniq/odata-query

Double filters in querystring

marcvan-dijk opened this issue · 3 comments

Hi Sean,

First of all, thanks for your package, it saves a lot of time creating those queries. I'm facing an issue that deeper objects result in double queries. Please consider the following object:

{
    "filter": {
        "Nomination": {
            "ScheduledAt": {
                "ge": "2021-12-12T23:00:00.000Z",
                "le": "2021-12-13T22:59:59.999Z"
            },
            "NominationStatusValues": {
                "any": {
                    "NominationStatusID": 3
                }
            }
        }
    },
    "orderBy": "id",
    "count": true,
    "top": 15
}

this results in the following querystring (with some enters for readability):

$filter=
Nomination/ScheduledAt ge 2021-12-12T23:00:00.000Z and 
Nomination/ScheduledAt le 2021-12-13T22:59:59.999Z and 
Nomination/NominationStatusValues/any(nominationstatusvalues:nominationstatusvalues/NominationStatusID eq 3) and 
Nomination/ScheduledAt ge 2021-12-12T23:00:00.000Z and 
Nomination/ScheduledAt le 2021-12-13T22:59:59.999Z and 
Nomination/NominationStatusValues/any(nominationstatusvalues:nominationstatusvalues/NominationStatusID eq 3)
&$orderby=id&$count=true&$top=15

Am I doing something wrong or did I stumble upon a bug? I've tried several versions (6.7.1, 6.7.0, 6.6.0) but to no avail.

Thanks in advance

Can confirm, we've got the exact same issue here:

let filter = {
    users: {
      firstName: { contains: 'admin' },
      isActive: { eq: true },
    },
    not: { users: { roles: { any: {} } } },
};

buildQuery({ filter });

results in:

"?$filter=contains(users/firstName,'admin') and users/isActive eq true and contains(users/firstName,'admin') and users/isActive eq true and not(users/roles/any())"

@techniq We also encounter the same issue here. Using version 7.0.3.
Nested query object with multiple props on the same level, using odata "in" operator, create a duplicated query string. Duplication happens as many times as the number of props on the same level (3 props = 3 duplications).
Looking forward for solution. Thank you.

import buildODataQueryFn from 'odata-query';

const query = {
  filter: {
    components: {
      control_number: {
        in: ['0158'],
      },
      component1_number: {
        in: ['0179'],
      },
      component2_number: {
        in: ['0187'],
      },
    },
  },
};

const queryString = buildODataQueryFn(query)

console.log(queryString)

// ?$filter=components/control_number in ('0158') and components/component1_number in ('0179') and components/component2_number in ('0187') and components/control_number in ('0158') and components/component1_number in ('0179') and components/component2_number in ('0187') and components/control_number in ('0158') and components/component1_number in ('0179') and components/component2_number in ('0187')

I'd be happy to review a PR, but to be honest, I don't have much time to dig into the issue.