Skip to content

Commit ce76cdc

Browse files
committed
Merge branch 'release/1.1.1'
2 parents 271c9fe + 9d9fda9 commit ce76cdc

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6+
## [1.1.1] - 2022-09-14
7+
8+
### Fixed
9+
10+
- [laravel-json-api#204](https://github.com/laravel-json-api/laravel/issues/204) Fixed `acceptsMiddleware` functionality
11+
when there is no matched route on the request.
12+
613
## [1.1.0] - 2022-02-09
714

815
### Added

src/ExceptionParser.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,13 @@ public function acceptsJson(): self
245245
*/
246246
public function acceptsMiddleware(...$middleware): self
247247
{
248-
$this->accept[] = static fn($ex, $request): bool => Collection::make($middleware)
249-
->intersect($request->route()->gatherMiddleware())
250-
->isNotEmpty();
248+
$this->accept[] = static function ($ex, $request) use ($middleware): bool {
249+
$route = $request->route();
250+
251+
return Collection::make($middleware)
252+
->intersect($route ? $route->gatherMiddleware() : [])
253+
->isNotEmpty();
254+
};
251255

252256
return $this;
253257
}

tests/Integration/AcceptHeaderTest.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace LaravelJsonApi\Exceptions\Tests\Integration;
2121

2222
use Illuminate\Contracts\Debug\ExceptionHandler;
23+
use Illuminate\Foundation\Application;
2324
use Illuminate\Http\Request;
2425
use Illuminate\Support\Facades\Route;
2526
use LaravelJsonApi\Exceptions\ExceptionParser;
@@ -43,7 +44,7 @@ protected function setUp(): void
4344
Route::middleware('api')->get('/test', function () {
4445
throw new HttpException(
4546
418,
46-
"I think I might be a teapot.",
47+
'I think I might be a teapot.',
4748
);
4849
});
4950

@@ -163,6 +164,48 @@ public function testAcceptsMiddlewareDoesNotMatch(): void
163164
->assertSee('teapot');
164165
}
165166

167+
/**
168+
* @see https://github.com/laravel-json-api/laravel/issues/204
169+
*/
170+
public function testAcceptsMiddlewareWhenRouteNotFound(): void
171+
{
172+
Handler::$testRenderer = ExceptionParser::make()
173+
->acceptsMiddleware('foo', 'api')
174+
->renderable();
175+
176+
$response = $this->get('/blah', ['Accept' => '*/*']);
177+
$response->assertStatus(404)->assertSee('Not Found');
178+
179+
// @TODO remove once Laravel 8 is no longer supported (8 doesn't add the header)
180+
if (version_compare(Application::VERSION, '9.0.0') >= 0) {
181+
$response->assertHeader('Content-Type', 'text/html; charset=UTF-8');
182+
}
183+
}
184+
185+
public function testAcceptsMiddlewareWhenRouteNotFoundWithJsonApiMediaType(): void
186+
{
187+
$expected = [
188+
'errors' => [
189+
[
190+
'title' => 'Not Found',
191+
'status' => '404',
192+
]
193+
],
194+
'jsonapi' => [
195+
'version' => '1.0',
196+
],
197+
];
198+
199+
Handler::$testRenderer = ExceptionParser::make()
200+
->acceptsMiddleware('foo', 'api')
201+
->renderable();
202+
203+
$this->get('/blah', ['Accept' => 'application/vnd.api+json'])
204+
->assertStatus(404)
205+
->assertHeader('Content-Type', 'application/vnd.api+json')
206+
->assertExactJson($expected);
207+
}
208+
166209
/**
167210
* Allow a developer to use their own callback for whether JSON:API should be rendered.
168211
*

tests/Integration/ExceptionsTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
use Carbon\Carbon;
2323
use Illuminate\Auth\Access\AuthorizationException;
2424
use Illuminate\Auth\AuthenticationException;
25-
use Illuminate\Contracts\Validation\Validator;
25+
use Illuminate\Contracts\Translation\Translator;
2626
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
2727
use Illuminate\Session\TokenMismatchException;
2828
use Illuminate\Support\Arr;
2929
use Illuminate\Support\Collection;
3030
use Illuminate\Support\Facades\Route;
3131
use Illuminate\Support\MessageBag;
3232
use Illuminate\Validation\ValidationException;
33+
use Illuminate\Validation\Validator;
3334
use LaravelJsonApi\Core\Exceptions\JsonApiException;
3435
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
3536
use Symfony\Component\HttpKernel\Exception\HttpException;
@@ -445,6 +446,9 @@ public function testValidationException(): void
445446

446447
$validator = $this->createMock(Validator::class);
447448
$validator->method('errors')->willReturn($messages);
449+
$validator->method('getTranslator')->willReturnCallback(
450+
fn() => $this->app->make(Translator::class)
451+
);
448452

449453
$this->ex = new ValidationException($validator);
450454

0 commit comments

Comments
 (0)