Skip to content
Snippets Groups Projects
Commit 93af3e5f authored by Alexey Korepov's avatar Alexey Korepov Committed by Thao Huynh Khac
Browse files

Issue #3374410: Pass the whole response to JsonapiParseInterface::parse() instead of only content

parent 7a2bb6e7
No related branches found
No related tags found
1 merge request!3Issue #3374410: Pass the whole response to JsonapiParseInterface::parse() instead of only content
......@@ -101,8 +101,7 @@ class ResponseSubscriber implements EventSubscriberInterface {
if ($need_parse) {
$content = $response->getContent();
if (strpos($content, '{"jsonapi"') === 0) {
$content = $this->jsonapiInclude->parse($content);
$event->getResponse()->setContent($content);
$this->jsonapiInclude->parse($response);
}
}
}
......
......@@ -4,6 +4,7 @@ namespace Drupal\jsonapi_include;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray;
use Symfony\Component\HttpFoundation\Response;
/**
* The class for parse Jsonapi content.
......@@ -20,17 +21,17 @@ class JsonapiParse implements JsonapiParseInterface {
protected $included;
/**
* Parse json api.
*
* @param string|object $response
* The response data from jsonapi.
*
* @return mixed
* Parse jsonapi data.
* {@inheritdoc}
*/
public function parse($response) {
$content = $this->parseJsonContent($response);
return is_string($content) ? $content : Json::encode($content);
// @todo Remove in the release 2.0 with removed the deprecated srings input.
if (!$response instanceof Response) {
@trigger_error('Parsing strings is deprecated deprecated in jsonapi_include:8.x-1.7 and is removed from jsonapi_include:8.x-2.0. Pass the full Response object instead. See https://www.drupal.org/project/jsonapi_include/issues/3374410', E_USER_DEPRECATED);
$content = $this->parseJsonContent($response);
return Json::encode($content);
}
$this->parseJsonContent($response);
return $response;
}
/**
......@@ -202,46 +203,59 @@ class JsonapiParse implements JsonapiParseInterface {
}
/**
* Parse json content.
* Integrates includes into the content of a Response.
*
* @param object $response
* The jsonapi object.
* @param \Symfony\Component\HttpFoundation\Response|string|array $response
* A Response object or string/array with a response content.
*
* @return mixed
* Parse result.
* @return \Symfony\Component\HttpFoundation\Response|array
* Returns
* Returns an array with the response content, if the input is string or
* array, or void if input is a Response.
*/
protected function parseJsonContent($response) {
if (is_string($response)) {
$json = Json::decode($response);
if ($response instanceof Response) {
$content = $response->getContent();
if (is_string($content)) {
$content = Json::decode($content);
}
}
elseif (is_object($response)) {
$json = $response;
// @todo Remove in the release 2.0 with removed the deprecated srings input.
elseif (is_array($response)) {
$content = $response;
}
else {
return $response;
elseif (is_string($response)) {
$content = Json::decode($response);
}
if (NestedArray::getValue($json, ['jsonapi', 'parsed'])) {
return $json;
if (NestedArray::getValue($content, ['jsonapi', 'parsed'])) {
return $response;
}
if (isset($json['errors']) || empty($json['data'])) {
return $json;
if (isset($content['errors']) || empty($content['data'])) {
return $response;
}
$this->included = $this->groupIncludes($json);
$this->included = $this->groupIncludes($content);
$data = [];
if (!$this->isAssoc($json['data'])) {
foreach ($json['data'] as $item) {
if (!$this->isAssoc($content['data'])) {
foreach ($content['data'] as $item) {
$data[] = $this->parseResource($item);
}
}
else {
$data = $this->parseResource($json['data']);
$data = $this->parseResource($content['data']);
}
if (isset($content['included'])) {
unset($content['included']);
}
if (isset($json['included'])) {
unset($json['included']);
$content['jsonapi']['parsed'] = TRUE;
$content['data'] = $data;
if ($response instanceof Response) {
$response->setContent(Json::encode($content));
return $response;
}
// @todo Remove in the release 2.0 with removed the deprecated srings input.
else {
return $content;
}
$json['jsonapi']['parsed'] = TRUE;
$json['data'] = $data;
return $json;
}
}
......@@ -3,20 +3,25 @@
namespace Drupal\jsonapi_include;
/**
* Interface for parse Jsonapi content.
* The interface for parsing the JSON:API Response.
*
* @package Drupal\jsonapi_include
*/
interface JsonapiParseInterface {
/**
* Parse json api.
* Parses the JSON:API Response with integrating includes inside the fields.
*
* @param string|object $response
* The response data from jsonapi.
* @param \Symfony\Component\HttpFoundation\Response|string $response
* A Response object with JSON:API response.
* Or string with a Response body (deprecated).
*
* @return mixed
* Parse jsonapi data.
* @return \Symfony\Component\HttpFoundation\Response|string
* Returns the Response object, if the input is a Response object.
* Or return a string, if the input is in the string format (deprecated).
*
* @todo With 2.0 release remove the string format and explicitly set the
* Response as the argument and the return value.
*/
public function parse($response);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment