From fbce09d838431641956488d64c1e979b9b818bc8 Mon Sep 17 00:00:00 2001 From: Maxcastel Date: Tue, 1 Dec 2026 16:11:29 +0100 Subject: [PATCH] fix(laravel): keep the validation rules of a parameter --- ...ationResourceMetadataCollectionFactory.php | 4 ++ .../Tests/ParameterUserConstraintsTest.php | 35 +++++++++++++++ .../app/Models/ConstrainedMagazine.php | 43 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/Laravel/Tests/ParameterUserConstraintsTest.php create mode 100644 src/Laravel/workbench/app/Models/ConstrainedMagazine.php diff --git a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php index 11b2aba085d..7453779697f 100644 --- a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php +++ b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php @@ -73,6 +73,10 @@ public function create(string $resourceClass): ResourceMetadataCollection private function addSchemaValidation(Parameter $parameter): Parameter { + if (null !== $parameter->getConstraints()) { + return $parameter; + } + $schema = $parameter->getSchema(); $required = $parameter->getRequired(); $openApi = $parameter->getOpenApi(); diff --git a/src/Laravel/Tests/ParameterUserConstraintsTest.php b/src/Laravel/Tests/ParameterUserConstraintsTest.php new file mode 100644 index 00000000000..eacfefa5745 --- /dev/null +++ b/src/Laravel/Tests/ParameterUserConstraintsTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Laravel\Tests; + +use Illuminate\Foundation\Testing\RefreshDatabase; +use Orchestra\Testbench\Concerns\WithWorkbench; +use Orchestra\Testbench\TestCase; + +class ParameterUserConstraintsTest extends TestCase +{ + use RefreshDatabase; + use WithWorkbench; + + public function testUserConstraintsAreNotReplaced(): void + { + $this->get('/api/constrained_magazines?search=ab', ['accept' => 'application/ld+json'])->assertStatus(422); + $this->get('/api/constrained_magazines', ['accept' => 'application/ld+json'])->assertStatus(422); + $this->get('/api/constrained_magazines?search=abc', ['accept' => 'application/ld+json'])->assertStatus(200); + + // 1.5 passes the "gte:1" rule generated from the schema, but not the "integer" one of the parameter + $this->get('/api/constrained_magazines?search=abc&issue=1.5', ['accept' => 'application/ld+json'])->assertStatus(422); + $this->get('/api/constrained_magazines?search=abc&issue=2', ['accept' => 'application/ld+json'])->assertStatus(200); + } +} diff --git a/src/Laravel/workbench/app/Models/ConstrainedMagazine.php b/src/Laravel/workbench/app/Models/ConstrainedMagazine.php new file mode 100644 index 00000000000..0a9759c8d83 --- /dev/null +++ b/src/Laravel/workbench/app/Models/ConstrainedMagazine.php @@ -0,0 +1,43 @@ + + * + * 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\Models; + +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Metadata\QueryParameter; +use Illuminate\Database\Eloquent\Model; + +/** + * The magazines, with parameters having their own validation rules. + */ +#[ApiResource( + operations: [ + new GetCollection( + uriTemplate: '/constrained_magazines', + parameters: [ + 'search' => new QueryParameter(constraints: ['required', 'min:3'], required: true), + 'issue' => new QueryParameter(schema: ['type' => 'integer', 'minimum' => 1], constraints: 'integer|min:1'), + ], + ), + new Get(uriTemplate: '/constrained_magazines/{uuid}'), + ], +)] +class ConstrainedMagazine extends Model +{ + protected $table = 'magazines'; + protected $primaryKey = 'uuid'; + protected $keyType = 'string'; + public $incrementing = false; +}