Skip to content
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

[5.4] Don't call method cleanParameterBag() twice for json request #18840

Merged
merged 1 commit into from
Apr 19, 2017
Merged

[5.4] Don't call method cleanParameterBag() twice for json request #18840

merged 1 commit into from
Apr 19, 2017

Conversation

dmasior
Copy link

@dmasior dmasior commented Apr 18, 2017

Fix for bug I found in TransformsRequest middleware. Currently for json request it is calling method cleanParameterBag 2x so method transform() is executed 2x. The result is malformed data (transformed twice).

@taylorotwell
Copy link
Member

How do I recreate the bug on a fresh Laravel installation?

@dmasior
Copy link
Author

dmasior commented Apr 18, 2017

To reproduce error I created test:

<?php

namespace Tests\Feature;

use Tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->postJson('/', ['test' => 2]);

        $response->assertJson(['test' => 8,]); // Expected to be 2+2=4 not 8 (2+2+4) as IncrementVarTwice->transform() method should be executed once
    }
}

then defined example route:

\Route::post('/', function (Request $request) {
    return \Response::json($request->all());
});

and a middleware:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\TransformsRequest;

class IncrementVarTwice extends TransformsRequest
{
    protected function transform($key, $value)
    {
        if ($key === 'test') {
            $value = $value + $value;
        }

        return $value;
    }
}

and registered it in App\Http\Kernel:

protected $middleware = [
        \App\Http\Middleware\IncrementVarTwice::class,
    ];

After that all i got:

PHPUnit 5.7.19 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 96 ms, Memory: 6.00MB

OK (2 tests, 2 assertions)

Which is NOK

@dmasior
Copy link
Author

dmasior commented Apr 18, 2017

The problem occurs only when sending data in json request. Not as query param, because $request->query is transformed only once:

    /**
     * Clean the request's data.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function clean($request)
    {
        $this->cleanParameterBag($request->query);

        $this->cleanParameterBag($request->request);

        if ($request->isJson()) {
            $this->cleanParameterBag($request->json());
        }
    }

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

Successfully merging this pull request may close these issues.

2 participants