-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
How to correctly filter a JsonApiDotNetCore resource by a list of integers #1417
Comments
This isn't currently possible. A JSON:API attribute is an opaque value, ie you can't descend into its structure. So accessing collection elements or nested properties is not possible. If you have an array of numbers, this workaround may unblock you. Otherwise, you can always model this through JSON:API relationships. Since the recent release of EF Core 8, this potentially becomes possible to implement, although there are still many rough edges. It also depends on which database you're using, as not all drivers support all the new features. Exploring this is tracked at #1214. Would you be interested in implementing this? |
As per the official documentation of json:api we can have array objects in the attributes. Also sharing the link and screenshot for the same - https://jsonapi.org/format/#document-resource-object-attributes So it seems that the support to filter the array/list objects is missing in jsonapidotnetcore as of now. Also need to clarify that it is mentioned in the documentation that In the below request,
Which type of collection is referred here? |
Yes, JSON:API allows it, and you can use them in request/response bodies. There's just no implementation to use them in query string parameters. That's why we welcome a contribution. Additionally, it would be nice if we can come up with syntax to allow partial updates inside an attribute value. As for your second question: that's how it works when modelled as a JSON:API relationship, it does not apply to complex attributes. |
Closing in favor of #1439. |
I am facing the similar issue which i see raised in earlier closed questions in which while trying to filter a resource based on a list of integers contained in a field named list 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?
So basically want to read the filter query parameter list of integer of 'groups' and need to get the list of integer 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>
Does Jsonapidotnetcore support this ?
The text was updated successfully, but these errors were encountered: