question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request must be send two times to make request filters applied

See original GitHub issue

According 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:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
michaelyalicommented, Mar 25, 2020

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:

  1. Each request is handled by the CrudRequestInterceptor which then stores and passes a parsed request params/query in the req.parsed which is being used by CrudController methods.
  2. You modify 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 merging options.filter with the request query/params is already being done by the CrudRequestInterceptor. That’s why a method cares only about req.parsed.search. And that’s why during the 1st method call you’re getting one result.
  3. When you do this req.options.query.filter = ... you thus modify CrudOption object by a link. And during the 2nd method call your CrudOptions object is already modified with your changes and that’s why the CrudRequestInterceptor parses a request with your query and creates a correct req.parsed.search object. That’s why your 2nd method call will give you another result.

Hope this explanation helped

1reaction
maximelafariecommented, Mar 25, 2020

Ok thank you for these explanations. 🖖

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found