Skip to content

Commit

Permalink
Fixed bug of missing properties when transforming DTO (#738)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Nov 12, 2024
1 parent 16d1373 commit 87cdc9d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/validated-dto/src/SimpleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,14 @@ protected function buildDataForExport(): array
...$this->dtoMapTransform,
];

return $this->mapDTOData($mapping, $this->validatedData);
$data = $this->validatedData;
foreach ($this->getAcceptedProperties() as $property) {
if (! array_key_exists($property, $data) && isset($this->{$property})) {
$data[$property] = $this->{$property};
}
}

return $this->mapDTOData($mapping, $data);
}

protected function buildDataForValidation(array $data): array
Expand Down
25 changes: 22 additions & 3 deletions tests/ValidatedDTO/Unit/ValidatedDTOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ public function __construct(protected string $subject_name)

expect($validatedDTO)->toArray()
->toBe($dataStructure);

$validatedDTO->age = 20;
expect($validatedDTO)->toArray()
->toBe([...$dataStructure, 'age' => 20]);
});

it('validates that the ValidatedDTO can be converted into a JSON string', function () {
Expand All @@ -215,6 +219,10 @@ public function __construct(protected string $subject_name)

expect($validatedDTO)->toJson()
->toBe(json_encode($dataStructure));

$validatedDTO->age = 20;
expect($validatedDTO)->toJson()
->toBe(json_encode([...$dataStructure, 'age' => 20]));
});

it('validates that the ValidatedDTO can be converted into a pretty JSON string', function () {
Expand All @@ -223,6 +231,10 @@ public function __construct(protected string $subject_name)

expect($validatedDTO)->toPrettyJson()
->toBe(json_encode($dataStructure, JSON_PRETTY_PRINT));

$validatedDTO->age = 20;
expect($validatedDTO)->toPrettyJson()
->toBe(json_encode([...$dataStructure, 'age' => 20], JSON_PRETTY_PRINT));
});

it('validates that the ValidatedDTO with nested data can be converted into an array', function () {
Expand Down Expand Up @@ -352,15 +364,22 @@ public function __construct(protected string $subject_name)
$validatedDTO = new ValidatedDTOInstance(['name' => $this->subject_name]);

$model = new class extends Model {
protected array $fillable = ['name'];
protected array $fillable = ['name', 'age'];
};

$model_instance = $validatedDTO->toModel($model::class);
$modelInstance = $validatedDTO->toModel($model::class);

expect($model_instance)
expect($modelInstance)
->toBeInstanceOf(Model::class)
->toArray()
->toBe(['name' => $this->subject_name]);

$validatedDTO->age = 20;
$modelInstance = $validatedDTO->toModel($model::class);
expect($modelInstance)
->toBeInstanceOf(Model::class)
->toArray()
->toBe(['name' => $this->subject_name, 'age' => 20]);
});

it('maps data before validation', function () {
Expand Down

0 comments on commit 87cdc9d

Please sign in to comment.