spatie / laravel-data

Powerful data objects for Laravel
https://spatie.be/docs/laravel-data/
MIT License
1.28k stars 209 forks source link

Not wrapping additional properties #893

Open ennorehling opened 5 days ago

ennorehling commented 5 days ago

✏️ Describe the bug When adding additional properties to a Data Object, they do not get wrapped by wrap().

↪️ To Reproduce Provide us a pest test like this one which shows the problem:


it('cannot wrap additional properties', function () {
    class UserData extends Data
    {
        public function __construct(
            public string $name,
        ) {
        }
    }

    dd(UserData::from(['name' => 'Ruben'])->additional(['role' => 'admin'])->wrap('data'));
});

Assertions aren't required, a simple dump or dd statement of what's going wrong is good enough 😄

✅ Expected behavior I'm expecting this to produce

[
    "data" => [
      "name" => "Ruben",
      "rank" => "admin",
    ],
  ]

but instead, I get [ "data" => [ "name" => "Ruben", ], "rank" => "admin", ]

A similar thing happens when I add a with() method to the object.

🖥️ Versions

Laravel: v10.48.22 Laravel Data: 4.11.0 PHP: 8.2.20