diff --git a/Request/ParamConverter/ParamConverterManager.php b/Request/ParamConverter/ParamConverterManager.php index 574bdeb8..fd525c74 100644 --- a/Request/ParamConverter/ParamConverterManager.php +++ b/Request/ParamConverter/ParamConverterManager.php @@ -40,6 +40,13 @@ public function apply(Request $request, $configurations) } foreach ($configurations as $configuration) { + // If the value is already an instance of the class we are trying to convert it into + // we should continue as no convertion is required `is_a` was un-deprecated in 5.3 + $value = $request->attributes->get($configuration->getName()); + if (is_object($value) && is_a($value, $configuration->getClass())) { + continue; + } + foreach ($this->all() as $converter) { if ($converter->supports($configuration)) { if ($converter->apply($request, $configuration)) { diff --git a/Tests/Request/ParamConverter/ParamConverterManagerTest.php b/Tests/Request/ParamConverter/ParamConverterManagerTest.php index 7c26da2a..ef623aeb 100644 --- a/Tests/Request/ParamConverter/ParamConverterManagerTest.php +++ b/Tests/Request/ParamConverter/ParamConverterManagerTest.php @@ -65,6 +65,33 @@ public function testApply() $this->manager->apply(new Request(), $configurations); } + public function testApplyNotCalledOnAlreadyConvertedObjects() + { + + $converter = $this->createParamConverterMock(); + $converter + ->expects($this->never()) + ->method('supports') + ; + + $converter + ->expects($this->never()) + ->method('apply') + ; + + $this->manager->add($converter); + + $request = new Request(); + $request->attributes->set('converted', new \stdClass); + + $configuration = new Configuration\ParamConverter(array( + 'name' => 'converted', + 'class' => 'stdClass', + )); + + $this->manager->apply($request, array($configuration)); + } + protected function createParamConverterMock() { return $this->getMock('Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface');