Request must be send two times to make request filters applied
See original GitHub issueAccording to my controller:
@Crud({
model: {
type: Lead,
},
query: {
alwaysPaginate: true,
join: {
produit: {
allow: ['nom'],
eager: true
}
}
},
params: {
id: {
field: 'id',
type: 'uuid',
primary: true
}
}
})
@UseGuards(AuthGuard('jwt'))
@UseInterceptors(ClassSerializerInterceptor)
@Controller('leads')
@ApiUseTags('Leads')
@ApiBearerAuth()
export class LeadController implements CrudController<Lead> {
constructor(public service: LeadService, public jwtUserService: JwtUserService) { }
get base(): CrudController<Lead> {
return this;
}
@Override()
async getMany(
@ParsedRequest() req: CrudRequest,
@Headers() headers,
@Query() query
) {
const user = await this.jwtUserService.getUserFromJwt(headers.authorization);
const hasQuery = (key: string) => !!query && !!query[key];
if (user.role === 'user') {
req.options.query.filter = {
user: {
$eq: user.id,
}
};
}
req.options.query.filter = {
...req.options.query.filter,
...hasQuery('produit') && { 'produit.id': { $in: query.produit } },
...hasQuery('statut') && { statut: { $in: query.statut } },
};
return this.base.getManyBase(req);
}
}
Let’s take the following routes (GET): If I’m calling
http://localhost:3000/leads
It returns the normal collection of Lead
resources:
{
"data": [
// Some data
],
"count": 100,
"total": 100,
"page": 1,
"pageCount": 1
}
But if I’m calling a first time:
http://localhost:3000/leads?statut%5B%5D=en_cours
It returns the same content as the global route /leads
But if I’m calling it a second time, it returns the expected result:
{
"data": [],
"count": 0,
"total": 0,
"page": null
}
I really don’t understand why should I send the same request two times to get the right result.
Moreover, req.options.query.filter
has always the right value each time I make a request. It’s only the result that is incoherent. (Sorry if it’s not related to the library, but I really don’t get it).
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
jqxgrid initial filter in ready request sent two times - jQWidgets
I have setup the initial filter property in jqxgrid . But when the initial filter call under the ready function in jqxgrid in...
Read more >Building an API Call Holding Multiple Filter-selected Values in ...
The end goal being that I will be able to send a request over the API that holds the values of multiple filters...
Read more >Use Request Filtering - IIS - Microsoft Learn
When the double-encoded requests filter is enabled, IIS normalizes the URL twice; if the first normalization is different from the second, the ...
Read more >What Is OncePerRequestFilter? - Baeldung
When processing requests asynchronously, both threads go through the same filter chain. Consequently, the filter is called twice: first, when ...
Read more >Implementing Action Filters in ASP.NET Core - Code Maze
Find out how to use action filters to write cleaner actions and ... is authorized for the current request; Resource filters – They...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here are the scenario and explanations of your bug which is not a bug actually but a misunderstanding of crud lib internals which is totally fine. I’ll explain more detailed:
CrudRequestInterceptor
which then stores and passes a parsed request params/query in thereq.parsed
which is being used by CrudController methods.req.options.query.filter
in the controller’s method but the method will not handle it and will not take this filter into account because of all the logic related to the mergingoptions.filter
with the request query/params is already being done by theCrudRequestInterceptor
. That’s why a method cares only aboutreq.parsed.search
. And that’s why during the 1st method call you’re getting one result.req.options.query.filter = ...
you thus modifyCrudOption
object by a link. And during the 2nd method call yourCrudOptions
object is already modified with your changes and that’s why theCrudRequestInterceptor
parses a request with your query and creates a correctreq.parsed.search
object. That’s why your 2nd method call will give you another result.Hope this explanation helped
Ok thank you for these explanations. 🖖