microsoft / microsoft/typespec

[Bug]: http-client-js expand guards use truthy check, dropping falsy values like false and 0

Open
#10,264 0 comments 1 reaction 0 assignees View on GitHub
emitter:client:js
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Describe the bug

The `http-client-js` emitter generates truthy guards (`&&`) for URI template expand keys. This means falsy values like `false`, `0`, and `""` are silently dropped from the query string.

### Reproduction

TypeSpec definition:

```typespec
model ListParams {
@query isActive?: boolean;
@query page?: int32;
}
```

Generated code:

```typescript
...(options?.isActive && { isActive: options.isActive }),
...(options?.page && { page: options.page }),
```

Calling with `{ isActive: false }` or `{ page: 0 }` drops the param entirely because `false && { ... }` and `0 && { ... }` short-circuit.

**Expected:** Use nullish checks instead of truthy checks:

```typescript
...(options?.isActive != undefined && { isActive: options.isActive }),
...(options?.page != undefined && { page: options.page }),
```

**Versions:** `@typespec/http-client-js@0.14.1`, `@typespec/compiler@1.10.0`

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/Microsoft/typespec/discussions).
- [x] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.