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

Add regex pattern match against the requested url path (#1) #197

Merged
merged 1 commit into from
Jan 17, 2025
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
13 changes: 10 additions & 3 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function handle(Request $request, Closure $next): mixed
try {
/** @var \Illuminate\Routing\Route $route */
$route = $request->route();
[$specPath, $pathItem] = $this->pathItem($route, $request->method());
[$specPath, $pathItem] = $this->pathItem($route, $request->method(), $request->path());
} catch (InvalidPathException|MalformedSpecException|MissingSpecException|TypeErrorException|UnresolvableReferenceException $exception) {
$this->spectator->captureRequestValidation($exception);
$this->spectator->captureResponseValidation($exception);
Expand Down Expand Up @@ -117,7 +117,7 @@ protected function validate(Request $request, Closure $next, string $specPath, P
* @throws IOException
* @throws InvalidJsonPointerSyntaxException
*/
protected function pathItem(Route $route, string $requestMethod): array
protected function pathItem(Route $route, string $requestMethod, string $requestUrlPath): array
{
$requestPath = Str::start($route->uri(), '/');

Expand All @@ -142,7 +142,14 @@ protected function pathItem(Route $route, string $requestMethod): array

if (Str::match($route->getCompiled()->getRegex(), $resolvedPath) !== '') {
$pathMatches = true;
if (in_array(strtolower($requestMethod), $methods, true)) {
$requestUrlPath = '/'.ltrim($requestUrlPath, '/');
$regExPattern = preg_replace('/\{[^}]+\}/', '.+', $resolvedPath);
$regExPattern = str_replace('/', '\/', $regExPattern);

$matchedMethod = in_array(strtolower($requestMethod), $methods, true);
$matchRequestUrl = preg_match('/^'.$regExPattern.'$/', $requestUrlPath) !== 0;

if ($matchedMethod && $matchRequestUrl) {
$partialMatch = [$resolvedPath, $pathItem];
}
}
Expand Down
63 changes: 63 additions & 0 deletions tests/Fixtures/Test.v3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
openapi: 3.0.0
info:
title: Test.v3
version: '1.0'
servers:
- url: 'http://localhost:3000'
paths:
/cars/electric:
patch:
summary: Update car charging status
tags: []
requestBody:
content:
application/json:
schema:
type: object
required:
- charging
properties:
charging:
type: boolean
description: Update charging status
example: true
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
name:
type: string

/cars/ice:
patch:
summary: Update car refilling status
tags: []
requestBody:
content:
application/json:
schema:
type: object
required:
- refill
properties:
refill:
type: boolean
description: Update refill status
example: true
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
name:
type: string

components:
schemas: {}
17 changes: 17 additions & 0 deletions tests/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,23 @@ public function test_parameter_decoupling(): void
$this->getJson('/users/1')
->assertValidRequest();
}

public function test_static_url_parameter_decoupling(): void
{
Spectator::using('Test.v3.yaml');

Route::patch('/cars/{name}', function ($name) {
return [
'name' => $name,
];
})->middleware([SubstituteBindings::class, Middleware::class]);

$this->patchJson('/cars/electric', ['charging' => true])
->assertValidRequest();

$this->patchJson('/cars/ice', ['refill' => true])
->assertValidRequest();
}
}

class TestUser extends Model
Expand Down