Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #178 from bastien-phi/nested_dto_having_casted_field
Browse files Browse the repository at this point in the history
Fix when nested DTO have casted field
  • Loading branch information
brendt committed Apr 8, 2021
2 parents 60f19dd + 80b8947 commit b22200a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `data-transfer-object` will be documented in this file

## 3.0.3 - 2021-05-08

- Fix when nested DTO have casted field (#178)

## 3.0.2 - 2021-04-02

- Allow valid DTOs to be passed to caster (#177)
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/DataTransferObjectProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private function resolveCasterFromType(): array
$reflectionClass = new ReflectionClass($type->getName());

do {
$attributes = $reflectionClass->getAttributes();
$attributes = $reflectionClass->getAttributes(CastWith::class);

$reflectionClass = $reflectionClass->getParentClass();
} while (! count($attributes) && $reflectionClass);
Expand All @@ -125,6 +125,6 @@ private function resolveCasterFromDefaults(): ?Caster
}
}

dd($defaultCastAttributes);
return null;
}
}
19 changes: 19 additions & 0 deletions tests/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Spatie\DataTransferObject\Tests\Dummy\BasicDto;
use Spatie\DataTransferObject\Tests\Dummy\ComplexDto;
use Spatie\DataTransferObject\Tests\Dummy\ComplexDtoWithCastedAttributeHavingCast;
use Spatie\DataTransferObject\Tests\Dummy\ComplexDtoWithNullableProperty;

class DataTransferObjectTest extends TestCase
Expand Down Expand Up @@ -62,6 +63,24 @@ public function create_with_null_nullable_nested_dto()
$this->assertNull($dto->other);
}

/** @test */
public function create_with_nested_dto_having_cast()
{
$dto = new ComplexDtoWithCastedAttributeHavingCast([
'name' => 'a',
'other' => [
'name' => 'b',
'object' => [
'name' => 'c',
],
],
]);

$this->assertEquals('a', $dto->name);
$this->assertEquals('b', $dto->other->name);
$this->assertEquals('c', $dto->other->object->name);
}

/** @test */
public function all_with_nested_dto()
{
Expand Down
14 changes: 14 additions & 0 deletions tests/Dummy/ComplexDtoWithCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Spatie\DataTransferObject\Tests\Dummy;

use Spatie\DataTransferObject\Attributes\DefaultCast;
use Spatie\DataTransferObject\DataTransferObject;

#[DefaultCast(ComplexObject::class, ComplexObjectCaster::class)]
class ComplexDtoWithCast extends DataTransferObject
{
public string $name;

public ComplexObject $object;
}
12 changes: 12 additions & 0 deletions tests/Dummy/ComplexDtoWithCastedAttributeHavingCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Spatie\DataTransferObject\Tests\Dummy;

use Spatie\DataTransferObject\DataTransferObject;

class ComplexDtoWithCastedAttributeHavingCast extends DataTransferObject
{
public string $name;

public ComplexDtoWithCast $other;
}

0 comments on commit b22200a

Please sign in to comment.