Skip to content

Commit 468a076

Browse files
committed
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.
1 parent d40a846 commit 468a076

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

src/hydra/fetchJsonLd.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,40 @@ test("fetch an error with Content-Type application/error+json", async () => {
154154
);
155155
});
156156

157+
test("fetch with an explicit empty options object still sends the default Accept header", async () => {
158+
let receivedAccept: string | null = null;
159+
160+
server.use(
161+
http.get("http://localhost/foo.jsonld", ({ request }) => {
162+
receivedAccept = request.headers.get("Accept");
163+
return Response.json(httpResponse, {
164+
headers: { "Content-Type": "application/ld+json" },
165+
status: 200,
166+
statusText: "OK",
167+
});
168+
}),
169+
);
170+
171+
await fetchJsonLd("http://localhost/foo.jsonld", {});
172+
expect(receivedAccept).toBe("application/ld+json");
173+
});
174+
175+
test("fetch does not mutate the passed options object", async () => {
176+
server.use(
177+
http.get("http://localhost/foo.jsonld", () =>
178+
Response.json(httpResponse, {
179+
headers: { "Content-Type": "application/ld+json" },
180+
status: 200,
181+
statusText: "OK",
182+
}),
183+
),
184+
);
185+
186+
const options = {};
187+
await fetchJsonLd("http://localhost/foo.jsonld", options);
188+
expect(options).toEqual({});
189+
});
190+
157191
test("fetch an empty document", async () => {
158192
server.use(
159193
http.get(

src/hydra/fetchJsonLd.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,10 @@ export default async function fetchJsonLd(
6161
}
6262

6363
function setHeaders(options: RequestInitExtended): RequestInit {
64-
if (!options.headers) {
65-
return { ...options, headers: {} };
66-
}
67-
6864
let headers =
69-
typeof options.headers === "function" ? options.headers() : options.headers;
65+
typeof options.headers === "function"
66+
? options.headers()
67+
: (options.headers ?? {});
7068

7169
headers = new Headers(headers);
7270

0 commit comments

Comments
 (0)