Skip to content

Commit 5788347

Browse files
authored
Merge pull request #21 from fetzi/bugfix/transform-null-handling
Add empty check for transform json data
2 parents afda5cd + a64cbff commit 5788347

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/JsonDecoder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public function decodesProtectedProperties()
8787

8888
private function transform($transformer, $jsonArrayData, $instance)
8989
{
90+
if (empty($jsonArrayData)) {
91+
return null;
92+
}
93+
9094
$classBindings = new ClassBindings($this);
9195
$transformer->register($classBindings);
9296

tests/specs/JsonDecoderSpec.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ public function it_should_be_able_to_transform_raw_with_private_properties()
103103
$response->getId()->shouldBe(1);
104104
$response->getName()->shouldReturn('John Doe');
105105
}
106+
107+
public function it_should_be_able_to_transform_null_values()
108+
{
109+
$this->register(new SampleTransformer());
110+
$response = $this->decode('null', JsonDecoderSample::class);
111+
112+
$response->shouldBe(null);
113+
}
106114
}
107115

108116
class JsonDecoderSample

0 commit comments

Comments
 (0)