Skip to content

Commit

Permalink
Merge pull request #883 from coopTilleuls/jsontoform-remove-false-data
Browse files Browse the repository at this point in the history
Enable or disable removing false data for JsonToFormDecoder
  • Loading branch information
lsmith77 committed Oct 14, 2014
2 parents 41ebd9c + 909c67b commit d83fa5f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Decoder/JsonToFormDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ private function xWwwFormEncodedLike(&$data)
// Encode recursively
$this->xWwwFormEncodedLike($value);
} elseif (false === $value) {
// Checkbox-like behavior: remove false data
unset($data[$key]);
// Checkbox-like behavior removes false data but PATCH HTTP method with just checkboxes does not work
// To fix this issue we prefer transform false data to null
// See https://github.com/FriendsOfSymfony/FOSRestBundle/pull/883
$value = null;
} elseif (!is_string($value)) {
// Convert everything to string
// true values will be converted to '1', this is the default checkbox behavior
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/3-listener-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fos_rest:
Your custom decoder service must use a class that implements the
``FOS\RestBundle\Decoder\DecoderInterface``.
If you want to be able to use form with checkbox and have true and false value (without any issue) you have to use : fos_rest.decoder.jsontoform (available since fosrest 0.8.0)
If you want to be able to use form with checkbox and have true and false value (without any issue) you have to use: `fos_rest.decoder.jsontoform` (available since fosrest 0.8.0)

If the listener receives content that it tries to decode but the decode fails then a BadRequestHttpException will be thrown with the message:
``'Invalid ' . $format . ' message received'``. When combined with the [exception controller support](4-exception-controller-support.md) this means your API will provide useful error messages to your API users if they are making invalid requests.
Expand Down
8 changes: 3 additions & 5 deletions Tests/Decoder/JsonToFormDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
*/
class JsonToFormDecoderTest extends \PHPUnit_Framework_TestCase
{

public function testDecode()
public function testDecodeWithRemovingFalseData()
{
$data = array(
'arrayKey' => array(
Expand All @@ -39,13 +38,12 @@ public function testDecode()

$this->assertTrue(is_array($decoded));
$this->assertTrue(is_array($decoded['arrayKey']));
$this->assertArrayNotHasKey('falseKey', $decoded['arrayKey']);
$this->assertNull($decoded['arrayKey']['falseKey']);
$this->assertEquals('foo', $decoded['arrayKey']['stringKey']);
$this->assertArrayNotHasKey('falseKey', $decoded);
$this->assertNull($decoded['falseKey']);
$this->assertEquals('1', $decoded['trueKey']);
$this->assertEquals('69', $decoded['intKey']);
$this->assertEquals('3.14', $decoded['floatKey']);
$this->assertEquals('bar', $decoded['stringKey']);
}

}

0 comments on commit d83fa5f

Please sign in to comment.