Skip to content

Commit

Permalink
Use InputBag where able
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Nov 4, 2021
1 parent dbe2ab2 commit e8335f1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
14 changes: 12 additions & 2 deletions EventListener/BodyListener.php
Expand Up @@ -15,6 +15,7 @@
use FOS\RestBundle\FOSRestBundle;
use FOS\RestBundle\Normalizer\ArrayNormalizerInterface;
use FOS\RestBundle\Normalizer\Exception\NormalizationException;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
Expand Down Expand Up @@ -88,7 +89,12 @@ public function onKernelRequest(RequestEvent $event): void
$decoder = $this->decoderProvider->getDecoder($format);
$data = $decoder->decode($content);
if (is_array($data)) {
$request->request = new ParameterBag($data);
if (class_exists(InputBag::class)) {
$request->request = new InputBag($data);
} else {
$request->request = new ParameterBag($data);
}

$normalizeRequest = true;
} else {
throw new BadRequestHttpException('Invalid '.$format.' message received');
Expand All @@ -105,7 +111,11 @@ public function onKernelRequest(RequestEvent $event): void
throw new BadRequestHttpException($e->getMessage());
}

$request->request = new ParameterBag($data);
if (class_exists(InputBag::class)) {
$request->request = new InputBag($data);
} else {
$request->request = new ParameterBag($data);
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions Tests/Controller/Annotations/QueryParamTest.php
Expand Up @@ -14,6 +14,7 @@
use FOS\RestBundle\Controller\Annotations\AbstractScalarParam;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;

Expand Down Expand Up @@ -45,9 +46,15 @@ public function testValueGetter()
->willReturn('foo');

$request = $this->getMockBuilder(Request::class)->getMock();
$parameterBag = new ParameterBag();
$parameterBag->set('foo', 'foobar');
$request->query = $parameterBag;

if (class_exists(InputBag::class)) {
$bag = new InputBag();
} else {
$bag = new ParameterBag();
}

$bag->set('foo', 'foobar');
$request->query = $bag;

$this->assertEquals('foobar', $this->param->getValue($request, 'bar'));
}
Expand Down
13 changes: 10 additions & 3 deletions Tests/Controller/Annotations/RequestParamTest.php
Expand Up @@ -14,6 +14,7 @@
use FOS\RestBundle\Controller\Annotations\AbstractScalarParam;
use FOS\RestBundle\Controller\Annotations\RequestParam;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;

Expand Down Expand Up @@ -45,9 +46,15 @@ public function testValueGetter()
->willReturn('foo');

$request = $this->getMockBuilder(Request::class)->getMock();
$parameterBag = new ParameterBag();
$parameterBag->set('foo', 'foobar');
$request->request = $parameterBag;

if (class_exists(InputBag::class)) {
$bag = new InputBag();
} else {
$bag = new ParameterBag();
}

$bag->set('foo', 'foobar');
$request->request = $bag;

$this->assertEquals('foobar', $this->param->getValue($request, 'bar'));
}
Expand Down

0 comments on commit e8335f1

Please sign in to comment.