Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Backport to v2.3.x fix for correctly parsing enum uriParameters (#148)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5904a4f)
  • Loading branch information
yannickroger authored and martin-georgiev committed Apr 12, 2019
1 parent a79c591 commit 919a309
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/NamedParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down
22 changes: 22 additions & 0 deletions test/NamedParameters/UriParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,26 @@ public function shouldCorrectlyParseRegexUriParameters()
$resource = $apiDef->getResourceByUri('/user/alec');
$this->assertInstanceOf('\Raml\Resource', $resource);
}

/** @test */
public function shouldCorrectlyParseEnumUriParameters()
{
$raml = <<<RAML
#%RAML 0.8
title: User API
version: 1.2
/user:
/{userName}:
displayName: Get a user by name
uriParameters:
userName:
enum: [one, two]
get:
displayName: retrieve a user's picture by user name
RAML;

$apiDef = $this->parser->parseFromString($raml, '');
$resource = $apiDef->getResourceByUri('/user/one');
$this->assertInstanceOf('\Raml\Resource', $resource);
}
}

0 comments on commit 919a309

Please sign in to comment.