Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validator] UniqueValidator - normalize before reducing keys #54137

Merged
merged 1 commit into from Mar 9, 2024

Conversation

Brajk19
Copy link
Contributor

@Brajk19 Brajk19 commented Mar 2, 2024

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues
License MIT

In #42403 checking for uniqueness of certain collection keys was enabled. Method UniqueValidator::reduceElementKeys removes all keys which are not specified.
Problem is that this happens before normalization, which in my opinion is not great because that method accepts array argument and if i have some object (DTO), TypeError will be thrown.

Example:

class ParentDTO
{
    /**
     * @var ChildDTO[]
     */
    #[Assert\Unique(
        normalizer: [ChildDTO::class, 'normalize']
        fields: 'id'
    )]
    public array $children;
}
class ChildDTO
{
    public string $id;
    public string $name;

    public static function normalize(self $obj): array
    {
        return [
            'id' => $obj->id,
            'name' => $obj->name
        ];
    }
}

Because normalization will happen after reduceElementKeys this will be thrown:
TypeError: Symfony\Component\Validator\Constraints\UniqueValidator::reduceElementKeys(): Argument #2 ($element) must be of type array, ...\ChildDTO given, called in .../UniqueValidator.php on line 48

If $element = $normalizer($element); is executed before reduceElementKeys it would enable using Assert\Unique with array of objects when correctly normalized

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "7.1" but it seems your PR description refers to branch "6.4".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@Brajk19 Brajk19 changed the base branch from 7.1 to 6.4 March 2, 2024 18:07
@xabbuh
Copy link
Member

xabbuh commented Mar 7, 2024

For 5.4?

@stof
Copy link
Member

stof commented Mar 7, 2024

@xabbuh the original PR was merged in 6.1

@xabbuh
Copy link
Member

xabbuh commented Mar 7, 2024

I see, this was introduced in #42403 and not in #38488.

@fabpot fabpot modified the milestones: 7.1, 6.4 Mar 9, 2024
@fabpot fabpot force-pushed the unique-validator-normalizer-fix branch from 2e13553 to 77df90b Compare March 9, 2024 13:47
@fabpot
Copy link
Member

fabpot commented Mar 9, 2024

Thank you @Brajk19.

@fabpot fabpot merged commit 04652b7 into symfony:6.4 Mar 9, 2024
8 of 10 checks passed
This was referenced Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants