8000 [PropertyAccessor] Missing TypeError catch · Issue #45199 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccessor] Missing TypeError catch #45199

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
b1rdex opened this issue Jan 27, 2022 · 2 comments
Closed

[PropertyAccessor] Missing TypeError catch #45199

b1rdex opened this issue Jan 27, 2022 · 2 comments

Comments

@b1rdex
Copy link
Contributor
b1rdex commented Jan 27, 2022

Symfony version(s) affected

6.0.2

Description

I stumbled upon a TypeError: Cannot assign string to property App\Api\Model\Request\CoordsCheckerRequest::$latitude of type float.

Browsing the code, I see there is a TypeError -> InvalidArgumentException wrapper:
https://github.com/symfony/symfony/blob/6.0/src/Symfony/Component/PropertyAccess/PropertyAccessor.php#L206-L217

Looks like another one should be added there.

How to reproduce

class CoordsCheckerRequest {
    public float $longitude;
}

$accessor = new PropertyAccessor();
$request = new CoordsCheckerRequest();
$accessor->setValue($request, 'longitude', 'foo');

Possible Solution

if (preg_match('/^Cannot assign (\S+) to property \S+::\$\S+ of type (\S+)$/', $message, $matches)) {
    [, $actualType, $expectedType] = $matches;

    throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
}

Additional Context

No response

@fancyweb
Copy link
Contributor
fancyweb commented Feb 3, 2022

Can you open a PR on 4.4 with your possible solution plz? I think it makes sense.

@b1rdex
Copy link
Contributor Author
b1rdex commented Feb 4, 2022

@fancyweb Sure! Please see #45306

nicolas-grekas added a commit that referenced this issue Feb 4, 2022
This PR was merged into the 4.4 branch.

Discussion
----------

[PropertyAccessor] Add missing TypeError catch

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #45199 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT

This bugfix wraps `TypeError: Cannot assign TYPE to property *::$* of type TYPE` with `InvalidArgumentException`.

<!--
Replace this notice by a short README for your feature/bugfix.
This will help reviewers and should be a good start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against the latest branch.
 - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
 - Never break backward compatibility (see https://symfony.com/bc).
-->

Commits
-------

db257f3 [PropertyAccessor] Add missing TypeError catch
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