Skip to content

Commit 6940885

Browse files
committed
bug #2961 [LiveComponent] Fix LiveUrlSubscriber throw MethodNotAllowed (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] Fix LiveUrlSubscriber throw `MethodNotAllowed` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | Fix #2960 | License | MIT > [!WARNING] > Not sure this is enough, but If anyone wants to build on it, feel free - wont be around tomorrow. --- The new LiveUrlSubscriber calls the Router to match the previous Url. At that instant, the RoutingContext is most of the time in POST, and so calling router->match($previousUrl) would throw a MethodNotAllowedException --- Most recent commit ([Catch MethodNotAllowedException](1da8a7b)) fixes _**this**_ issue ... but I think we have probably other use cases where it would not work. My previous commit (reverted in the current state) [Force RequestsContext to "GET"](e81aad2) seems a more promising lead. And the first one (reverted too in current state) [No LiveProps no change](1da8a7b) was to avoid entirely the generation when no props were extracted from URL... sadly i don't think it's enough to say "no props should now be used in URL".. ? --- Thank you `@zacharylund` for the investigation and `@jmsche` for the perfect reproducer Commits ------- 5b8d701 [LiveComponent] Fix LiveUrlSubscriber throw `MethodNotAllowed`
2 parents 43fd7e9 + 5b8d701 commit 6940885

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/LiveComponent/src/Util/UrlFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\LiveComponent\Util;
1313

14+
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
1415
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
1516
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
1617
use Symfony\Component\Routing\RouterInterface;
@@ -43,7 +44,7 @@ public function createFromPreviousAndProps(
4344

4445
try {
4546
$newUrl = $this->createPath($previousUrl, $pathMappedProps);
46-
} catch (ResourceNotFoundException|MissingMandatoryParametersException) {
47+
} catch (ResourceNotFoundException|MethodNotAllowedException|MissingMandatoryParametersException) {
4748
return null;
4849
}
4950

src/LiveComponent/tests/Unit/Util/UrlFactoryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\LiveComponent\Tests\Unit\Util;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
1516
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
1617
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
1718
use Symfony\Component\Routing\RouterInterface;
@@ -140,6 +141,19 @@ public function testResourceNotFoundException()
140141
$this->assertNull($factory->createFromPreviousAndProps($previousUrl, [], []));
141142
}
142143

144+
public function testMethodNotAllowedException()
145+
{
146+
$previousUrl = '/foo/bar';
147+
$router = $this->createMock(RouterInterface::class);
148+
$router->expects(self::once())
149+
->method('match')
150+
->with($previousUrl)
151+
->willThrowException(new MethodNotAllowedException(['GET']));
152+
$factory = new UrlFactory($router);
153+
154+
$this->assertNull($factory->createFromPreviousAndProps($previousUrl, [], []));
155+
}
156+
143157
public function testMissingMandatoryParametersException()
144158
{
145159
$previousUrl = '/foo/bar';

0 commit comments

Comments
 (0)