8000 [PropertyAccessor] Unable to write to singular property using setter while plural adder/remover exist · Issue #28961 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccessor] Unable to write to singular property using setter while plural adder/remover exist #28961

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

Closed
karser opened this issue Oct 23, 2018 · 0 comments

Comments

@karser
Copy link
Contributor
karser commented Oct 23, 2018

Hi there, I found a bug that exists in 3.4, 4.1 and master branches.
I'll submit a PR soon.

What's happening:
That's the priorities issue: findAdderAndRemover('User', 'email') is called earlier than $this->isMethodAccessible('User', 'setEmail', 1)

The reproducer:

$user = new UserWithSingularAndPluralProps();
$accessor = PropertyAccess::createPropertyAccessor();

if ($accessor->isWritable($user, 'email')) {
    $accessor->setValue($user, 'email', 'test@email.com');
}

Here is User entity that has email and emails fields:

/**
 * Notice we don't have setter for emails
 * because we count on adder/remover
 */
class UserWithSingularAndPluralProps
{
    /** @var string|null */
    private $email;

    /** @var array */
    private $emails = [];

    /**
     * @return null|string
     */
    public function getEmail(): ?string
    {
        return $this->email;
    }

    /**
     * @param null|string $email
     */
    public function setEmail(?string $email): void
    {
        $this->email = $email;
    }

    /**
     * @return array
     */
    public function getEmails(): array
    {
        return $this->emails;
    }

    /**
     * @param string $email
     */
    public function addEmail(string $email): void
    {
        $this->emails[] = $email;
    }

    /**
     * @param string $email
     */
    public function removeEmail(string $email): void
    {
        $this->emails = array_diff($this->emails, [$email]);
    }
}

Expected result: the value is assigned to user.email.

Actual error:
Invalid argument supplied for foreach() in symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 576

nicolas-grekas added a commit that referenced this issu 6B81 e Nov 15, 2018
…y using setter while plural adder/remover exist (karser)

This PR was merged into the 3.4 branch.

Discussion
----------

[PropertyAccessor] Fix unable to write to singular property using setter while plural adder/remover exist

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28961
| License       | MIT

Please take a look at the tests I added - they describe the issue. It has to do with the priorities: `findAdderAndRemover('User', 'email')` is called earlier than `$this->isMethodAccessible('User', 'setEmail', 1)`

Commits
-------

8238f16 [PropertyAccessor] Fix unable to write to singular property using setter while plural adder/remover exist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0