Skip to content

Commit 41383ee

Browse files
bug #521 [Live] Forcing JSON to be an object instead of an empty array (weaverryan)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Live] Forcing JSON to be an object instead of an empty array | Q | A | ------------- | --- | Bug fix? | yes/no | New feature? | no | Tickets | None | License | MIT If data or props are an empty array, the JSON would be `[]`, causing Stimulus to throw an exception as this is not an object (and the `data` value is meant to be an object). Cheers! Commits ------- 21dec71 fixing deterministic id 8082c56 [Live] Forcing JSON to be an object instead of an empty array
2 parents aac29cb + 4396585 commit 41383ee

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/LiveComponent/src/EventListener/AddLiveAttributesSubscriber.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\UX\LiveComponent\LiveComponentHydrator;
2121
use Symfony\UX\LiveComponent\Twig\DeterministicTwigIdCalculator;
2222
use Symfony\UX\LiveComponent\Util\FingerprintCalculator;
23+
use Symfony\UX\LiveComponent\Util\JsonUtil;
2324
use Symfony\UX\LiveComponent\Util\TwigAttributeHelper;
2425
use Symfony\UX\TwigComponent\ComponentAttributes;
2526
use Symfony\UX\TwigComponent\ComponentMetadata;
@@ -100,8 +101,8 @@ private function getLiveAttributes(MountedComponent $mounted, ComponentMetadata
100101
$attributes = [
101102
'data-controller' => 'live',
102103
'data-live-url-value' => $helper->escapeAttribute($url),
103-
'data-live-data-value' => $helper->escapeAttribute(json_encode($dehydratedComponent->getData(), \JSON_THROW_ON_ERROR)),
104-
'data-live-props-value' => $helper->escapeAttribute(json_encode($dehydratedComponent->getProps(), \JSON_THROW_ON_ERROR)),
104+
'data-live-data-value' => $helper->escapeAttribute(JsonUtil::encodeObject($dehydratedComponent->getData())),
105+
'data-live-props-value' => $helper->escapeAttribute(JsonUtil::encodeObject($dehydratedComponent->getProps())),
105106
];
106107

107108
if ($this->container->has(CsrfTokenManagerInterface::class) && $metadata->get('csrf')) {

src/LiveComponent/src/EventListener/InterceptChildComponentRenderSubscriber.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\UX\LiveComponent\LiveComponentHydrator;
1616
use Symfony\UX\LiveComponent\Twig\DeterministicTwigIdCalculator;
1717
use Symfony\UX\LiveComponent\Util\FingerprintCalculator;
18+
use Symfony\UX\LiveComponent\Util\JsonUtil;
1819
use Symfony\UX\LiveComponent\Util\TwigAttributeHelper;
1920
use Symfony\UX\TwigComponent\ComponentFactory;
2021
use Symfony\UX\TwigComponent\ComponentStack;
@@ -99,7 +100,7 @@ public function preComponentCreated(PreCreateForRenderEvent $event): void
99100
'<div data-live-id="%s" data-live-fingerprint-value="%s" data-live-props-value="%s"></div>',
100101
$this->twigAttributeHelper->escapeAttribute($deterministicId),
101102
$this->twigAttributeHelper->escapeAttribute($newPropsFingerprint),
102-
$this->twigAttributeHelper->escapeAttribute(json_encode($dehydratedComponent->getProps(), \JSON_THROW_ON_ERROR))
103+
$this->twigAttributeHelper->escapeAttribute(JsonUtil::encodeObject($dehydratedComponent->getProps()))
103104
);
104105
$event->setRenderedString($rendered);
105106
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\LiveComponent\Util;
13+
14+
/**
15+
* @author Ryan Weaver <ryan@symfonycasts.com>
16+
*
17+
* @experimental
18+
*
19+
* @internal
20+
*/
21+
final class JsonUtil
22+
{
23+
public static function encodeObject($data): string
24+
{
25+
if (0 === \count($data)) {
26+
return '{}';
27+
}
28+
29+
return json_encode($data, \JSON_THROW_ON_ERROR);
30+
}
31+
}

src/LiveComponent/tests/Functional/EventListener/AddLiveAttributesSubscriberTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public function testItAddsIdAndFingerprintToChildComponent(): void
8585

8686
$lis = $ul->children('li');
8787
// deterministic id: should not change, and counter should increase
88-
$this->assertSame('live-2816377500-0', $lis->first()->attr('data-live-id'));
89-
$this->assertSame('live-2816377500-2', $lis->last()->attr('data-live-id'));
88+
$this->assertSame('live-3649730296-0', $lis->first()->attr('data-live-id'));
89+
$this->assertSame('live-3649730296-2', $lis->last()->attr('data-live-id'));
9090

9191
// fingerprints
9292
// first and last both have the same input - thus fingerprint

0 commit comments

Comments
 (0)