diff --git a/src/Middleware.php b/src/Middleware.php index c734e77..36334eb 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -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()); @@ -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); diff --git a/src/Validation/RequestValidator.php b/src/Validation/RequestValidator.php index fc8350b..d09fcf6 100644 --- a/src/Validation/RequestValidator.php +++ b/src/Validation/RequestValidator.php @@ -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)) diff --git a/tests/Fixtures/ParametersNameMismatch.v1.yml b/tests/Fixtures/ParametersNameMismatch.v1.yml new file mode 100644 index 0000000..50232cc --- /dev/null +++ b/tests/Fixtures/ParametersNameMismatch.v1.yml @@ -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: {} diff --git a/tests/RequestValidatorTest.php b/tests/RequestValidatorTest.php index b8ba3ec..c088ebe 100644 --- a/tests/RequestValidatorTest.php +++ b/tests/RequestValidatorTest.php @@ -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