Skip to content

Commit

Permalink
fix(laravel): overlaping route format
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Nov 8, 2024
1 parent 64e3a5a commit 17e2c17
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@ public function boot(ResourceNameCollectionFactoryInterface $resourceNameCollect
// _format is read by the middleware
$uriTemplate = $operation->getRoutePrefix().str_replace('{._format}', '{_format?}', $uriTemplate);
$route = (new Route([$operation->getMethod()], $uriTemplate, [ApiPlatformController::class, '__invoke']))
->where('_format', '^\.[a-zA-Z]+')
->name($operation->getName())
->setDefaults(['_api_operation_name' => $operation->getName(), '_api_resource_class' => $operation->getClass()]);

Expand Down
11 changes: 11 additions & 0 deletions src/Laravel/Tests/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected function defineEnvironment($app): void
{
tap($app['config'], function (Repository $config): void {
$config->set('app.debug', true);
$config->set('api-platform.resources', [app_path('Models'), app_path('ApiResource')]);
});
}

Expand Down Expand Up @@ -337,4 +338,14 @@ public function testRelationWithGroups(): void
$this->assertArrayHasKey('relation', $content);
$this->assertArrayHasKey('name', $content['relation']);
}

/**
* @see https://github.com/api-platform/core/issues/6779
*/
public function testSimilarRoutesWithFormat(): void
{
$response = $this->get('/api/staff_position_histories?page=1', ['accept' => 'application/ld+json']);
$response->assertStatus(200);
$this->assertSame('/api/staff_position_histories', $response->json());
}
}
25 changes: 25 additions & 0 deletions src/Laravel/workbench/app/ApiResource/Staff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Workbench\App\ApiResource;

use ApiPlatform\Metadata\ApiResource;

#[ApiResource(provider: [self::class, 'provide'])]
class Staff
{
public static function provide()
{
return [];
}
}
25 changes: 25 additions & 0 deletions src/Laravel/workbench/app/ApiResource/StaffPositionHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Workbench\App\ApiResource;

use ApiPlatform\Metadata\ApiResource;

#[ApiResource(provider: [self::class, 'provide'])]
class StaffPositionHistory
{
public static function provide()
{
return [];
}
}

0 comments on commit 17e2c17

Please sign in to comment.