From b97c5ac7dca70ada644e11294da0e38ba01a4418 Mon Sep 17 00:00:00 2001 From: neomerx Date: Sat, 2 Mar 2019 19:01:31 +0300 Subject: [PATCH] Close #230 --- src/Representation/DocumentWriter.php | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Representation/DocumentWriter.php b/src/Representation/DocumentWriter.php index f15d00e2..e2f5c292 100644 --- a/src/Representation/DocumentWriter.php +++ b/src/Representation/DocumentWriter.php @@ -266,21 +266,42 @@ protected function getResourceRepresentation(ResourceInterface $resource, FieldS $attributes = $this->getAttributesRepresentation($filter->getAttributes($resource)); if (empty($attributes) === false) { + \assert( + \json_encode($attributes) !== false, + 'Attributes for resource type `' . $resource->getType() . + '` cannot be converted into JSON. Please check its Schema returns valid data.' + ); $representation[DocumentInterface::KEYWORD_ATTRIBUTES] = $attributes; } $relationships = $this->getRelationshipsRepresentation($filter->getRelationships($resource)); if (empty($relationships) === false) { + \assert( + \json_encode($relationships) !== false, + 'Relationships for resource type `' . $resource->getType() . + '` cannot be converted into JSON. Please check its Schema returns valid data.' + ); $representation[DocumentInterface::KEYWORD_RELATIONSHIPS] = $relationships; } if ($resource->hasLinks() === true) { - $representation[DocumentInterface::KEYWORD_LINKS] = - $this->getLinksRepresentation($this->getUrlPrefix(), $resource->getLinks()); + $links = $this->getLinksRepresentation($this->getUrlPrefix(), $resource->getLinks()); + \assert( + \json_encode($links) !== false, + 'Links for resource type `' . $resource->getType() . + '` cannot be converted into JSON. Please check its Schema returns valid data.' + ); + $representation[DocumentInterface::KEYWORD_LINKS] = $links; } if ($resource->hasResourceMeta() === true) { - $representation[DocumentInterface::KEYWORD_META] = $resource->getResourceMeta(); + $meta = $resource->getResourceMeta(); + \assert( + \json_encode($meta) !== false, + 'Meta for resource type `' . $resource->getType() . + '` cannot be converted into JSON. Please check its Schema returns valid data.' + ); + $representation[DocumentInterface::KEYWORD_META] = $meta; } return $representation;