Skip to content
This repository
Browse code

fix param converter to handle correctly isOptional parameter

  • Loading branch information...
commit a3871ccc74aa99838eb86ebab3b2704d2b163693 1 parent 2e7bb13
Jérémie Augustin jaugustin authored
9 Request/ParamConverter/PropelParamConverter.php
@@ -29,8 +29,13 @@ public function apply(Request $request, ConfigurationInterface $configuration)
29 29 // find by Pk
30 30 if (in_array('id', $exclude) || false === $object = $this->findPk($classQuery, $request)) {
31 31 // find by criteria
32   - if (false === $object = $this->findOneBy($classQuery, $request, $exclude)) {
33   - throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
  32 + if (false === ($object = $this->findOneBy($classQuery, $request, $exclude))) {
  33 + if ($configuration->isOptional()) {
  34 + //we find nothing but the obejct is optional
  35 + $object = null;
  36 + } else {
  37 + throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
  38 + }
34 39 }
35 40 }
36 41
12 Tests/Request/ParamConverter/PropelParamConverterTest.php
@@ -128,4 +128,16 @@ public function testParamConverterFindLogicError()
128 128 $configuration = new ParamConverter(array('class' => 'Propel\PropelBundle\Tests\Fixtures\Model\Book', 'name' => 'book'));
129 129 $paramConverter->apply($request, $configuration);
130 130 }
  131 +
  132 +
  133 + public function testParamConverterFindWithOptionalParam()
  134 + {
  135 + $paramConverter = new PropelParamConverter();
  136 + $request = new Request(array(), array(), array('book' => null));
  137 + $configuration = new ParamConverter(array('class' => 'Propel\PropelBundle\Tests\Fixtures\Model\Book', 'name' => 'book', 'optional' => 'true'));
  138 + $paramConverter->apply($request, $configuration);
  139 +
  140 + $this->assertNull($request->attributes->get('book'),
  141 + 'param "book" should be null if book is not found and the parameter is optional');
  142 + }
131 143 }

0 comments on commit a3871cc

Please sign in to comment.
Something went wrong with that request. Please try again.