Skip to content

Commit bb503bd

Browse files
jasonvargaclaude
andauthored
[6.x] Fix serializable_classes issues (#14443)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f320e86 commit bb503bd

File tree

6 files changed

+54
-18
lines changed

6 files changed

+54
-18
lines changed

src/API/Cachers/DefaultCacher.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ class DefaultCacher extends AbstractCacher
1515
*/
1616
public function get(Request $request)
1717
{
18-
return Cache::get($this->getCacheKey($request));
18+
$cached = Cache::get($this->getCacheKey($request));
19+
20+
if (! is_array($cached)) {
21+
return null;
22+
}
23+
24+
return new JsonResponse($cached['content'], $cached['status'], $cached['headers'], json: true);
1925
}
2026

2127
/**
@@ -25,7 +31,11 @@ public function put(Request $request, JsonResponse $response)
2531
{
2632
$key = $this->trackEndpoint($request);
2733

28-
Cache::put($key, $response, $this->cacheExpiry());
34+
Cache::put($key, [
35+
'content' => $response->getContent(),
36+
'status' => $response->getStatusCode(),
37+
'headers' => $response->headers->all(),
38+
], $this->cacheExpiry());
2939
}
3040

3141
/**

src/GraphQL/ResponseCache/DefaultCache.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Statamic\GraphQL\ResponseCache;
44

5+
use Illuminate\Http\JsonResponse;
56
use Illuminate\Http\Request;
67
use Illuminate\Support\Carbon;
78
use Illuminate\Support\Facades\Cache;
@@ -12,7 +13,13 @@ class DefaultCache implements ResponseCache
1213
{
1314
public function get(Request $request)
1415
{
15-
return Cache::get($this->getCacheKey($request));
16+
$cached = Cache::get($this->getCacheKey($request));
17+
18+
if (! is_array($cached)) {
19+
return null;
20+
}
21+
22+
return new JsonResponse($cached['content'], $cached['status'], $cached['headers'], json: true);
1623
}
1724

1825
public function put(Request $request, $response)
@@ -21,7 +28,11 @@ public function put(Request $request, $response)
2128

2229
$ttl = Carbon::now()->addMinutes((int) config('statamic.graphql.cache.expiry', 60));
2330

24-
Cache::put($key, $response, $ttl);
31+
Cache::put($key, [
32+
'content' => $response->getContent(),
33+
'status' => $response->getStatusCode(),
34+
'headers' => $response->headers->all(),
35+
], $ttl);
2536
}
2637

2738
protected function track($request)

src/Providers/AddonServiceProvider.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -721,14 +721,11 @@ protected function registerSerializableClasses(array $classes)
721721
{
722722
$existing = $this->app['config']->get('cache.serializable_classes');
723723

724-
if ($existing === true) {
724+
if ($existing === null || $existing === true) {
725725
return;
726726
}
727727

728-
$this->app['config']->set('cache.serializable_classes', array_merge(
729-
is_array($existing) ? $existing : [],
730-
$classes
731-
));
728+
$this->app['config']->set('cache.serializable_classes', array_merge(is_array($existing) ? $existing : [], $classes));
732729
}
733730

734731
protected function schedule(Schedule $schedule)

src/Providers/AppServiceProvider.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,14 @@ private function registerSerializableClasses()
239239
{
240240
$existing = $this->app['config']->get('cache.serializable_classes');
241241

242-
if ($existing === true) {
242+
if ($existing === null || $existing === true) {
243243
return;
244244
}
245245

246-
$classes = [
246+
$this->app['config']->set('cache.serializable_classes', array_merge(is_array($existing) ? $existing : [], [
247247
\Statamic\Auth\File\User::class,
248+
\Statamic\Auth\File\Passkey::class,
249+
\Statamic\Auth\Eloquent\Passkey::class,
248250
\Statamic\Assets\Asset::class,
249251
\Statamic\Assets\AssetContainer::class,
250252
\Statamic\Entries\Collection::class,
@@ -264,12 +266,7 @@ private function registerSerializableClasses()
264266
\Carbon\Carbon::class,
265267
\Illuminate\Support\Carbon::class,
266268
\Illuminate\Support\Collection::class,
267-
];
268-
269-
$this->app['config']->set('cache.serializable_classes', array_merge(
270-
is_array($existing) ? $existing : [],
271-
$classes
272-
));
269+
]));
273270
}
274271

275272
protected function registerMiddlewareGroup()

tests/API/CacherTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public function it_caches_endpoint_using_default_cacher()
5757
$this->assertEquals([$cacheKey], Cache::get('api-cache:tracked-responses'));
5858

5959
// manually manipulate whats in the cache so we can be sure it uses it.
60-
Cache::put($cacheKey, response()->json(['foo' => 'bar']), 10);
60+
Cache::put($cacheKey, [
61+
'content' => json_encode(['foo' => 'bar']),
62+
'status' => 200,
63+
'headers' => ['content-type' => ['application/json']],
64+
], 10);
6165

6266
$this->get($endpoint)
6367
->assertOk()

tests/Feature/GraphQL/RequestCacheTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ public function it_ignores_configured_events()
210210
], $requests->all());
211211
}
212212

213+
#[Test]
214+
public function it_caches_response_data_as_primitives()
215+
{
216+
app()->instance('request-tracking', $requests = Collection::make());
217+
218+
$this->post('/graphql', ['query' => '{one}']);
219+
220+
$key = 'gql-cache:'.md5('{one}').'_'.md5(json_encode(null));
221+
$cached = Cache::get($key);
222+
223+
$this->assertIsArray($cached);
224+
$this->assertArrayHasKey('content', $cached);
225+
$this->assertArrayHasKey('status', $cached);
226+
$this->assertArrayHasKey('headers', $cached);
227+
$this->assertEquals(200, $cached['status']);
228+
}
229+
213230
#[Test]
214231
public function it_can_disable_caching()
215232
{

0 commit comments

Comments
 (0)