rekalogika/mapper

Mapper casts nulllable property to string when not using constructor property promotion in target class

ToshY opened this issue · 1 comments

ToshY commented

Problem

I've noticed that nullable properties get cast to empty string ("") instead of being kept as null value when not using constructor property promotion in the target class.

Reproduction

src/Dto/BookDto.php

namespace App\Dto;

class BookDto
{
    public function __construct(
        public string|null $title = null,
    ) {
    }
}

src/Entity/Book.php

namespace App\Entity;

class Book
{
    private string|null $title = null;

    public function __construct(
        private int $id,
    ) {
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }
}

Run mapper

$book = new \App\Entity\Book(1);
$this->mapper->map(new \App\Dto\BookDto(null), $book);

Result

App\Entity\Book {#2485 ▼
  -id: 1
  -notes: ""
}

Good catch. Should be fixed in 9f905f6 . Thank you.