Browse Source

Merge pull request #6945 from localheinz/fix/remove-main-key

Fix: Remove key when value is null
Jordi Boggiano 7 years ago
parent
commit
ad27182618

+ 1 - 1
src/Composer/Json/JsonManipulator.php

@@ -417,7 +417,7 @@ class JsonManipulator
     {
         $decoded = JsonFile::parseJson($this->contents);
 
-        if (!isset($decoded[$key])) {
+        if (!array_key_exists($key, $decoded)) {
             return true;
         }
 

+ 16 - 0
tests/Composer/Test/Json/JsonManipulatorTest.php

@@ -2310,6 +2310,22 @@ class JsonManipulatorTest extends TestCase
 ', $manipulator->getContents());
     }
 
+    public function testRemoveMainKeyRemovesKeyWhereValueIsNull()
+    {
+        $manipulator = new JsonManipulator(json_encode(array(
+            'foo' => 9000,
+            'bar' => null,
+        )));
+
+        $manipulator->removeMainKey('bar');
+
+        $expected = json_encode(array(
+            'foo' => 9000,
+        ));
+
+        $this->assertJsonStringEqualsJsonString($expected, $manipulator->getContents());
+    }
+
     public function testIndentDetection()
     {
         $manipulator = new JsonManipulator('{