Skip to content
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
5 changes: 4 additions & 1 deletion src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ protected function pathItem(Route $route, string $requestMethod, string $request
$pathMatches = false;
$partialMatch = null;

$route->wheres = [];
$pathMatchRegex = $route->toSymfonyRoute()->compile()->getRegex();

foreach ($openapi->paths as $path => $pathItem) {
$resolvedPath = $this->resolvePath($path);
$methods = array_keys($pathItem->getOperations());
Expand All @@ -140,7 +143,7 @@ protected function pathItem(Route $route, string $requestMethod, string $request
}
}

if (Str::match($route->getCompiled()->getRegex(), $resolvedPath) !== '') {
if (Str::match($pathMatchRegex, $resolvedPath) !== '') {
$pathMatches = true;
$requestUrlPath = '/'.ltrim($requestUrlPath, '/');
$regExPattern = preg_replace('/\{[^}]+\}/', '.+', $resolvedPath);
Expand Down
5 changes: 4 additions & 1 deletion src/Validation/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ protected function translateParameterName(string $parameter): string
/** @var \Illuminate\Routing\Route $route */
$route = $this->request->route();

preg_match($route->getCompiled()->getRegex(), $this->specPath, $matches);
$route->wheres = [];
$regex = $route->toSymfonyRoute()->compile()->getRegex();

preg_match($regex, $this->specPath, $matches);

return Collection::make($matches)
->filter(fn (string $value, int|string $key) => is_string($key))
Expand Down
34 changes: 34 additions & 0 deletions tests/Fixtures/ParametersNameMismatch.v1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: 3.0.0
info:
title: Global.v1
version: '1.0'
servers:
- url: 'https://api.protect.earth/v1'
description: Production
paths:
/testing/{id}:
get:
summary: Get org by int id
tags: [ ]
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
id:
type: integer
name:
type: string
operationId: get-something
parameters:
- schema:
type: integer
format: int64
name: id
in: path
required: true
components:
schemas: {}
18 changes: 18 additions & 0 deletions tests/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,24 @@ public function test_static_url_parameter_decoupling(): void
$this->patchJson('/cars/ice', ['refill' => true])
->assertValidRequest();
}

public function test_route_with_parameters_on_name_mismatch_non_string(): void
{
Config::set('spectator.path_prefix', 'v1');

Spectator::using('ParametersNameMismatch.v1.yml');

Route::get('/v1/testing/{idx}', function (int $idx) {
return [
'id' => $idx,
'name' => 'Testing',
];
})->whereNumber('idx')->middleware(Middleware::class);

$this->getJson('/v1/testing/100')
->assertValidRequest()
->assertValidResponse(200);
}
}

class TestUser extends Model
Expand Down