Skip to content

Commit

Permalink
Improve routes matching (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
czita authored Aug 5, 2022
1 parent 3033fc7 commit 67d7605
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
"branch-alias": {
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Schema/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,15 @@ private function generatePattern(): string

if ($suffixes) {
return sprintf(
'#%s' . // Always start with raw pattern
'#^%s' . // Always start with raw pattern
'(%s)?$#U', // Optionally followed by one of suffixes
$rawPattern,
implode('|', array_map('preg_quote', $suffixes))
);
}

return sprintf(
'#%s$#', // Exactly match raw pattern
'#^%s$#', // Exactly match raw pattern
$rawPattern
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Cases/Core/DI/ApiExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test(function (): void {
Assert::count(4, $schema->getEndpoints());
Assert::equal(['GET'], $schema->getEndpoints()[0]->getMethods());
Assert::equal('/api/v1/foobar/baz1', $schema->getEndpoints()[0]->getMask());
Assert::equal('#/api/v1/foobar/baz1$#', $schema->getEndpoints()[0]->getPattern());
Assert::equal('#^/api/v1/foobar/baz1$#', $schema->getEndpoints()[0]->getPattern());
Assert::equal([], $schema->getEndpoints()[0]->getParameters());
Assert::equal(AnnotationFoobarController::class, $schema->getEndpoints()[0]->getHandler()->getClass());
Assert::equal('baz1', $schema->getEndpoints()[0]->getHandler()->getMethod());
Expand Down
4 changes: 2 additions & 2 deletions tests/Cases/Core/Schema/Serialization/ArrayHydrator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test(function (): void {
Assert::same('/group1-path/group2-path/c1-path/m2-path', $endpoint1->getMask());
Assert::same('/group1-path/group2-path/c1-path/m2-path', $endpoint1->getAttribute('pattern'));
Assert::same(null, $endpoint1->getAttribute('missing'));
Assert::same('#/group1-path/group2-path/c1-path/m2-path$#', $endpoint1->getPattern());
Assert::same('#^/group1-path/group2-path/c1-path/m2-path$#', $endpoint1->getPattern());
Assert::same([], $endpoint1->getParameters());
Assert::same([], $endpoint1->getNegotiations());

Expand All @@ -114,7 +114,7 @@ test(function (): void {
Assert::same('/group1-path/group2-path/c1-path/m3-path/{m3-p1}', $endpoint2->getMask());
Assert::same('/group1-path/group2-path/c1-path/m3-path/(?P<m3-p1>[^/]+)', $endpoint2->getAttribute('pattern'));
Assert::same(null, $endpoint2->getAttribute('missing'));
Assert::same('#/group1-path/group2-path/c1-path/m3-path/(?P<m3-p1>[^/]+)(json|xml)?$#U', $endpoint2->getPattern());
Assert::same('#^/group1-path/group2-path/c1-path/m3-path/(?P<m3-p1>[^/]+)(json|xml)?$#U', $endpoint2->getPattern());

Assert::same(null, $endpoint2->getRequestBody());

Expand Down

0 comments on commit 67d7605

Please sign in to comment.