Skip to content

Commit bb9a7e8

Browse files
committed
bug #3058 [LiveComponent] Fix new URL generation when using LiveProp with custom fieldName (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Fix new URL generation when using `LiveProp` with custom `fieldName` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Follows #2673 Related to https://symfony-devs.slack.com/archives/C01FN4EQNLX/p1756395137633869?thread_ts=1756320013.552729&cid=C01FN4EQNLX, I believe this is the ultimate last fix for the `X-Live-Url` generation based on `LiveProp` 😅 I confirm that in <2.28, the URL query params were using the `fieldName` value from `LiveProp` if available, but we missed it (I didn't even know about that, the feature is not documented). Commits ------- 25c3e13 [LiveComponent] Fix new URL generation when using `LiveProp` with custom `fieldName`
2 parents e0976b1 + 25c3e13 commit bb9a7e8

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/LiveComponent/src/Metadata/LiveComponentMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getAllUrlMappings(object $component): array
7575

7676
foreach ($this->getAllLivePropsMetadata($component) as $livePropMetadata) {
7777
if ($livePropMetadata->urlMapping()) {
78-
$urlMappings[$livePropMetadata->getName()] = $livePropMetadata->urlMapping();
78+
$urlMappings[$livePropMetadata->calculateFieldName($component, $livePropMetadata->getName())] = $livePropMetadata->urlMapping();
7979
}
8080
}
8181

src/LiveComponent/tests/Fixtures/Component/ComponentWithUrlBoundProps.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class ComponentWithUrlBoundProps
3333
#[LiveProp(url: true)]
3434
public array $arrayProp = [];
3535

36+
#[LiveProp(url: new UrlMapping(as: 'arr_alias'))]
37+
public array $arrayPropAlias = [];
38+
39+
#[LiveProp(writable: true, fieldName: 'getArrayFieldName()', url: true)]
40+
public array $arrayPropFieldName = [];
41+
3642
#[LiveProp]
3743
public ?string $unboundProp = null;
3844

@@ -113,4 +119,9 @@ public function updateLiveProp(#[LiveArg] string $propName, #[LiveArg] mixed $pr
113119

114120
$this->{$propName} = $propValue;
115121
}
122+
123+
public function getArrayFieldName(): string
124+
{
125+
return 'arr_field_name';
126+
}
116127
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ public function testQueryStringMappingAttribute()
144144
'objectPropWithSerializerForHydration' => ['name' => 'objectPropWithSerializerForHydration'],
145145
'propertyWithModifierAndAlias' => ['name' => 'alias_p'],
146146
'pathPropForAnotherController' => ['name' => 'pathPropForAnotherController'],
147+
'arrayPropAlias' => ['name' => 'arr_alias'],
148+
'arr_field_name' => ['name' => 'arr_field_name'],
147149
];
148150

149151
$this->assertEquals($expected, $queryMapping);

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public function getTestData(): iterable
118118
'propValue' => 'bar',
119119
],
120120
];
121+
121122
yield 'Changes in prop, with two path params but only one prop' => [
122123
'previousLocation' => '/route_with_two_path_params_but_one_prop/foo/30',
123124
'expectedLocation' => '/route_with_two_path_params_but_one_prop/bar/30',
@@ -140,6 +141,42 @@ public function getTestData(): iterable
140141
],
141142
];
142143

144+
yield 'Change in query (array)' => [
145+
'previousLocation' => '/route_with_prop/foo',
146+
'expectedLocation' => '/route_with_prop/foo?arrayProp%5B0%5D=hello&arrayProp%5B1%5D=world',
147+
'initialComponentData' => [
148+
'pathProp' => 'foo',
149+
],
150+
'args' => [
151+
'propName' => 'arrayProp',
152+
'propValue' => ['hello', 'world'],
153+
],
154+
];
155+
156+
yield 'Change in query (array & alias)' => [
157+
'previousLocation' => '/route_with_prop/foo',
158+
'expectedLocation' => '/route_with_prop/foo?arr_alias%5B0%5D=hello&arr_alias%5B1%5D=world',
159+
'initialComponentData' => [
160+
'pathProp' => 'foo',
161+
],
162+
'args' => [
163+
'propName' => 'arrayPropAlias',
164+
'propValue' => ['hello', 'world'],
165+
],
166+
];
167+
168+
yield 'Change in query (array & field name)' => [
169+
'previousLocation' => '/route_with_prop/foo',
170+
'expectedLocation' => '/route_with_prop/foo?arr_field_name%5B0%5D=hello&arr_field_name%5B1%5D=world',
171+
'initialComponentData' => [
172+
'pathProp' => 'foo',
173+
],
174+
'args' => [
175+
'propName' => 'arrayPropFieldName',
176+
'propValue' => ['hello', 'world'],
177+
],
178+
];
179+
143180
yield 'Changes in props and query' => [
144181
'previousLocation' => '/route_with_prop/foo',
145182
'expectedLocation' => '/route_with_prop/baz?q=foo+bar',

0 commit comments

Comments
 (0)