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

[9.x] Do not transform JsonSerializable instances to array in HTTP client methods #41077

Merged
merged 1 commit into from
Feb 17, 2022
Merged

[9.x] Do not transform JsonSerializable instances to array in HTTP client methods #41077

merged 1 commit into from
Feb 17, 2022

Conversation

JaZo
Copy link
Contributor

@JaZo JaZo commented Feb 17, 2022

This is a follow-up of #41055.

Problem

When a class implements both Arrayable and JsonSerializable, the former currently takes precedence. However, a class can have different forms for Arrayable and JsonSerializable. For example, an empty hashmap should be an empty array in array form, but an empty object in JSON form i.e. {"attributes":{}}. This is especially important if the endpoint always expects an object, even if it is empty. So I'd expect that if I post an object as JSON, and setup the correct jsonSerialize method, it'll be serialized as such, not transformed to an array.

Solution

This PR will skip transforming the request data, only in JSON body format, if it implements JsonSerializable and thus passes it 1:1 to Guzzle for JSON serialization.

Change

Given the following class:

class Foo implements JsonSerializable, Arrayable
{
    public function jsonSerialize(): mixed
    {
        return [
            'attributes' => (object) [],
        ];
    }

    public function toArray(): array
    {
        return [
            'attributes' => [],
        ];
    }
}

Before

Http::post('http://example.com', new Foo); // posts {"attributes": []}

After

Http::post('http://example.com', new Foo); // posts {"attributes": {}}

@taylorotwell taylorotwell merged commit ffe4d6d into laravel:9.x Feb 17, 2022
@JaZo JaZo deleted the bugfix/prefer-json-serializable-over-arrayable branch February 21, 2022 12:16
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 this pull request may close these issues.

2 participants