Skip to content

Commit 909c67b

Browse files
committed
Transform false data to null in all cases
1 parent 547e842 commit 909c67b

File tree

4 files changed

+6
-56
lines changed

4 files changed

+6
-56
lines changed

Decoder/JsonToFormDecoder.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@
1818
*/
1919
class JsonToFormDecoder implements DecoderInterface
2020
{
21-
/**
22-
* @var bool
23-
*/
24-
private $removeFalseData;
25-
26-
/**
27-
* @param bool $removeFalseData
28-
*/
29-
public function __construct($removeFalseData = true)
30-
{
31-
$this->removeFalseData = $removeFalseData;
32-
}
33-
3421
/**
3522
* Makes data decoded from JSON application/x-www-form-encoded compliant
3623
*
@@ -43,12 +30,10 @@ private function xWwwFormEncodedLike(&$data)
4330
// Encode recursively
4431
$this->xWwwFormEncodedLike($value);
4532
} elseif (false === $value) {
46-
if ($this->removeFalseData) {
47-
// Checkbox-like behavior: remove false data
48-
unset($data[$key]);
49-
} else {
50-
$value = null;
51-
}
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;
5237
} elseif (!is_string($value)) {
5338
// Convert everything to string
5439
// true values will be converted to '1', this is the default checkbox behavior

Resources/config/body_listener.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<parameter key="fos_rest.normalizer.camel_keys.class">FOS\RestBundle\Normalizer\CamelKeysNormalizer</parameter>
1010
<parameter key="fos_rest.decoder.json.class">FOS\RestBundle\Decoder\JsonDecoder</parameter>
1111
<parameter key="fos_rest.decoder.jsontoform.class">FOS\RestBundle\Decoder\JsonToFormDecoder</parameter>
12-
<parameter key="fos_rest.decoder.jsontoform.remove_false_data">true</parameter>
1312
<parameter key="fos_rest.decoder.xml.class">FOS\RestBundle\Decoder\XmlDecoder</parameter>
1413
<parameter key="fos_rest.decoder_provider.class">FOS\RestBundle\Decoder\ContainerDecoderProvider</parameter>
1514
<parameter key="fos_rest.body_listener.class">FOS\RestBundle\EventListener\BodyListener</parameter>
@@ -22,9 +21,7 @@
2221

2322
<service id="fos_rest.decoder.json" class="%fos_rest.decoder.json.class%" />
2423

25-
<service id="fos_rest.decoder.jsontoform" class="%fos_rest.decoder.jsontoform.class%">
26-
<argument>%fos_rest.decoder.jsontoform.remove_false_data%</argument>
27-
</service>
24+
<service id="fos_rest.decoder.jsontoform" class="%fos_rest.decoder.jsontoform.class%" />
2825

2926
<service id="fos_rest.decoder.xml" class="%fos_rest.decoder.xml.class%" />
3027

Resources/doc/3-listener-support.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,8 @@ 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 (more than just one 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

206-
`fos_rest.decoder.jsontoform` removes false values. In the case you have a form with just one checkbox or need to patch one checkbox it does not work. Instead of remove false value you can transform it to null. For this you need to override a parameter:
207-
```yaml
208-
parameters:
209-
fos_rest.decoder.jsontoform.remove_false_data: false
210-
```
211206
If the listener receives content that it tries to decode but the decode fails then a BadRequestHttpException will be thrown with the message:
212207
``'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.
213208

Tests/Decoder/JsonToFormDecoderTest.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,6 @@ public function testDecodeWithRemovingFalseData()
3636
$decoder = new JsonToFormDecoder();
3737
$decoded = $decoder->decode(json_encode($data));
3838

39-
$this->assertTrue(is_array($decoded));
40-
$this->assertTrue(is_array($decoded['arrayKey']));
41-
$this->assertArrayNotHasKey('falseKey', $decoded['arrayKey']);
42-
$this->assertEquals('foo', $decoded['arrayKey']['stringKey']);
43-
$this->assertArrayNotHasKey('falseKey', $decoded);
44-
$this->assertEquals('1', $decoded['trueKey']);
45-
$this->assertEquals('69', $decoded['intKey']);
46-
$this->assertEquals('3.14', $decoded['floatKey']);
47-
$this->assertEquals('bar', $decoded['stringKey']);
48-
}
49-
50-
public function testDecodeWithoutRemovingFalseData()
51-
{
52-
$data = array(
53-
'arrayKey' => array(
54-
'falseKey' => false,
55-
'stringKey' => 'foo',
56-
),
57-
'falseKey' => false,
58-
'trueKey' => true,
59-
'intKey' => 69,
60-
'floatKey' => 3.14,
61-
'stringKey' => 'bar',
62-
);
63-
$decoder = new JsonToFormDecoder(false);
64-
$decoded = $decoder->decode(json_encode($data));
65-
6639
$this->assertTrue(is_array($decoded));
6740
$this->assertTrue(is_array($decoded['arrayKey']));
6841
$this->assertNull($decoded['arrayKey']['falseKey']);

0 commit comments

Comments
 (0)