Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

File validation fails when file contains sub-arrays #57

Merged
merged 2 commits into from
Jun 22, 2015

Conversation

jonhudson
Copy link

This patch updates the ServerRequestFactory to properly marshal uploaded files that are represented as a nested associative array (i.e., sub-arrays with non-numeric keys). As an example, prior to this patch, the following uploaded file structure would fail:

[
    'files' => [
        'tmp_name' => [
            'first_one' => 'php://temp',
        ],
        'tmp_name' => [
            'first_one' => 0,
        ],
        'error' => [
            'first_one' => 0,
        ],
        'name' => [
            'first_one' => 'foo.bar',
        ],
        'type' => [
            'first_one' => 'text/plain',
        ],
    ],
]

This structure is now correctly parsed.

@weierophinney
Copy link
Member

Please update the pull request summary to describe what this accomplishes; since I link to the PRs in the changelog, that's often the first place people look to understand how the change impacts them.

To be clear: we already support sub-arrays, but specifically indexed sub-arrays, vs associative sub-arrays, which is what this PR addresses.

@weierophinney weierophinney added this to the 1.0.4 milestone Jun 22, 2015
@@ -469,6 +469,7 @@ private static function createUploadedFileFromSpec(array $value)
*/
private static function normalizeNestedFileSpec(array $files = [])
{
$normalizedFiles = [];
Copy link
Member

Choose a reason for hiding this comment

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

There's no need to create a new array here.

Copy link
Member

Choose a reason for hiding this comment

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

I stand corrected when running the unit tests. Still trying to understand why this change results in any change in behavior…

Copy link
Author

Choose a reason for hiding this comment

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

The UploadedFile objects were being appended to the $files array, which meant that the tmp_name and other attributes preceeded them in the array. When same array gets passed to the validation function, it was trying to then validate strings such as the tmp_name as an instance of UploadedFile, and failing. So I just created a new array to store the UploadedFile objects in and returned that instead.

@weierophinney weierophinney merged commit 77f4cec into zendframework:master Jun 22, 2015
weierophinney added a commit that referenced this pull request Jun 22, 2015
File validation fails when file contains sub-arrays
weierophinney added a commit that referenced this pull request Jun 22, 2015
- Already have tests for nested arrays; this one is specifically for
  associative arrays.
weierophinney added a commit that referenced this pull request Jun 22, 2015
weierophinney added a commit that referenced this pull request Jun 22, 2015
weierophinney added a commit that referenced this pull request Jun 22, 2015
@weierophinney
Copy link
Member

I've updated the summary for you.

@jonhudson
Copy link
Author

Thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants