From 468a076cd9a2fe2aa365527fde29f4bdb6682c52 Mon Sep 17 00:00:00 2001 From: soyuka Date: Sat, 4 Jul 2026 10:55:27 +0200 Subject: [PATCH] fix(hydra): send default Accept header when options is an empty object setHeaders() short-circuited on falsy options.headers, returning a plain {} instead of building Headers with the Accept default. This broke @api-platform/admin's schemaAnalyzer, which always calls getParameters() with an explicit {} (see #177). Removed the early return so the function always falls through to the Headers/Accept logic, and swapped the headers derivation to avoid mutating the caller's options object as a side effect. --- src/hydra/fetchJsonLd.test.ts | 34 ++++++++++++++++++++++++++++++++++ src/hydra/fetchJsonLd.ts | 8 +++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/hydra/fetchJsonLd.test.ts b/src/hydra/fetchJsonLd.test.ts index 68c168e..ce9f4f7 100644 --- a/src/hydra/fetchJsonLd.test.ts +++ b/src/hydra/fetchJsonLd.test.ts @@ -154,6 +154,40 @@ test("fetch an error with Content-Type application/error+json", async () => { ); }); +test("fetch with an explicit empty options object still sends the default Accept header", async () => { + let receivedAccept: string | null = null; + + server.use( + http.get("http://localhost/foo.jsonld", ({ request }) => { + receivedAccept = request.headers.get("Accept"); + return Response.json(httpResponse, { + headers: { "Content-Type": "application/ld+json" }, + status: 200, + statusText: "OK", + }); + }), + ); + + await fetchJsonLd("http://localhost/foo.jsonld", {}); + expect(receivedAccept).toBe("application/ld+json"); +}); + +test("fetch does not mutate the passed options object", async () => { + server.use( + http.get("http://localhost/foo.jsonld", () => + Response.json(httpResponse, { + headers: { "Content-Type": "application/ld+json" }, + status: 200, + statusText: "OK", + }), + ), + ); + + const options = {}; + await fetchJsonLd("http://localhost/foo.jsonld", options); + expect(options).toEqual({}); +}); + test("fetch an empty document", async () => { server.use( http.get( diff --git a/src/hydra/fetchJsonLd.ts b/src/hydra/fetchJsonLd.ts index 99be05c..349e707 100644 --- a/src/hydra/fetchJsonLd.ts +++ b/src/hydra/fetchJsonLd.ts @@ -61,12 +61,10 @@ export default async function fetchJsonLd( } function setHeaders(options: RequestInitExtended): RequestInit { - if (!options.headers) { - return { ...options, headers: {} }; - } - let headers = - typeof options.headers === "function" ? options.headers() : options.headers; + typeof options.headers === "function" + ? options.headers() + : (options.headers ?? {}); headers = new Headers(headers);