Skip to content

Commit 15a54df

Browse files
alxhubmhevery
authored andcommitted
fix(common): accept falsy values as HTTP bodies (angular#19958)
Previously, HttpClient used the overly clever test "body || null" to determine when a body parameter was provided. This breaks when the valid bodies '0' or 'false' are provided. This change tests directly against 'undefined' to detect the presence of the body parameter, and thus correctly allows falsy values through. Fixes angular#19825. Fixes angular#19195. PR Close angular#19958
1 parent eaaae2e commit 15a54df

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

packages/common/http/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export class HttpClient {
371371
}
372372

373373
// Construct the request.
374-
req = new HttpRequest(first, url !, options.body || null, {
374+
req = new HttpRequest(first, url !, (options.body !== undefined ? options.body : null), {
375375
headers,
376376
params,
377377
reportProgress: options.reportProgress,

packages/common/http/src/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class HttpRequest<T> {
171171
// the body argument is to use a known no-body method like GET.
172172
if (mightHaveBody(this.method) || !!fourth) {
173173
// Body is the third argument, options are the fourth.
174-
this.body = third as T || null;
174+
this.body = (third !== undefined) ? third as T : null;
175175
options = fourth;
176176
} else {
177177
// No body required, options are the third argument. The body stays null.

packages/common/http/src/response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class HttpResponse<T> extends HttpResponseBase {
262262
body?: T | null, headers?: HttpHeaders; status?: number; statusText?: string; url?: string;
263263
} = {}) {
264264
super(init);
265-
this.body = init.body || null;
265+
this.body = init.body !== undefined ? init.body : null;
266266
}
267267

268268
readonly type: HttpEventType.Response = HttpEventType.Response;

packages/common/http/test/client_spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ export function main() {
115115
expect(testReq.request.body).toBe(body);
116116
testReq.flush('hello world');
117117
});
118+
it('with a json body of false', (done: DoneFn) => {
119+
client.post('/test', false, {observe: 'response', responseType: 'text'}).subscribe(res => {
120+
expect(res.ok).toBeTruthy();
121+
expect(res.status).toBe(200);
122+
done();
123+
});
124+
const testReq = backend.expectOne('/test');
125+
expect(testReq.request.body).toBe(false);
126+
testReq.flush('hello world');
127+
});
128+
it('with a json body of 0', (done: DoneFn) => {
129+
client.post('/test', 0, {observe: 'response', responseType: 'text'}).subscribe(res => {
130+
expect(res.ok).toBeTruthy();
131+
expect(res.status).toBe(200);
132+
done();
133+
});
134+
const testReq = backend.expectOne('/test');
135+
expect(testReq.request.body).toBe(0);
136+
testReq.flush('hello world');
137+
});
118138
it('with an arraybuffer', (done: DoneFn) => {
119139
const body = new ArrayBuffer(4);
120140
client.post('/test', body, {observe: 'response', responseType: 'text'}).subscribe(res => {

packages/common/http/test/response_spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export function main() {
4040
expect(resp.ok).toBeTruthy();
4141
expect(resp.url).toBeNull();
4242
});
43+
it('accepts a falsy body', () => {
44+
expect(new HttpResponse({body: false}).body).toEqual(false);
45+
expect(new HttpResponse({body: 0}).body).toEqual(0);
46+
});
4347
});
4448
it('.ok is determined by status', () => {
4549
const good = new HttpResponse({status: 200});

0 commit comments

Comments
 (0)