diff --git a/Decoder/JsonToFormDecoder.php b/Decoder/JsonToFormDecoder.php index ebe8a9058..c82d61c53 100644 --- a/Decoder/JsonToFormDecoder.php +++ b/Decoder/JsonToFormDecoder.php @@ -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 diff --git a/Resources/doc/3-listener-support.md b/Resources/doc/3-listener-support.md index e5fe86f33..f42222bf6 100644 --- a/Resources/doc/3-listener-support.md +++ b/Resources/doc/3-listener-support.md @@ -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. diff --git a/Tests/Decoder/JsonToFormDecoderTest.php b/Tests/Decoder/JsonToFormDecoderTest.php index 3797c1013..374e30372 100644 --- a/Tests/Decoder/JsonToFormDecoderTest.php +++ b/Tests/Decoder/JsonToFormDecoderTest.php @@ -20,8 +20,7 @@ */ class JsonToFormDecoderTest extends \PHPUnit_Framework_TestCase { - - public function testDecode() + public function testDecodeWithRemovingFalseData() { $data = array( 'arrayKey' => array( @@ -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']); } - }