json-api-dotnet / JsonApiDotNetCore

A framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.

Home Page:https://www.jsonapi.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to correctly filter a JsonApiDotNetCore resource by a list of integers

krishnaunthinkable opened this issue · comments

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 ?

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
Capture

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,

the first filter is applied on the collection of articles, while the second one is applied on the nested collection of tags.

Which type of collection is referred here?
GET /articles?include=author,tags&filter=equals(author.lastName,'Smith')&filter[tags]=any(label,'tech','design') HTTP/1.1
image

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.