Skip to content

[JsonEncoder] Add JsonEncodable attribute #59401

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

Merged
merged 1 commit into from
Jan 15, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class UnusedTagsPass implements CompilerPassInterface
'html_sanitizer',
'http_client.client',
'json_encoder.denormalizer',
'json_encoder.encodable',
'json_encoder.normalizer',
'kernel.cache_clearer',
'kernel.cache_warmer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
use Symfony\Component\JsonEncoder\Attribute\JsonEncodable;
use Symfony\Component\JsonEncoder\Decode\Denormalizer\DenormalizerInterface as JsonEncoderDenormalizerInterface;
use Symfony\Component\JsonEncoder\DecoderInterface as JsonEncoderDecoderInterface;
use Symfony\Component\JsonEncoder\Encode\Normalizer\NormalizerInterface as JsonEncoderNormalizerInterface;
Expand Down Expand Up @@ -745,6 +746,10 @@ static function (ChildDefinition $definition, AsPeriodicTask|AsCronTask $attribu
}
);
}
$container->registerAttributeForAutoconfiguration(JsonEncodable::class, static function (ChildDefinition $definition): void {
$definition->addTag('json_encoder.encodable');
$definition->addTag('container.excluded');
});

if (!$container->getParameter('kernel.debug')) {
// remove tagged iterator argument for resource checkers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\Dto\Dummy;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\JsonEncoder\DecoderInterface;
use Symfony\Component\JsonEncoder\EncoderInterface;
use Symfony\Component\TypeInfo\Type;
Expand All @@ -21,10 +22,13 @@
*/
class JsonEncoderTest extends AbstractWebTestCase
{
public function testEncode()
protected function setUp(): void
{
static::bootKernel(['test_case' => 'JsonEncoder']);
}

public function testEncode()
{
/** @var EncoderInterface $encoder */
$encoder = static::getContainer()->get('json_encoder.encoder.alias');

Expand All @@ -33,8 +37,6 @@ public function testEncode()

public function testDecode()
{
static::bootKernel(['test_case' => 'JsonEncoder']);

/** @var DecoderInterface $decoder */
$decoder = static::getContainer()->get('json_encoder.decoder.alias');

Expand All @@ -44,4 +46,22 @@ public function testDecode()

$this->assertEquals($expected, $decoder->decode('{"@name": "DUMMY", "range": "0..1"}', Type::object(Dummy::class)));
}

public function testWarmupEncodableClasses()
{
/** @var Filesystem $fs */
$fs = static::getContainer()->get('filesystem');

$encodersDir = \sprintf('%s/json_encoder/encoder/', static::getContainer()->getParameter('kernel.cache_dir'));

// clear already created encoders
if ($fs->exists($encodersDir)) {
$fs->remove($encodersDir);
}

static::getContainer()->get('json_encoder.cache_warmer.encoder_decoder.alias')->warmUp(static::getContainer()->getParameter('kernel.cache_dir'));

$this->assertFileExists($encodersDir);
$this->assertCount(1, glob($encodersDir.'/*'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\RangeNormalizer;
use Symfony\Component\JsonEncoder\Attribute\Denormalizer;
use Symfony\Component\JsonEncoder\Attribute\EncodedName;
use Symfony\Component\JsonEncoder\Attribute\JsonEncodable;
use Symfony\Component\JsonEncoder\Attribute\Normalizer;

/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
#[JsonEncodable]
class Dummy
{
#[EncodedName('@name')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ services:
alias: json_encoder.decoder
public: true

json_encoder.cache_warmer.encoder_decoder.alias:
alias: .json_encoder.cache_warmer.encoder_decoder
public: true

Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\Dto\Dummy: ~
Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\RangeNormalizer: ~
22 changes: 22 additions & 0 deletions src/Symfony/Component/JsonEncoder/Attribute/JsonEncodable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonEncoder\Attribute;

/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*
* @experimental
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
final class JsonEncodable
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public function process(ContainerBuilder $container): void
$encodableClassNames = [];

// retrieve concrete services tagged with "json_encoder.encodable" tag
foreach ($container->findTaggedServiceIds('json_encoder.encodable') as $id => $tags) {
foreach ($container->getDefinitions() as $id => $definition) {
if (!$definition->hasTag('json_encoder.encodable')) {
continue;
}

if (($className = $container->getDefinition($id)->getClass()) && !$container->getDefinition($id)->isAbstract()) {
$encodableClassNames[] = $className;
}
Expand Down