Skip to content

Commit

Permalink
Merge pull request #39 from bookboon/feature/no-interfere-with-other-…
Browse files Browse the repository at this point in the history
…normalizers

feat: json LD normalizer will now not interfere with operation of oth…
  • Loading branch information
lkm authored Jul 15, 2022
2 parents ea3659f + c9be1c7 commit 73d99ae
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
3 changes: 1 addition & 2 deletions Client/JsonLDClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ protected function makeRequest(
$errorResponse = $this->deserialize(
$response->getBody()->getContents(),
null,
ApiErrorResponse::class,
'json'
ApiErrorResponse::class
);
} catch (JsonLDSerializationException $e2) {
}
Expand Down
8 changes: 8 additions & 0 deletions Serializer/JsonLDMapNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ public function normalize($object, $format = null, array $context = [])
*/
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
{
if (!$this->isCorrectFormat($format, $data)) {
return false;
}

return $this->isDataArray($data);
}

public function supportsNormalization($data, $format = null, array $context = []) : bool
{
if (!$this->isCorrectFormat($format, $data)) {
return false;
}

return $data instanceof ArrayObject;
}

Expand Down
14 changes: 13 additions & 1 deletion Serializer/JsonLDNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ public function normalize($object, $format = null, array $context = [])

public function supportsNormalization($data, $format = null, array $context = []) : bool
{
if (!$this->isCorrectFormat($format, $data)) {
return false;
}

return (
(is_object($data) && $format === JsonLDEncoder::FORMAT)
is_object($data)
|| $this->isDataArray($data)
)
&& !($data instanceof DateTime)
Expand All @@ -66,6 +70,10 @@ public function supportsNormalization($data, $format = null, array $context = []

public function supportsDenormalization($data, $type, $format = null, array $context = []) : bool
{
if (!$this->isCorrectFormat($format, $data)) {
return false;
}

return isset($data['@type']) ||
isset($data[0]['@type']) ||
in_array($type, [ApiErrorResponse::class, ApiError::class. '[]'], true) ||
Expand Down Expand Up @@ -177,4 +185,8 @@ protected function isDataArray($data) : bool
{
return is_array($data) && (count($data) === 0 || (array_keys($data) === range(0, count($data) - 1)));
}

protected function isCorrectFormat(mixed $format, mixed $data): bool {
return $format === JsonLDEncoder::FORMAT || ($this->isDataArray($data) && $format === null);
}
}
24 changes: 24 additions & 0 deletions Tests/Serializer/JsonLDMapNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ public function testSupportsMethodReturnsFalseWhenDataIsNotArray(): void
$this->assertFalse($jsonLDMapNormalizer->supportsDenormalization(new DateTime(), ''), "types that are not arrays should return false");
}

public function testSupportsMethodReturnsFalseWhenFormatIsNotJsonLD(): void
{
$jsonLDMapNormalizer = new JsonLDMapNormalizer(new ObjectNormalizer(), new MappingCollection([], []));

$this->assertFalse($jsonLDMapNormalizer->supportsDenormalization([], '', 'json'), "if format is json should return false");
$this->assertFalse($jsonLDMapNormalizer->supportsNormalization(new ArrayObject(), 'json'), "if format is json should return false");
}

public function testSupportsMethodReturnsTrueWhenFormatIsNull(): void
{
$jsonLDMapNormalizer = new JsonLDMapNormalizer(new ObjectNormalizer(), new MappingCollection([], []));

$this->assertTrue($jsonLDMapNormalizer->supportsDenormalization([], ''), "if format is null should support denormalisation");
$this->assertFalse($jsonLDMapNormalizer->supportsNormalization(new ArrayObject()), "if format is null should not support normalisation of object");
}

public function testSupportsMethodReturnsTrueWhenFormatIsJsonLD(): void
{
$jsonLDMapNormalizer = new JsonLDMapNormalizer(new ObjectNormalizer(), new MappingCollection([], []));

$this->assertTrue($jsonLDMapNormalizer->supportsDenormalization([], '', 'json-ld'), "if format is json-ld should support denormalisation");
$this->assertTrue($jsonLDMapNormalizer->supportsNormalization(new ArrayObject(), 'json-ld'), "if format is json-ld should support normalisation");
}

public function testSupportsMethodReturnsTrueWhenMapValuesHaveTypePropertyDefined(): void
{
$jsonLDMapNormalizer = new JsonLDMapNormalizer(new ObjectNormalizer(), new MappingCollection([], []));
Expand Down
37 changes: 36 additions & 1 deletion Tests/Serializer/JsonLDNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Bookboon\JsonLDClient\Client\JsonLDException;
use Bookboon\JsonLDClient\Client\JsonLDSerializationException;
use Bookboon\JsonLDClient\Mapping\MappingApi;
use Bookboon\JsonLDClient\Mapping\MappingCollection;
use Bookboon\JsonLDClient\Mapping\MappingEndpoint;
use Bookboon\JsonLDClient\Serializer\JsonLDEncoder;
use Bookboon\JsonLDClient\Serializer\JsonLDMapNormalizer;
use Bookboon\JsonLDClient\Serializer\JsonLDNormalizer;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\ChildClass;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\CircularChild;
Expand All @@ -26,6 +28,7 @@
use DateTime;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface;

class JsonLDNormalizerTest extends TestCase
Expand Down Expand Up @@ -542,14 +545,46 @@ public function testPrefersClassInOwnNamespaceRatherThanMapping(): void
$this->assertEquals('childclass', $childClass->getTitle());
}

public function testSupportsMethodReturnsTrueWhenFormatIsNull(): void
{
$jsonLDNormalizer = new JsonLDNormalizer(new ObjectNormalizer(), new MappingCollection([], []));

$this->assertTrue($jsonLDNormalizer->supportsDenormalization([
['@type' => 'SomeClass']
], ''), "if format is null should not support denormalisation of array");
$this->assertFalse($jsonLDNormalizer->supportsDenormalization(
['@type' => 'SomeClass']
, ''), "if format is null should not support denormalisation of object");
$this->assertTrue($jsonLDNormalizer->supportsNormalization([]),
"if format is null should support normalisation of array");
$this->assertFalse($jsonLDNormalizer->supportsNormalization(new ArrayObject()),
"if format is null should not support normalisation of object");
}

public function testSupportsMethodReturnsTrueWhenFormatIsJsonLd(): void
{
$jsonLDNormalizer = new JsonLDNormalizer(new ObjectNormalizer(), new MappingCollection([], []));

$this->assertTrue($jsonLDNormalizer->supportsDenormalization([
['@type' => 'SomeClass']
], '', 'json-ld'), "if format is json-ld should support denormalisation");
$this->assertTrue($jsonLDNormalizer->supportsDenormalization(
['@type' => 'SomeClass'], '', 'json-ld'
), "if format is json-ld should support denormalisation");
$this->assertTrue($jsonLDNormalizer->supportsNormalization(new SimpleClass(), 'json-ld'),
"if format is json-ld should support normalisation of object");
$this->assertTrue($jsonLDNormalizer->supportsNormalization([], 'json-ld'),
"if format is json-ld should support normalisation of array");
}

private function getContextWithMapping(string $className): array
{
return [
JsonLDNormalizer::MAPPPING_KEY => new MappingEndpoint($className, 'http://localhost/blah')
];
}

private function getSerializerHelper() : SerializerInterface
private function getSerializerHelper(): SerializerInterface
{
return SerializerHelper::create(
[],
Expand Down

0 comments on commit 73d99ae

Please sign in to comment.