Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
35 changes: 35 additions & 0 deletions src/Laravel/Tests/ParameterUserConstraintsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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);
}
}
43 changes: 43 additions & 0 deletions src/Laravel/workbench/app/Models/ConstrainedMagazine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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;
}
Loading