Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scramble does not recognise if wrapper is already contained in resource. #403

Closed
mihob opened this issue May 29, 2024 · 3 comments · Fixed by #446
Closed

Scramble does not recognise if wrapper is already contained in resource. #403

mihob opened this issue May 29, 2024 · 3 comments · Fixed by #446

Comments

@mihob
Copy link

mihob commented May 29, 2024

If the toArray function of a resource already contains the wrapper (data), Laravel will not wrap the result again. Scramble does not take this into account.

@romalytvynenko
Copy link
Member

@mihob Please share the examples so I can reproduce. Thanks!

@mihob
Copy link
Author

mihob commented May 29, 2024

For example

class MyResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'foo' => $this->foo,
            'bar' => $this->bar,
            'data' => $this->something
        ];
    }
}

Laravel will not wrap the Result in data because it is already present in the returned array, the json response will look like this:

{
    'foo': '....',
    'bar': '....',
    'data': '....'
}

But scramble does not take this into account and the says the response will look like this:

{
    'data':  {
        'foo': '....',
        'bar': '....',
        'data': '....'
    }
}

Does that help you? @romalytvynenko

@mihob
Copy link
Author

mihob commented May 29, 2024

Here you can see how the decision is made as to whether the data is wrapped again.

vendor/laravel/framework/src/Illuminate/Http/Resources/Json/ResourceResponse.php

function wrap(...) and function haveDefaultWrapperAndDataIsUnwrapped(...)/function haveAdditionalInformationAndDataIsUnwrapped(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants