Skip to content

Commit

Permalink
fix returning null from endpoints
Browse files Browse the repository at this point in the history
A recent change [1] introduced a bug which converts `null` to `{}`
(empty object) when returned from endpoints. This commit explicitly
checks for `undefined` and does not modify `null` responses.

[1]: sveltejs#1808
  • Loading branch information
utkarshkukreti committed Jul 12, 2021
1 parent f116f36 commit 9f5da5a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default async function render_route(request, route) {
(!type || type.startsWith('application/json'))
) {
headers = { ...headers, 'content-type': 'application/json; charset=utf-8' };
normalized_body = JSON.stringify(body || {});
normalized_body = JSON.stringify(typeof body === 'undefined' ? {} : body);
} else {
normalized_body = /** @type {import('types/hooks').StrictBody} */ (body);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ export default function (test) {
const res = await fetch('/endpoint-output/empty');
assert.type(await res.buffer(), 'object');
});

test('null body returns null json value', null, async ({ fetch }) => {
const res = await fetch('/endpoint-output/null');
assert.equal(res.status, 200);
assert.equal(await res.json(), null);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
return { body: null };
}

0 comments on commit 9f5da5a

Please sign in to comment.