Skip to content

Commit

Permalink
fix: Fix the null value should be removed only if set as default on t…
Browse files Browse the repository at this point in the history
…yped properties (#FRAM-72) (#11)
  • Loading branch information
Johnmeurt committed Oct 26, 2022
1 parent 09feb5e commit 99f202c
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 13 deletions.
8 changes: 2 additions & 6 deletions src/Context/NormalizationContext.php
Expand Up @@ -199,12 +199,8 @@ public function skipPropertyValue(PropertyMetadata $property, $value): bool
return !$this->shouldAddNull();
}

// This does not remove the 'null' default value.
if ($this->removeDefaultValue()) {
return $property->defaultValue === $value;
}

return false;
// This does not remove the 'null' default value for non typed properties.
return $this->removeDefaultValue() && $property->isDefaultValue($value);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Builder/PropertyMetadataBuilder.php
Expand Up @@ -149,7 +149,7 @@ public function build(): PropertyMetadata
$property->setDenormalizationOptions($this->denormalizationOptions);

$defaultValues = $this->reflection->getDefaultProperties();
if (isset($defaultValues[$this->name])) {
if (array_key_exists($this->name, $defaultValues)) {
$property->setDefaultValue($defaultValues[$this->name]);
}

Expand Down
18 changes: 18 additions & 0 deletions src/Metadata/PropertyMetadata.php
Expand Up @@ -91,6 +91,13 @@ class PropertyMetadata
*/
public $defaultValue;

/**
* A flag to know if the property has a default value.
*
* @var bool
*/
private $hasDefaultValue = false;

/**
* Flag to know if the property used php type.
*
Expand Down Expand Up @@ -356,6 +363,7 @@ public function inline(): bool
public function setDefaultValue($defaultValue): void
{
$this->defaultValue = $defaultValue;
$this->hasDefaultValue = true;
}

/**
Expand All @@ -368,6 +376,16 @@ public function defaultValue()
return $this->defaultValue;
}

/**
* Check whether the value is the default value of the property.
*
* @param mixed $value
*/
public function isDefaultValue($value): bool
{
return $this->hasDefaultValue && $this->defaultValue === $value;
}

/**
* Set the options for normalization context
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Test/Context/NormalizationContextTest.php
Expand Up @@ -318,7 +318,7 @@ public function test_release_ref()
public function test_default_value()
{
$property = $this->property();
$property->defaultValue = 1;
$property->setDefaultValue(1);

$context = $this->context();
$this->assertFalse($context->skipPropertyValue($property, 0));
Expand Down
12 changes: 12 additions & 0 deletions tests/TestPhp72/Fixtures/entities.php
Expand Up @@ -3,6 +3,18 @@
namespace Bdf\Serializer\TestPhp72;


class Bar
{
public $id;
public $label;
}

class BarWithDefaultValue
{
public $id;
public $label = null;
}

class UserNonTyped
{
private $id;
Expand Down
16 changes: 16 additions & 0 deletions tests/TestPhp72/SerializerOptionsTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Bdf\Serializer\TestPhp72;

use Bdf\Serializer\Context\NormalizationContext;
use Bdf\Serializer\SerializerBuilder;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -39,4 +40,19 @@ public function test_serialize_with_null_value()
$data = ['id' => 1];
$this->assertEquals($data, $result);
}

/**
*
*/
public function test_remove_default_null_value()
{
$serializer = SerializerBuilder::create()->build();

$object = new Bar();
$object->label = null;
$this->assertEquals([], $serializer->toArray($object, [NormalizationContext::REMOVE_DEFAULT_VALUE => true]));

$object = new BarWithDefaultValue();
$this->assertEquals([], $serializer->toArray($object, [NormalizationContext::REMOVE_DEFAULT_VALUE => true]));
}
}
8 changes: 7 additions & 1 deletion tests/TestPhp74/Fixtures/entities.php
Expand Up @@ -61,5 +61,11 @@ class Foo
class Bar
{
public int $id;
public string $label;
public ?string $label;
}

class BarWithDefaultValue
{
public int $id;
public ?string $label = null;
}
22 changes: 18 additions & 4 deletions tests/TestPhp74/SerializerTest.php
Expand Up @@ -33,11 +33,25 @@ public function test_normalize_add_null_value_for_typed_properties()
'age' => null,
];

$result = $serializer->toArray($object, ['null' => true]);
$this->assertEquals($data, $result);
$this->assertEquals($data, $serializer->toArray($object));
$this->assertEquals($data, $serializer->toArray($object, [NormalizationContext::NULL => true]));
}

/**
*
*/
public function test_remove_default_value_when_equals_to_null()
{
$serializer = SerializerBuilder::create()->build();

$object = new Bar();
$object->label = null;
$this->assertEquals(['label' => null], $serializer->toArray($object, [NormalizationContext::REMOVE_DEFAULT_VALUE => true]));

$result = $serializer->toArray($object);
$this->assertEquals($data, $result);
$object = new BarWithDefaultValue();
$object->label = null;
$this->assertEquals(['label' => null], $serializer->toArray($object));
$this->assertEquals([], $serializer->toArray($object, [NormalizationContext::REMOVE_DEFAULT_VALUE => true]));
}

/**
Expand Down

0 comments on commit 99f202c

Please sign in to comment.