Skip to content

Commit

Permalink
Merge pull request #651 from vincequeiroz/master
Browse files Browse the repository at this point in the history
Check nullable fields on handleTypeConversions method
  • Loading branch information
TomHAnderson authored Oct 8, 2018
2 parents a7e0f31 + 1fadc0a commit 57cf4bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ public function hydrateValue($name, $value, $data = null)
{
$value = parent::hydrateValue($name, $value, $data);

if (is_null($value) && method_exists($this->metadata, 'isNullable') && $this->metadata->isNullable($name)) {
return null;
}

return $this->handleTypeConversions($value, $this->metadata->getTypeOfField($name));
}

Expand Down Expand Up @@ -535,6 +539,10 @@ function ($item) {
*/
protected function handleTypeConversions($value, $typeOfField)
{
if (is_null($value)) {
return null;
}

switch ($typeOfField) {
case 'boolean':
$value = (bool)$value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,26 @@ public function testHandleTypeConversionsDecimal()
$this->assertTrue(is_string($entity->getGenericField()));
$this->assertEquals('12345', $entity->getGenericField());
}

public function testHandleTypeConversionsNullable()
{
// When using hydration by value, it will use the public API of the entity to set values (setters)
$this->configureObjectManagerForSimpleEntityWithGenericField(null);

$entity = new Asset\SimpleEntityWithGenericField();
$data = ['genericField' => null];

$entity = $this->hydratorByValue->hydrate($data, $entity);

$this->assertTrue(is_null($entity->getGenericField()));
$this->assertEquals(null, $entity->getGenericField());

$entity = new Asset\SimpleEntityWithGenericField();
$data = ['genericField' => null];

$entity = $this->hydratorByReference->hydrate($data, $entity);

$this->assertTrue(is_null($entity->getGenericField()));
$this->assertEquals(null, $entity->getGenericField());
}
}

0 comments on commit 57cf4bb

Please sign in to comment.