Skip to content

Commit

Permalink
Merge pull request #3022 from l0gicgate/FixRouteParserBug
Browse files Browse the repository at this point in the history
Ensure RouteParser Always Present After Routing
  • Loading branch information
l0gicgate committed Nov 15, 2020
2 parents 2974dc3 + e26f4a1 commit 8dde3cb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
11 changes: 8 additions & 3 deletions Slim/Handlers/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ protected function determineStatusCode(): int
* as willdurand/negotiation for any other situation.
*
* @param ServerRequestInterface $request
* @return string
* @return string|null
*/
protected function determineContentType(ServerRequestInterface $request): ?string
{
Expand All @@ -231,10 +231,15 @@ protected function determineContentType(ServerRequestInterface $request): ?strin
* when multiple content types are provided via Accept header.
*/
if ($current === 'text/plain' && $count > 1) {
return next($selectedContentTypes);
$next = next($selectedContentTypes);
if (is_string($next)) {
return $next;
}
}

return $current;
if (is_string($current)) {
return $current;
}
}

if (preg_match('/\+(json|xml)/', $acceptHeader, $matches)) {
Expand Down
3 changes: 2 additions & 1 deletion Slim/Middleware/RoutingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function __construct(RouteResolverInterface $routeResolver, RouteParserIn
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$request = $request->withAttribute(RouteContext::ROUTE_PARSER, $this->routeParser);
$request = $this->performRouting($request);
return $handler->handle($request);
}
Expand All @@ -72,6 +71,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
*/
public function performRouting(ServerRequestInterface $request): ServerRequestInterface
{
$request = $request->withAttribute(RouteContext::ROUTE_PARSER, $this->routeParser);

$routingResults = $this->resolveRoutingResultsFromRequest($request);
$routeStatus = $routingResults->getRouteStatus();

Expand Down
4 changes: 4 additions & 0 deletions tests/Routing/RouteRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ class RouteRunnerTest extends TestCase
public function testRoutingIsPerformedIfRoutingResultsAreUnavailable()
{
$handler = (function (ServerRequestInterface $request, ResponseInterface $response) {
$routeParser = $request->getAttribute(RouteContext::ROUTE_PARSER);
$this->assertInstanceOf(RouteParser::class, $routeParser);

$routingResults = $request->getAttribute(RouteContext::ROUTING_RESULTS);
$this->assertInstanceOf(RoutingResults::class, $routingResults);

return $response;
})->bindTo($this);

Expand Down

0 comments on commit 8dde3cb

Please sign in to comment.