Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support substituted backed enum in path #171

Merged
merged 1 commit into from
Jan 28, 2024
Merged
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
4 changes: 3 additions & 1 deletion src/Validation/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ protected function validateParameters()
$parameter_value = $route->parameters()[$parameter->name];
if ($parameter_value instanceof Model) {
$parameter_value = $route->originalParameters()[$parameter->name];
} elseif ($parameter_value instanceof \BackedEnum) {
$parameter_value = $route->originalParameters()[$parameter->name];
}
} elseif ($parameter->in === 'query' && $this->hasQueryParam($parameter->name)) {
$parameter_value = $this->getQueryParam($parameter->name);
Expand All @@ -109,7 +111,7 @@ protected function validateParameters()
}

if ($parameter_value) {
if ($expected_parameter_schema->type && gettype($parameter_value) !== $expected_parameter_schema->type) {
if (isset($expected_parameter_schema->type) && gettype($parameter_value) !== $expected_parameter_schema->type) {
$expected_type = $expected_parameter_schema->type;

if ($expected_type === 'number') {
Expand Down
46 changes: 46 additions & 0 deletions tests/Fixtures/Enum.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
openapi: 3.1.0
info:
title: Enum
version: '1.0'
servers:
- url: 'http://localhost:3000'
paths:
/enum-in-path/{type}:
parameters:
- in: path
name: type
required: true
schema:
type: string
enum:
- name
- email
get:
summary: Request with enum in path
tags: []
responses:
'204':
description: No Content
/enum-in-path-via-reference/{type}:
parameters:
- in: path
name: type
required: true
schema:
$refs: '#/components/schemas/TestEnum'
get:
summary: Request with enum in path via reference
tags: []
responses:
'204':
description: No Content

components:
schemas:
TestEnum:
type: string
enum:
- name
- email


66 changes: 66 additions & 0 deletions tests/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -786,8 +788,72 @@ public static function requiredReadOnlySchemaProvider(): array
],
];
}

/**
* @dataProvider enumProvider
*/
public function test_enum_in_path(string $type, bool $isValid): void
{
Spectator::using('Enum.yml');

$this->withoutExceptionHandling([BackedEnumCaseNotFoundException::class]);

Route::get('/enum-in-path/{type}', function (TestEnum $type) {
return response()->noContent();
})->middleware([SubstituteBindings::class, Middleware::class]);

if ($isValid) {
$this->getJson("/enum-in-path/{$type}")
->assertValidRequest();
} else {
$this->getJson("/enum-in-path/{$type}")
->assertInvalidRequest();
}
}

/**
* @dataProvider enumProvider
*/
public function test_enum_in_path_via_reference(string $type, bool $isValid): void
{
Spectator::using('Enum.yml');

$this->withoutExceptionHandling([BackedEnumCaseNotFoundException::class]);

Route::get('/enum-in-path-via-reference/{type}', function (TestEnum $type) {
return response()->noContent();
})->middleware([SubstituteBindings::class, Middleware::class]);

if ($isValid) {
$this->getJson("/enum-in-path-via-reference/{$type}")
->assertValidRequest();
} else {
$this->getJson("/enum-in-path-via-reference/{$type}")
->assertInvalidRequest();
}
}

public static function enumProvider(): array
{
return [
'valid enum' => [
'name',
true,
],
'invalid enum' => [
'foo',
false,
],
];
}
}

class TestUser extends Model
{
}

enum TestEnum: string
{
case name = 'name';
case email = 'email';
}