Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure RouteParser Always Present After Routing #3022

Merged
merged 3 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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