PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 9, 2022

[FIXED] How to sanitize output with custom filter (API Platform)

 February 09, 2022     api-platform.com, symfony     No comments   

Issue

I'm trying to add a distance filter on my entity using api-platform (2.6.5). Everything works fine, except for the response body :

"hydra:member": [
    {
        "0": {
            "@id": "/api/places/1",
            "@type": "Place",
            "id": 1,
            "name": "test",
            "description": "test",
            "slug": "test",
            "address": "test",
            "country": "Suisse",
            "latitude": "50.6300000",
            "longitude": "3.0620000",
            "tableCount": 10,
            "tablePlace": 6,
            "tableCapacity": 60
        },
        "distance": "8.887448516728334"
    },
    {
        "0": {
            "@id": "/api/places/8",
            ...
        },
        "distance": "10.273770606986691"
    },
    {...}

The calculated distance is not considered like an entity property and api platform return them separately (This is a problem for the frontend).

Here is my entity :

/**
* @ORM\Entity(repositoryClass=PlaceRepository::class)
 * @ApiResource(
 *     normalizationContext={"groups"={"place", "place:read"}, "swagger_definition_name": "Read"},
 *     denormalizationContext={"groups"={"place", "place:write"}, "swagger_definition_name": "Write"},
 * )
 * @ApiFilter(SearchFilter::class, properties={"name": "partial", "description": "partial", "zip":"start", "city": "exact"})
 * @ApiFilter(NearbyFilter::class, properties={"location"})
 * @Entity @HasLifecycleCallbacks
 */
class Place
{
....
/**
 * @ORM\Column(type="decimal", precision=10, scale=7, nullable=true)
 * @Groups({"place"})
 */
private $latitude;

/**
 * @ORM\Column(type="decimal", precision=10, scale=7, nullable=true)
 * @Groups({"place"})
 */
private $longitude;
/**
 * @var float The distance calculated
 * @Groups({"place:read"})
 */
private $distance;

And here is my filter :

final class NearbyFilter extends AbstractContextAwareFilter
{
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
    {
        
        if ($property !== 'location') {
            return;
        }
        
        [$lat, $lng, $radius] = explode(',', $value);

        // add distance in DQL based on filter location point
        $queryBuilder
            ->addSelect('
                (6371.0 * acos (
                    cos ( radians(:latitude) )
                    * cos( radians(o.latitude) )
                    * cos( radians(o.longitude) - radians(:longitude) )
                    + sin ( radians(:latitude) )
                    * sin( radians(o.latitude) )
                )) AS distance')
            ->having('distance <= :radius')
            ->setParameter('radius', $radius)
            ->setParameter('latitude', $lat)
            ->setParameter('longitude', $lng)
            ->orderBy('distance')
        ;

    }

As you can see, i tried to add unmapped property 'distance' but that have no effect :/ I also tried to use Dto and Denormalizer to fix this without success (But I'm new with it).

Can you have an idea to fix my problem ?

Thanks !


Solution

Finally find a solution with DataProvider :

namespace App\DataProvider;

use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\Place;

class PlaceDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface
{
    private $collectionDataProvider;
    public function __construct(CollectionDataProviderInterface $collectionDataProvider)
    {
        $this->collectionDataProvider = $collectionDataProvider;
    }

    public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
    {
        $places = $this->collectionDataProvider->getCollection($resourceClass, $operationName, $context);

        foreach ($places as &$place) {
            if(!$place instanceof Place && is_array($place)) {
                $sanitizedPlace = $place[0];

                if( !is_null( $place['distance'] ) ){
                    $sanitizedPlace = $this->setDistance($sanitizedPlace, $place['distance']);
                }

                $place = $sanitizedPlace;
            }
        }
        
        return $places;
    }

    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
    {
        return $resourceClass === Place::class;
    }

    public function setDistance(Place $place, $distance): Place
    {
        $place->setDistance($distance);
        
        return $place;
    }
}

Thanks



Answered By - Bonbelo
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing