slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

Unable to set empty array to functions withQueryParams and withParsedBody #1731

Closed phm46 closed 8 years ago

phm46 commented 8 years ago

If I set empty array to function withQueryParams and withParsedBody in middleware like this:

$app->add(function ($request, $response, $next) {
    var_dump($request->getQueryParams());
    var_dump($request->getParsedBody());

    $request = $request->withQueryParams(Array())->withParsedBody(Array());

    var_dump($request->getQueryParams());
    var_dump($request->getParsedBody());
    $response = $next($request, $response);
    return $next($request, $response);
});

it returns same values but it was supposed to return empty array.

AndrewCarterUK commented 8 years ago

I couldn't reproduce your issue.

This code:

<?php

chdir(dirname(__DIR__));
require 'vendor/autoload.php';

use Slim\Http\Environment;
use Slim\Http\Request;

// Creating request with query params and parsed body
$request = Request::createFromEnvironment(Environment::mock());
$request = $request->withQueryParams(['foo' => 'bar'])->withParsedBody(['bar' => 'foo']);

echo 'With data:' . PHP_EOL;
var_dump($request->getQueryParams());
var_dump($request->getParsedBody());
echo PHP_EOL;

// Attempting to clear
$request = $request->withQueryParams(Array())->withParsedBody(Array());

echo 'Without data:' . PHP_EOL;
var_dump($request->getQueryParams());
var_dump($request->getParsedBody());

Gave:

With data:
array(1) {
  'foo' =>
  string(3) "bar"
}
array(1) {
  'bar' =>
  string(3) "foo"
}

Without data:
array(0) {
}
array(0) {
}

Could you modify this script to reproduce your issue?

phm46 commented 8 years ago

Yes, that is true. But this bug happens only if you use normal request with query and body params. If you manualy set these params with

$request = $request->withQueryParams(['foo' => 'bar'])->withParsedBody(['bar' => 'foo']);

it works corectly. Try this code:

<?php

chdir(dirname(__DIR__));
require 'vendor/autoload.php';
$app = new \Slim\App;

use Slim\Http\Environment;
use Slim\Http\Request;

$app->get('/', function($request, $response, $args){
    echo 'With data:' . PHP_EOL;
    var_dump($request->getQueryParams());
    var_dump($request->getParsedBody());

    echo 'Without data:' . PHP_EOL;
    $request = $request->withQueryParams(Array())->withParsedBody(Array());
    var_dump($request->getQueryParams());
    var_dump($request->getParsedBody());
});
$app->run();

and call it from browser as script.php/?foo=bar&bar=foo

AndrewCarterUK commented 8 years ago

Confirmed the bug, this is the offending code: https://github.com/slimphp/Slim/blob/3.x/Slim/Http/Request.php#L728-L741

AndrewCarterUK commented 8 years ago

Here is a fix for getQueryParams: https://github.com/slimphp/Slim/pull/1732

There needs to be a similar PR for getParsedBody.

akrabat commented 8 years ago

Fixed.