From 919a3092b72f5ba73bccb539d144edb4070fd6c9 Mon Sep 17 00:00:00 2001 From: Yannick ROGER Date: Fri, 12 Apr 2019 15:46:56 +0200 Subject: [PATCH] Backport to v2.3.x fix for correctly parsing enum uriParameters (#148) (cherry picked from commit 5904a4f59df04e0a9bcb7a9fd06300ed95e17bc1) --- src/NamedParameter.php | 2 +- src/Resource.php | 6 +++--- test/NamedParameters/UriParameterTest.php | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/NamedParameter.php b/src/NamedParameter.php index c06b9075..9a4de633 100644 --- a/src/NamedParameter.php +++ b/src/NamedParameter.php @@ -844,7 +844,7 @@ public function getMatchPattern() if ($this->validationPattern) { $pattern = $this->validationPattern; } elseif ($enum = $this->getEnum()) { - $pattern = '^(' . implode('\|', array_map('preg_quote', $enum)) . ')$'; + $pattern = '^(' . implode('|', array_map('preg_quote', $enum)) . ')$'; } else { switch ($this->getType()) { case self::TYPE_NUMBER: diff --git a/src/Resource.php b/src/Resource.php index a36ccbd3..8851f0ce 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -200,11 +200,11 @@ public function matchesUri($uri) if ('^' === $matchPattern[0]) { $matchPattern = substr($matchPattern, 1); } - + if ('$' === substr($matchPattern, -1)) { $matchPattern = substr($matchPattern, 0, -1); } - + $regexUri = str_replace( '/{'.$uriParameter->getKey().'}', '/'.$matchPattern, @@ -221,7 +221,7 @@ public function matchesUri($uri) $regexUri = preg_replace('/\/{.*}/U', '\/([^/]+)', $regexUri); $regexUri = preg_replace('/\/~{.*}/U', '\/([^/]*)', $regexUri); - $regexUri = '|^' . $regexUri . '$|'; + $regexUri = chr(128).'^'.$regexUri.'$'.chr(128); return (bool) preg_match($regexUri, $uri); } diff --git a/test/NamedParameters/UriParameterTest.php b/test/NamedParameters/UriParameterTest.php index 00de4ea0..689e8513 100644 --- a/test/NamedParameters/UriParameterTest.php +++ b/test/NamedParameters/UriParameterTest.php @@ -60,4 +60,26 @@ public function shouldCorrectlyParseRegexUriParameters() $resource = $apiDef->getResourceByUri('/user/alec'); $this->assertInstanceOf('\Raml\Resource', $resource); } + + /** @test */ + public function shouldCorrectlyParseEnumUriParameters() + { + $raml = <<parser->parseFromString($raml, ''); + $resource = $apiDef->getResourceByUri('/user/one'); + $this->assertInstanceOf('\Raml\Resource', $resource); + } }