Skip to content

[Validator] Handling of lists slightly changed from 4.2.5 to 4.2.6 #33903

@rejinka

Description

@rejinka

Symfony version(s) affected: >=4.2.6,>=4.3.0

Description
Please read the text in context of the part "how to reproduce".

In Symfony 4.2.5, it was not necessary to add the Valid-Constraint to a list to get the objects in this list to be validated (e.g. would result in a NotNull-Violation, if the name was null). In 4.2.6 this is now necessary.

I didn't found anything about this behavior-change. Therefore i searched the documentation, the UPGRADE- and CHANGELOG-files and the tests in the validator-component. Also i don't know, whether this should be handled as a BC-break or a Bugfix (as it always was necessary to use the Valid-Constraint, if the relation was 1:1).

How to reproduce

<?php
declare(strict_types = 1);

namespace Tests;

use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorBuilder;

class Parent_425
{

    /**
     * @Assert\All(
     *     @Assert\Type(Child_::class)
     * )
     */
    public $children;

}

class Parent_426
{

    /**
     * @Assert\All(
     *     @Assert\Type(Child_::class)
     * )
     * @Assert\Valid
     */
    public $children;

}

class Child_
{

    /**
     * @Assert\NotNull
     */
    public $name = null;

}


class Valid extends TestCase
{

    /**
     * @var ValidatorInterface
     */
    private $validator;

    protected function setUp()
    {
        $this->validator = (new ValidatorBuilder())
            ->addLoader(
                new AnnotationLoader(
                    new AnnotationReader(

                    )
                )
            )
            ->getValidator();
    }

    /**
     * @test
     */
    public function valid_constraint_not_needed_in_425()
    {
        $parent = new Parent_425();
        $parent->children = [new Child_()];

        $violations = $this->validator->validate($parent);

        self::assertSame(1, $violations->count(), sprintf('Not correct in %s.%s', Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION));
    }

    /**
     * @test
     */
    public function valid_constraint_needed_since_426()
    {
        $parent = new Parent_426();
        $parent->children = [new Child_()];

        $violations = $this->validator->validate($parent);

        self::assertSame(1, $violations->count(), sprintf('Not correct in %s:%s', Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION));
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions