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

[8.x] fix JsonResponse::fromJasonString() double encoding string #37076

Merged
merged 1 commit into from Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Illuminate/Http/JsonResponse.php
Expand Up @@ -22,13 +22,14 @@ class JsonResponse extends BaseJsonResponse
* @param int $status
* @param array $headers
* @param int $options
* @param bool $json
* @return void
*/
public function __construct($data = null, $status = 200, $headers = [], $options = 0)
public function __construct($data = null, $status = 200, $headers = [], $options = 0, $json = false)
{
$this->encodingOptions = $options;

parent::__construct($data, $status, $headers);
parent::__construct($data, $status, $headers, $json);
}

/**
Expand Down Expand Up @@ -118,4 +119,12 @@ public function hasEncodingOption($option)
{
return (bool) ($this->encodingOptions & $option);
}

/**
* {@inheritdoc}
*/
public static function fromJsonString(?string $data = null, int $status = 200, array $headers = [])
{
return new static($data, $status, $headers, 0, true);
}
}
8 changes: 8 additions & 0 deletions tests/Http/HttpJsonResponseTest.php
Expand Up @@ -104,6 +104,14 @@ public function jsonErrorDataProvider()
[$nan],
];
}

public function testFromJsonString()
{
$json_string = '{"foo":"bar"}';
$response = JsonResponse::fromJsonString($json_string);

$this->assertSame('bar', $response->getData()->foo);
}
}

class JsonResponseTestJsonableObject implements Jsonable
Expand Down