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

Commit 919a309

Browse files
yannickrogermartin-georgiev
authored andcommitted
Backport to v2.3.x fix for correctly parsing enum uriParameters (#148)
(cherry picked from commit 5904a4f)
1 parent a79c591 commit 919a309

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/NamedParameter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ public function getMatchPattern()
844844
if ($this->validationPattern) {
845845
$pattern = $this->validationPattern;
846846
} elseif ($enum = $this->getEnum()) {
847-
$pattern = '^(' . implode('\|', array_map('preg_quote', $enum)) . ')$';
847+
$pattern = '^(' . implode('|', array_map('preg_quote', $enum)) . ')$';
848848
} else {
849849
switch ($this->getType()) {
850850
case self::TYPE_NUMBER:

src/Resource.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ public function matchesUri($uri)
200200
if ('^' === $matchPattern[0]) {
201201
$matchPattern = substr($matchPattern, 1);
202202
}
203-
203+
204204
if ('$' === substr($matchPattern, -1)) {
205205
$matchPattern = substr($matchPattern, 0, -1);
206206
}
207-
207+
208208
$regexUri = str_replace(
209209
'/{'.$uriParameter->getKey().'}',
210210
'/'.$matchPattern,
@@ -221,7 +221,7 @@ public function matchesUri($uri)
221221

222222
$regexUri = preg_replace('/\/{.*}/U', '\/([^/]+)', $regexUri);
223223
$regexUri = preg_replace('/\/~{.*}/U', '\/([^/]*)', $regexUri);
224-
$regexUri = '|^' . $regexUri . '$|';
224+
$regexUri = chr(128).'^'.$regexUri.'$'.chr(128);
225225

226226
return (bool) preg_match($regexUri, $uri);
227227
}

test/NamedParameters/UriParameterTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,26 @@ public function shouldCorrectlyParseRegexUriParameters()
6060
$resource = $apiDef->getResourceByUri('/user/alec');
6161
$this->assertInstanceOf('\Raml\Resource', $resource);
6262
}
63+
64+
/** @test */
65+
public function shouldCorrectlyParseEnumUriParameters()
66+
{
67+
$raml = <<<RAML
68+
#%RAML 0.8
69+
title: User API
70+
version: 1.2
71+
/user:
72+
/{userName}:
73+
displayName: Get a user by name
74+
uriParameters:
75+
userName:
76+
enum: [one, two]
77+
get:
78+
displayName: retrieve a user's picture by user name
79+
RAML;
80+
81+
$apiDef = $this->parser->parseFromString($raml, '');
82+
$resource = $apiDef->getResourceByUri('/user/one');
83+
$this->assertInstanceOf('\Raml\Resource', $resource);
84+
}
6385
}

0 commit comments

Comments
 (0)