Use Symfony's built-in ETag handling for HTTP caching

Replace manual If-None-Match comparison with Response::setEtag() and
Response::isNotModified(), which properly handles ETag quoting, weak
vs strong comparison, and 304 response cleanup. Fixes PHPStan return
type error and CI test failures.
This commit is contained in:
Sebastian Almberg 2026-02-08 15:35:49 +01:00
parent 185107f4e1
commit 02869f2aa7

View file

@ -92,17 +92,12 @@ class KiCadApiController extends AbstractController
* Creates a JSON response with HTTP cache headers (ETag and Cache-Control). * Creates a JSON response with HTTP cache headers (ETag and Cache-Control).
* Returns 304 Not Modified if the client's ETag matches. * Returns 304 Not Modified if the client's ETag matches.
*/ */
private function createCachedJsonResponse(Request $request, array $data, int $maxAge): JsonResponse private function createCachedJsonResponse(Request $request, array $data, int $maxAge): Response
{ {
$etag = '"' . md5(json_encode($data)) . '"';
if ($request->headers->get('If-None-Match') === $etag) {
return new Response('', Response::HTTP_NOT_MODIFIED);
}
$response = new JsonResponse($data); $response = new JsonResponse($data);
$response->setEtag(md5(json_encode($data)));
$response->headers->set('Cache-Control', 'private, max-age=' . $maxAge); $response->headers->set('Cache-Control', 'private, max-age=' . $maxAge);
$response->headers->set('ETag', $etag); $response->isNotModified($request);
return $response; return $response;
} }