microsoft / microsoft/typespec
[Bug]: http-client-js URI template expand keys don't match variable names for bracket @query params
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
### Describe the bug
When a TypeSpec operation uses bracket notation in `@query` (common for JSON:API style APIs), the `http-client-js` emitter generates a URI template with percent-encoded variable names (`filter%5BisActive%5D`) but uses camelCase property names (`filterIsActive`) as the expand object keys. Since these don't match, `uri-template`'s `parse().expand()` silently drops the query parameters from the URL.
### Reproduction
TypeSpec definition:
```typespec
model ClientFilterParams {
@query("filter[isActive]") filterIsActive?: boolean;
@query("page[number]") pageNumber?: int32;
}
```
Generated code (`clientsClientOperations.ts`):
```typescript
const path = parse("/api/v1/clients{?filter%5BisActive%5D,page%5Bnumber%5D}").expand({
...(options?.filterIsActive && { filterIsActive: options.filterIsActive }),
...(options?.pageNumber && { pageNumber: options.pageNumber }),
});
```
The template variable names are `filter%5BisActive%5D` and `page%5Bnumber%5D`, but the expand keys are `filterIsActive` and `pageNumber`. The `uri-template` library requires these to match.
**Expected:** expand keys should use the encoded names:
```typescript
const path = parse("/api/v1/clients{?filter%5BisActive%5D,page%5Bnumber%5D}").expand({
...(options?.filterIsActive && { "filter%5BisActive%5D": options.filterIsActive }),
...(options?.pageNumber && { "page%5Bnumber%5D": options.pageNumber }),
});
```
**Result:** Query parameters like `filter[isActive]=true` and `page[number]=1` are silently dropped from the URL.
**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
Assessment
This issue has not been assessed yet.