Core: Proposition: using annotation for filters

Created on 6 Apr 2017  路  11Comments  路  Source: api-platform/core

Thank you again for your great work !

It would be easier to add a filter in the entity with annotations instead of adding a service.
For example :

<?php

// src/AppBundle/Entity/Offer.php

namespace AppBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;          // for example

/**
 * @ApiResource()
 */
class Offer
{
    /**
     * @ApiFilter('type="search_filter", precision="exact")
     */
    private $id;

    /**
     * @ApiFilter('type="date_filter")
     */
    private $date;

    ...
}

The goal here is to simplify the use of filters with Api-Platform, and I think it's important because the great success of Api-Platform is it's simplicity.

enhancement

Most helpful comment

I like the idea, may I suggest an alternative syntax?

<?php

// src/AppBundle/Entity/Offer.php

namespace AppBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;

/**
 * @ApiResource
 */
class Offer
{
    /**
     * @ApiFilter(SearchFilter::class, strategy="exact")
     */
    private $id;

    /**
     * @ApiFilter(DateFilter::class)
     */
    private $date;

    ...
}

I would also allow to add the @ApiFilter on the class to enable the filter for all properties.

All 11 comments

I think this is a good idea. We should wait for @api-platform/core-team approval first, but would you ike to work on a PR for this feature?

I like the idea, may I suggest an alternative syntax?

<?php

// src/AppBundle/Entity/Offer.php

namespace AppBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;

/**
 * @ApiResource
 */
class Offer
{
    /**
     * @ApiFilter(SearchFilter::class, strategy="exact")
     */
    private $id;

    /**
     * @ApiFilter(DateFilter::class)
     */
    private $date;

    ...
}

I would also allow to add the @ApiFilter on the class to enable the filter for all properties.

I would love to see this feature! But we should also think about filters applied to multiple fields (i.e. search_filter)

We'll need a bridge with the Symfony DI component:

  1. collect all annotations for the same filter in a given class
  2. create a service extending the abstract one with all properties of this class configured
  3. populate the existing attributes key (we should also keep the current system to allow to apply a filter only on a specific operation).
     * @ApiFilter(SearchFilter::class, strategy="exact")

This is really neat, it'll help using custom filters too :).

I would also allow to add the @ApiFilter on the class to enable the filter for all properties.

This might work well on simple filters, or if you want the same behavior for every properties. I doubt that this would be useful in real case scenarios.

For example with the search_filter:

/**
 * @ApiFilter(SearchFilter::class, strategy="exact")
 */
class Dummy {
  /**
   * @ApiFilter(SearchFilter::class, strategy="partial")
   */
   private $foo;
}

This would mean that properties filters override the resource ones?

What about multiple filters on one property? Would there be multiple annotations? (or @ApiFilters()).

Last but not least, on the class if say we put a date_filter:

/**
 * @ApiFilter(DateFilter::class, strategy=DateFilter::EXCLUDE_NULL)
 */
class Dummy {
}

This should only apply to Dates properties on the given class.

I'd love such feature! Happy to prototype it :)

What about multiple filters on one property? Would there be multiple annotations?

Yes, exactly like for @Assert annotations.

cool feature , I can propose something also ^^

Is someone working on this yet?

Could be on 2.1.0 maybe @soyuka.

I don't think someone has been working on it, if someone want begin it, that could be neat ! @sroze @jordscream

I'll have time this week end for this. Though it may be better to wait for #1099.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stipic picture stipic  路  3Comments

theshaunwalker picture theshaunwalker  路  3Comments

DenisVorobyov picture DenisVorobyov  路  3Comments

breitsmiley picture breitsmiley  路  3Comments

rockyweng picture rockyweng  路  3Comments