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; +}