Skip to content

Commit 54e2fd7

Browse files
committed
fix: attach cache metadata to JSON responses
1 parent 7de1424 commit 54e2fd7

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/Controller/PathResolverController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function resolve(Request $request): CacheableJsonResponse {
6969
$cacheable->addCacheContexts(['languages:language_content']);
7070
}
7171

72-
$cacheable->applyTo($response);
72+
$response->addCacheableDependency($cacheable);
7373

7474
$this->applySecurityHeaders($response, $max_age);
7575

@@ -94,7 +94,7 @@ private function errorResponse(int $status, string $title, string $detail): Cach
9494

9595
$cacheable = new CacheableMetadata();
9696
$cacheable->setCacheMaxAge(0);
97-
$cacheable->applyTo($response);
97+
$response->addCacheableDependency($cacheable);
9898
$this->applySecurityHeaders($response, 0);
9999

100100
return $response;

src/Controller/RoutesFeedController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function routes(Request $request): CacheableJsonResponse {
119119
$cacheable = new CacheableMetadata();
120120
$cacheable->setCacheMaxAge(0);
121121
$cacheable->addCacheTags(['config:jsonapi_frontend.settings']);
122-
$cacheable->applyTo($response);
122+
$response->addCacheableDependency($cacheable);
123123

124124
$this->applySecurityHeaders($response);
125125

@@ -144,7 +144,7 @@ private function errorResponse(int $status, string $title, string $detail): Cach
144144

145145
$cacheable = new CacheableMetadata();
146146
$cacheable->setCacheMaxAge(0);
147-
$cacheable->applyTo($response);
147+
$response->addCacheableDependency($cacheable);
148148
$this->applySecurityHeaders($response);
149149

150150
return $response;

tests/src/Kernel/PathResolutionTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Drupal\node\Entity\Node;
1010
use Drupal\node\Entity\NodeType;
1111
use Drupal\user\Entity\User;
12+
use Symfony\Component\HttpFoundation\Request;
1213

1314
/**
1415
* Kernel tests for path resolution functionality.
@@ -195,4 +196,31 @@ public function testResolveInternalPath(): void {
195196
$this->assertEquals($node->uuid(), $result['entity']['id']);
196197
}
197198

199+
/**
200+
* Tests the /jsonapi/resolve controller response.
201+
*/
202+
public function testResolveControllerResponse(): void {
203+
$node = Node::create([
204+
'type' => 'page',
205+
'title' => 'About Us',
206+
'status' => 1,
207+
'path' => ['alias' => '/about-us'],
208+
]);
209+
$node->save();
210+
211+
$controller = \Drupal\jsonapi_frontend\Controller\PathResolverController::create($this->container);
212+
$request = Request::create('/jsonapi/resolve', 'GET', [
213+
'path' => '/about-us',
214+
'_format' => 'json',
215+
]);
216+
217+
$response = $controller->resolve($request);
218+
$payload = json_decode((string) $response->getContent(), TRUE);
219+
220+
$this->assertIsArray($payload);
221+
$this->assertTrue($payload['resolved']);
222+
$this->assertEquals('entity', $payload['kind']);
223+
$this->assertEquals($node->uuid(), $payload['entity']['id']);
224+
}
225+
198226
}

0 commit comments

Comments
 (0)