Skip to content

Commit

Permalink
Merge pull request #1 from bookboon/hotfix/fallback-extractor
Browse files Browse the repository at this point in the history
Handle entities without docblocks by falling back to reflection
  • Loading branch information
lkm authored Jul 22, 2020
2 parents d4a25c2 + 4d3df44 commit 20e301c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
17 changes: 15 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@ services:
jsonldclient.client:
class: GuzzleHttp\Client

jsonldclient.property_extractor:
class: Symfony\Component\PropertyInfo\PropertyInfoExtractor
arguments:
- ['@property_info.php_doc_extractor', '@property_info.reflection_extractor']
- ['@property_info.php_doc_extractor', '@property_info.reflection_extractor']
- []
- []
- []

jsonldclient.property_accessor:
alias: 'property_accessor'
public: true

jsonldclient.object_normalizer:
class: Symfony\Component\Serializer\Normalizer\ObjectNormalizer
arguments:
- null
- null
- '@property_accessor'
- '@property_info.php_doc_extractor'
- '@jsonldclient.property_accessor'
- '@jsonldclient.property_extractor'
- null
- null
- []
Expand Down
31 changes: 31 additions & 0 deletions Tests/Fixtures/Models/NestedClassWithoutDoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace Bookboon\JsonLDClient\Tests\Fixtures\Models;


class NestedClassWithoutDoc
{
protected $string;
protected $simpleClass;

public function getString() : string
{
return $this->string;
}

public function setString(string $string): void
{
$this->string = $string;
}

public function getSimpleClass() : ?SimpleClass
{
return $this->simpleClass;
}

public function setSimpleClass(?SimpleClass $simpleClass): void
{
$this->simpleClass = $simpleClass;
}
}
16 changes: 15 additions & 1 deletion Tests/Fixtures/SerializerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Bookboon\JsonLDClient\Serializer\NullableDateTimeNormalizer;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
Expand All @@ -19,7 +21,19 @@ class SerializerHelper
{
public static function create(array $mappings = [], array $defaultContext = []) : SerializerInterface
{
$propertyExtractor = new PhpDocExtractor();
$docblockExtractor = new PhpDocExtractor();
$reflectionExtractor = new ReflectionExtractor();
$propertyExtractor = new PropertyInfoExtractor(
[
$docblockExtractor,
$reflectionExtractor
],
[
$docblockExtractor,
$reflectionExtractor
]
);

$propertyAccessor = new PropertyAccessor();

$collection = new MappingCollection($mappings, 'Bookboon\JsonLDClient\Tests\Fixtures\Models');
Expand Down
23 changes: 23 additions & 0 deletions Tests/Serializer/JsonLDNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Bookboon\JsonLDClient\Tests\Fixtures\Models\DatedClass;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\NestedArrayClass;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\NestedClass;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\NestedClassWithoutDoc;
use Bookboon\JsonLDClient\Tests\Fixtures\Models\SimpleClass;
use Bookboon\JsonLDClient\Tests\Fixtures\SerializerHelper;
use DateTime;
Expand Down Expand Up @@ -91,6 +92,28 @@ public function testNestedDeserialize() : void
self::assertEquals('some other string', $object->getSimpleClass()->getValue());
}

public function testNestedWithoutDocDeserialize() : void
{
$serializer = SerializerHelper::create([]);
$testJson = <<<JSON
{
"@type": "NestedClassWithoutDoc",
"string": "some random string",
"simpleClass": {
"@type": "SimpleClass",
"value": "some other string"
}
}
JSON;

$object = $serializer->deserialize($testJson, '', JsonLDEncoder::FORMAT);

self::assertInstanceOf(NestedClassWithoutDoc::class, $object);
self::assertEquals("some random string", $object->getString());
self::assertInstanceOf(SimpleClass::class, $object->getSimpleClass());
self::assertEquals('some other string', $object->getSimpleClass()->getValue());
}

public function testNestedArrayDeserialize() : void
{
$serializer = SerializerHelper::create([]);
Expand Down

0 comments on commit 20e301c

Please sign in to comment.