Skip to content

[Mime] Throw InvalidArgumentException on invalid form field type inside array #52033

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

Merged
merged 1 commit into from
Oct 17, 2023

Conversation

l-naumann
Copy link
Contributor

@l-naumann l-naumann commented Oct 13, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? no
Deprecations? no
Tickets n/a
License MIT

Example code:

$f = new FormDataPart([
            'foo' => [
                'bar' => 'baz',
                'qux' => [
                    'quux' => 1,
                ],
            ],
        ]);
$f->getParts();

Currently, when you use a disallowed type inside an array, a TypeError is thrown:
Symfony\Component\Mime\Part\Multipart\FormDataPart::configurePart(): Argument #2 ($part) must be of type Symfony\Component\Mime\Part\TextPart, int given

This change adds a clearly understandable error message:
The value of the form field "foo[qux][quux]" can only be a string, an array, or an instance of TextPart ("int" given).

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@@ -75,6 +75,10 @@ private function prepareFields(array $fields): array
return;
}

if (!\is_string($item) && !$item instanceof TextPart) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can move this check to the constructor directly? That would be more consistent as we already check some of that there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also considered this, but in my opinion it has two disadvantages:

  1. We have to traverse through the array recursively. In prepareFields this is already the case, but in the constructor we only traverse at top-level.
  2. We don't have the field name in form of "foo[qux][quux]" in the constructor. Either we cannot include it in the error message, or we have to generate it redundantly.

Also we already have a check (regarding the form field values with integer keys) inside prepareFields, so I don't feel like adding another check there affects the consistency much.

How about we remove the check in the constructor altogether? All the checks could be done inside prepareFields and the constructor would look like this:

 public function __construct(array $fields = [])
    {
        parent::__construct();

        $this->fields = $fields;

        // HTTP does not support \r\n in header values
        $this->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having all the checks in prepareFields works for me.

@nicolas-grekas
Copy link
Member

Thank you @l-naumann.

@nicolas-grekas nicolas-grekas merged commit c6930e3 into symfony:6.4 Oct 17, 2023
@l-naumann l-naumann deleted the dx/form-data-part branch October 17, 2023 12:08
@xabbuh xabbuh mentioned this pull request Aug 13, 2024
xabbuh added a commit that referenced this pull request Aug 13, 2024
This PR was merged into the 6.4 branch.

Discussion
----------

[Mime]  fix test

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

updates the test added in #57892 to reflect the change of behavior introduced with #52033

Commits
-------

2e8b9ce fix test
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.

4 participants