inertia-laravel icon indicating copy to clipboard operation
inertia-laravel copied to clipboard

Fix unexpected JSON resource serialization

Open TheDoctor0 opened this issue 2 years ago • 0 comments

I was in the process of porting the project to Inertia and I encountered a very unexpected issue with the JSON resources serialization. Some had a different structure in comparison to the one returned from API routes - data wrapper was present.

I tinkered with the source code and discovered that the issue lies in how resources are being serialized in the response https://github.com/inertiajs/inertia-laravel/blob/master/src/Response.php#L136.

I was a little surprised to find out that the FakeResource used for tests has a $wrap property set to null to mitigate this issue but that has not been documented anywhere. Without this property, the test for response with JSON resource fails.

To fix this unexpected behavior I changed the logic for resources serialization that will make sure to not use the data wrapper.

TheDoctor0 avatar Jul 19 '22 10:07 TheDoctor0