Skip to content

Commit

Permalink
Merge pull request #304 from Stratadox/master
Browse files Browse the repository at this point in the history
Fix #303: Pass metadata dirs and expression evaluator to JMS serializer builder
  • Loading branch information
goetas authored Feb 19, 2020
2 parents 859ed30 + 84ce3a2 commit 27cfbb4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/HateoasBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ public function build(): Hateoas
}
});

$this->serializerBuilder->addMetadataDirs($this->metadataDirs);
$this->serializerBuilder->setExpressionEvaluator($this->expressionEvaluator);

$jmsSerializer = $this->serializerBuilder->build();

return new Hateoas($jmsSerializer, $linkHelper);
Expand Down
28 changes: 28 additions & 0 deletions tests/Hateoas/Tests/Fixtures/NoAnnotations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Hateoas\Tests\Fixtures;

class NoAnnotations
{
private $id;
private $number;
private $unused = 'N/A';

public function __construct(string $id, int $number)
{
$this->id = $id;
$this->number = $number;
}

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

public function number(): int
{
return $this->number;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<class name="Hateoas\Tests\Fixtures\NoAnnotations" xmlns:h="https://github.com/willdurand/Hateoas" xml-root-name="resource">
<h:relation rel="self">
<h:href uri="https://github.com/willdurand/Hateoas/issues/303" />
</h:relation>
<property name="unused" exclude="true" />
<property name="id" exclude="true" />
<virtual-property name="id" expression="'id-' ~ object.id()" />
</class>
</serializer>
24 changes: 24 additions & 0 deletions tests/Hateoas/Tests/HateoasBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Hateoas\Tests\Fixtures\AdrienBrault;
use Hateoas\Tests\Fixtures\CircularReference1;
use Hateoas\Tests\Fixtures\CircularReference2;
use Hateoas\Tests\Fixtures\NoAnnotations;
use Hateoas\Tests\Fixtures\WithAlternativeRouter;
use Hateoas\UrlGenerator\CallableUrlGenerator;
use JMS\Serializer\SerializationContext;
Expand Down Expand Up @@ -160,4 +161,27 @@ public function testWithNullInEmbedded()
$hateoas->serialize($reference1, 'json', SerializationContext::create()->setSerializeNull(true))
);
}

public function testWithXmlRootNameFromXmlConfiguration()
{
$hateoas = HateoasBuilder::create()
->addMetadataDir(self::rootPath() . '/Fixtures/config')
->build();

$resource = new NoAnnotations('#303', 303);

$this->assertSame(
<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<resource>
<id><![CDATA[id-#303]]></id>
<number>303</number>
<link rel="self" href="https://github.com/willdurand/Hateoas/issues/303"/>
</resource>
XML
,
$hateoas->serialize($resource, 'xml')
);
}
}

0 comments on commit 27cfbb4

Please sign in to comment.