Skip to content

Commit 91f93e0

Browse files
authored
fix(laravel): set application/ld+json content-type on /contexts/{shortName} (#7973)
1 parent dcef827 commit 91f93e0

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/Laravel/Tests/JsonLdTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,30 @@ public function testApiDocsRegex(): void
304304
$response->assertNotFound();
305305
}
306306

307+
public function testJsonLdContextHasCorrectContentType(): void
308+
{
309+
$response = $this->get('/api/contexts/Entrypoint', ['accept' => 'application/ld+json']);
310+
$response->assertStatus(200);
311+
$response->assertHeader('content-type', 'application/ld+json; charset=utf-8');
312+
$this->assertArrayHasKey('@context', $response->json());
313+
}
314+
315+
public function testJsonLdResourceContextHasCorrectContentType(): void
316+
{
317+
$response = $this->get('/api/contexts/Book', ['accept' => 'application/ld+json']);
318+
$response->assertStatus(200);
319+
$response->assertHeader('content-type', 'application/ld+json; charset=utf-8');
320+
$this->assertArrayHasKey('@context', $response->json());
321+
}
322+
323+
public function testJsonLdContextDefaultsToEntrypoint(): void
324+
{
325+
$response = $this->get('/api/contexts/', ['accept' => 'application/ld+json']);
326+
$response->assertStatus(200);
327+
$response->assertHeader('content-type', 'application/ld+json; charset=utf-8');
328+
$this->assertArrayHasKey('@context', $response->json());
329+
}
330+
307331
public function testHidden(): void
308332
{
309333
PostFactory::new()->has(CommentFactory::new()->count(10))->count(10)->create();

src/Laravel/routes/api.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@
8080

8181
Route::group(['prefix' => $prefix], static function (): void {
8282
Route::group(['middleware' => ApiPlatformMiddleware::class], static function (): void {
83-
Route::get('/contexts/{shortName?}{_format?}', ContextAction::class)
84-
->name('api_jsonld_context');
83+
Route::get('/contexts/{shortName?}{_format?}', static function (Request $request, ContextAction $contextAction, string $shortName = 'Entrypoint') {
84+
return $contextAction($shortName, $request);
85+
})->name('api_jsonld_context');
8586

8687
Route::get('/validation_errors/{id}', static fn () => throw new NotExposedHttpException('Not exposed.'))
8788
->name('api_validation_errors')

0 commit comments

Comments
 (0)