Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to include interface metadata #817

Merged
merged 2 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private function addMetadataSection(NodeBuilder $builder)
->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
->end()
->end()
->booleanNode('include_interfaces')->defaultFalse()->end()
->booleanNode('auto_detection')->defaultTrue()->end()
->booleanNode('infer_types_from_doctrine_metadata')
->info('Infers type information from Doctrine metadata if no explicit type has been defined for a property.')
Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/JMSSerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public function loadInternal(array $config, ContainerBuilder $container)

$container
->getDefinition('jms_serializer.metadata_factory')
->replaceArgument(2, $config['metadata']['debug']);
->replaceArgument(2, $config['metadata']['debug'])
->addMethodCall('setIncludeInterfaces', [$config['metadata']['include_interfaces']]);

// warmup
if (!empty($config['metadata']['warmup']['paths']['included']) && class_exists(Finder::class)) {
Expand Down
2 changes: 2 additions & 0 deletions Resources/doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ values:
file_cache:
dir: "%kernel.cache_dir%/serializer"

include_interfaces: true

# Using auto-detection, the mapping files for each bundle will be
# expected in the Resources/config/serializer directory.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces;

use JMS\Serializer\Annotation\Discriminator;

/**
* @Discriminator(
* field="type",
* map={
* "a": "JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces\AnInterfaceImplementation"
* }
* )
*/
interface AnInterface
{
public function execute(): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces;

use JMS\Serializer\Annotation\Type;

class AnInterfaceImplementation implements AnInterface
{
/**
* @Type("string")
*/
private $bar;

public function __construct(string $bar)
{
$this->bar = $bar;
}

public function execute(): void
{
$this->bar = 'overwrite';
}
}
24 changes: 24 additions & 0 deletions Tests/DependencyInjection/Fixture/IncludeInterfaces/AnObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces;

use JMS\Serializer\Annotation\Type;

class AnObject
{
/**
* @Type("string")
*/
private $foo;

/**
* @Type("JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces\AnInterface")
*/
private $bar;

public function __construct(string $foo, AnInterface $bar)
{
$this->foo = $foo;
$this->bar = $bar;
}
}
32 changes: 32 additions & 0 deletions Tests/DependencyInjection/JMSSerializerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use JMS\Serializer\Metadata\Driver\TypedPropertiesDriver;
use JMS\Serializer\SerializationContext;
use JMS\SerializerBundle\JMSSerializerBundle;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces\AnInterfaceImplementation;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces\AnObject;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionLanguage;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionProperties;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SimpleObject;
Expand Down Expand Up @@ -644,6 +646,36 @@ public function testTypedDriverIsEnabled()
self::assertSame('int', $metadata->propertyMetadata['foo']->type['name']);
}

public function testIncludeInterfaces()
{
$container = $this->getContainerForConfig([
[
'metadata' => [
'include_interfaces' => true,
],
]
]);
$serializer = $container->get('jms_serializer');

$actual = $serializer->toArray(
new AnObject(
'foo',
new AnInterfaceImplementation(
'bar'
)
)
);
$expected = [
'foo' => 'foo',
'bar' => [
'bar' => 'bar',
'type' => 'a',
],
];

self::assertSame($expected, $actual);
}

private function getContainerForConfigLoad(array $configs, ?callable $configurator = null)
{
$bundle = new JMSSerializerBundle();
Expand Down