Azure / Azure/typespec-azure

[typespec-ts] Boolean properties annotated with @encode(string) are serialized as JSON booleans

Open
#5,296 0 comments 0 reactions 0 assignees View on GitHub
emitter:typescript
Dominant language
TypeScript
Stars
27
Forks
90
Avg merge
1d 22h
Merged PRs (30d)
156

Description

## Description

The TypeScript emitter generates a compilable client for boolean values annotated with `@encode(string)`, but it serializes the public boolean value as a JSON boolean instead of the required wire string.

## Spector service

- Spec: https://github.com/microsoft/typespec/tree/main/packages/http-specs/specs/encode/boolean
- Scenarios:
- `Encode_Boolean_Property_trueLower`
- `Encode_Boolean_Property_falseLower`
- `Encode_Boolean_Property_trueUpper`
- `Encode_Boolean_Property_falseMixed`

## Reproduction

```ts
const client = new BooleanClient({
endpoint: "http://localhost:3002",
allowInsecureConnection: true,
});

await client.trueLower({ value: true });
```

## Actual behavior

```text
RestError: Body provided doesn't match expected body: at $.value: expected a string but got boolean
statusCode: 400
request body: {"value":true}
```

The generated serializer treats the property as an ordinary boolean and does not apply the `@encode(string)` wire conversion. As a result, it sends:

```json
{ "value": true }
```

instead of:

```json
{ "value": "true" }
```

The same missing encoding logic affects the other supported boolean string representations. The response path must perform the inverse conversion as well so the public API continues to expose a boolean.

## Expected behavior

Request serialization should convert the public boolean to the string representation specified by `@encode(string)`. Response deserialization should convert the wire string back to the public boolean value.

Contributor guide

Open the contributing guide

Research direction

Start with the TypeScript emitter's generated serializer and deserializer for the BooleanClient reproduction, then run the four Spector scenarios in packages/http-specs/specs/encode/boolean: trueLower, falseLower, trueUpper, and falseMixed. Done means requests send the encoded wire strings and responses convert them back to public booleans for every listed representation.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.