api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.39k stars 848 forks source link

dynamic validation groups not working #4174

Closed remoteclient closed 3 years ago

remoteclient commented 3 years ago

API Platform version(s) affected: 2.6.3

Description
I have to implement dynamic validation. Think I have done it by the books, but it seems that the invoke function of my GroupsGenerator is not called.

How to reproduce
The GroupsGenerator:

<?php
namespace App\Validator;
use ApiPlatform\Core\Bridge\Symfony\Validator\ValidationGroupsGeneratorInterface;
use App\Entity\Lead;
use App\Entity\PartnerWebsite;
use Symfony\Component\Security\Core\Security;
final class LeadGroupsGenerator implements ValidationGroupsGeneratorInterface
{
    private Security $security;
    public function __construct(Security $security)
    {
        $this->security = $security;
    }
    public function __invoke($object): array
    {
        var_dump($this->security->getUser() instanceof PartnerWebsite);die();
        assert($object instanceof Lead);
        return $this->security->getUser() instanceof PartnerWebsite ? ['PartnerWebsite'] : ['Lead'];
    }
}

From my xml configuration:

        <collectionOperations>
            <collectionOperation name="post">
                <attribute name="method">POST</attribute>
                <attribute name="security">is_granted('ROLE_MWS_LEAD_CREATE')</attribute>
                <attribute name="denormalization_context">
                    <attribute name="groups">
                        <attribute>mws_lead_create</attribute>
                    </attribute>
                </attribute>
                <attribute name="normalization_context">
                    <attribute name="groups">
                        <attribute>mws_lead_view</attribute>
                    </attribute>
                </attribute>
                <attribute name="validation_groups">
                    <attribute>LeadGroupsGenerator::class</attribute>
                </attribute>
            </collectionOperation>
        </collectionOperations>

The Generator is registered, but __invike function is not called:

php bin/console debug:container | grep LeadGroups
  App\Validator\LeadGroupsGenerator                                    App\Validator\LeadGroupsGenerator 
remoteclient commented 3 years ago

The issue is solved. Problem was with the documentation. The definition for the Generator has to be:

<attribute name="validation_groups">App\Validator\LeadGroupsGenerator</attribute>