Skip to content

Commit d83fa5f

Browse files
committed
Merge pull request #883 from coopTilleuls/jsontoform-remove-false-data
Enable or disable removing false data for JsonToFormDecoder
2 parents 41ebd9c + 909c67b commit d83fa5f

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

Decoder/JsonToFormDecoder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ private function xWwwFormEncodedLike(&$data)
3030
// Encode recursively
3131
$this->xWwwFormEncodedLike($value);
3232
} elseif (false === $value) {
33-
// Checkbox-like behavior: remove false data
34-
unset($data[$key]);
33+
// Checkbox-like behavior removes false data but PATCH HTTP method with just checkboxes does not work
34+
// To fix this issue we prefer transform false data to null
35+
// See https://github.com/FriendsOfSymfony/FOSRestBundle/pull/883
36+
$value = null;
3537
} elseif (!is_string($value)) {
3638
// Convert everything to string
3739
// true values will be converted to '1', this is the default checkbox behavior

Resources/doc/3-listener-support.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fos_rest:
201201
Your custom decoder service must use a class that implements the
202202
``FOS\RestBundle\Decoder\DecoderInterface``.
203203
204-
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)
204+
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)
205205

206206
If the listener receives content that it tries to decode but the decode fails then a BadRequestHttpException will be thrown with the message:
207207
``'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.

Tests/Decoder/JsonToFormDecoderTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
*/
2121
class JsonToFormDecoderTest extends \PHPUnit_Framework_TestCase
2222
{
23-
24-
public function testDecode()
23+
public function testDecodeWithRemovingFalseData()
2524
{
2625
$data = array(
2726
'arrayKey' => array(
@@ -39,13 +38,12 @@ public function testDecode()
3938

4039
$this->assertTrue(is_array($decoded));
4140
$this->assertTrue(is_array($decoded['arrayKey']));
42-
$this->assertArrayNotHasKey('falseKey', $decoded['arrayKey']);
41+
$this->assertNull($decoded['arrayKey']['falseKey']);
4342
$this->assertEquals('foo', $decoded['arrayKey']['stringKey']);
44-
$this->assertArrayNotHasKey('falseKey', $decoded);
43+
$this->assertNull($decoded['falseKey']);
4544
$this->assertEquals('1', $decoded['trueKey']);
4645
$this->assertEquals('69', $decoded['intKey']);
4746
$this->assertEquals('3.14', $decoded['floatKey']);
4847
$this->assertEquals('bar', $decoded['stringKey']);
4948
}
50-
5149
}

0 commit comments

Comments
 (0)