Skip to content

How to pass filter in query params if resource has array attribute property #1370

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

Closed
bhavik-mittal opened this issue Oct 26, 2023 · 12 comments
Closed
Labels

Comments

@bhavik-mittal
Copy link

I have a array field attribute in my resource named 'int[ ]? Statuses' which is an array of integer. I want to filter the resource based on 'Statuses' field in resource so how the filter needs to be passed from the request body?

@bkoelman
Copy link
Member

How is this stored in the database?

@bhavik-mittal
Copy link
Author

It is being stored as text array in database.

@bkoelman
Copy link
Member

In that case you can't filter on it. Would cause a full table scan.

@bhavik-mittal
Copy link
Author

Is there atleast any way to get an array attribute property in the QueryLayer using filter query parameter using JsonApiDotNetCore?

@bkoelman
Copy link
Member

No, there is not. The JSON:API way is to model as a to-many relationship, which unlocks all existing features.

@bkoelman
Copy link
Member

A workaround to enable filtering on numbers could be to store and expose as a string field with separators around the numbers and use the contains filter function.
For example: ?filter=contains(yourfield,'#1#') with database column value #1#2#10#.

@bhavik-mittal
Copy link
Author

Is there at least any way to read the filter data of array field attribute Statuses[ ] from the QueryLayer?

@bkoelman
Copy link
Member

bkoelman commented Nov 6, 2023

I don't understand your question. QueryLayer contains what's in the request query string. It does not contain the data returned from the database.

@bhavik-mittal
Copy link
Author

bhavik-mittal commented Nov 6, 2023

Yes, i want to read the filter query parameter array field attribute Statuses[ ] and need to get the array filter value in the controller from QueryLayer to apply my custom logic as i have created the custom controllers which are inheriting from JsonApiController<TResource, TIdentifier>

@bkoelman
Copy link
Member

bkoelman commented Nov 6, 2023

I don't know. What have you tried?

@bkoelman
Copy link
Member

Closing due to inactivity.

@krishnaunthinkable
Copy link

krishnaunthinkable commented Dec 6, 2023

I am facing the same issue while trying to filter a resource based on a list of integers contained in a field named list<int> Groups. My goal is to retrieve resources where the 'Groups' field includes any of the specified integers. I attempted to use the following filter query in the URL:filter=any(groups,'1','2')

However, I encountered an error stating that the filter is invalid and that there was a failure to convert '1' from type 'String' to type 'List`1'.
Here is the error message I received:

"errors": [
    {
        "id": "01689e1d-2ba6-4043-aa3b-56bfe9712fab",
        "status": "400",
        "title": "The specified filter is invalid.",
        "detail": "Failed to convert '1' of type 'String' to type 'List`1'. Failed at position 15: any(groups,^'1','2')",
        "source": {
            "parameter": "filter"
        }
    }
]

And here is the stack trace for further context:

at JsonApiDotNetCore.QueryStrings.FilterQueryStringParameterReader.ReadSingleValue(String parameterName, String parameterValue)
at JsonApiDotNetCore.QueryStrings.FilterQueryStringParameterReader.Read(String parameterName, StringValues parameterValue)
at JsonApiDotNetCore.QueryStrings.QueryStringReader.ReadAll(DisableQueryStringAttribute disableQueryStringAttribute)
at JsonApiDotNetCore.Middleware.AsyncQueryStringActionFilter.d__2.MoveNext()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<g__Awaited|10_0>d.MoveNext()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<g__Awaited|26_0>d.MoveNext()

and i have also try this filter[groups]=1,2 , filter[groups]='1,2' and facing same issue.

Could you please advise on the correct way to pass the filter for a list of integers in the request body using JsonApiDotNetCore? Is there a specific syntax or format that I need to follow to filter on the Groups field for values like 1 and 2?
Does Jsonapidotnetcore support this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants