Skip to content

Allow @encode(string) on boolean, define case-insensitive true|false string semantics, and add Spector coverage + generic string matcher support - #10875

Merged
timotheeguerin merged 19 commits into
mainfrom
copilot/relax-encode-for-boolean
Jun 24, 2026
Merged

Allow @encode(string) on boolean, define case-insensitive true|false string semantics, and add Spector coverage + generic string matcher support#10875
timotheeguerin merged 19 commits into
mainfrom
copilot/relax-encode-for-boolean

Conversation

Copilot AI commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

@encode(string) was restricted to numeric targets, blocking APIs that model booleans as strings (for example "true" / "false" in payloads or query values). This change extends support to boolean targets, clarifies the docs contract, and adds http-specs Spector coverage.

  • Compiler validation

    • Relaxed @encode validation so the implicit string encoding path (@encode(string)) is valid for boolean in addition to numeric.
    • Updated related diagnostic wording to reflect numeric-or-boolean applicability.
  • Standard library/docs contract

    • Updated @encode decorator docs to state boolean string encoding uses case-insensitive true / false values.
    • Added a boolean-focused @encode(string) example.
    • Removed parsing-specific wording from the boolean string semantics text.
  • Coverage

    • Added a focused compiler decorator test that validates @encode(string) on a boolean model property.
    • Updated the non-supported-type expectation to match the new allowed target set.
    • Added/updated Spector coverage in packages/http-specs/specs/encode/boolean with matching mock API and regenerated packages/http-specs/spec-summary.md.
    • Boolean property scenarios now cover multiple casings/values via:
      • /encode/boolean/property/true-lower
      • /encode/boolean/property/false-lower
      • /encode/boolean/property/true-upper
      • /encode/boolean/property/false-mixed
    • Updated boolean mock API request validation to use a standard matcher so request boolean-string values are accepted case-insensitively (true/false), while still returning varied response casings for coverage.
  • Spector standard matcher support

    • Added a built-in generic string matcher in @typespec/spec-api: match.string.caseInsensitive(string).
    • Replaced the encode/boolean scenario’s local custom matcher with this shared matcher (match.string.caseInsensitive(String(requestValue))).
    • Added matcher unit tests under packages/spec-api/test/matchers/string.test.ts.
model FeatureFlags {
  @encode(string)
  enabled: boolean;
}

Copilot AI linked an issue Jun 3, 2026 that may be closed by this pull request
3 tasks
Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added compiler:core Issues for @typespec/compiler meta:website TypeSpec.io updates labels Jun 3, 2026
Copilot AI changed the title [WIP] Update encode decorator to support boolean values Allow @encode(string) on boolean and define true|false string semantics Jun 3, 2026
Copilot AI requested a review from timotheeguerin June 3, 2026 16:36
Comment thread website/src/content/docs/docs/standard-library/built-in-decorators.md Outdated
Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
Comment thread packages/compiler/generated-defs/TypeSpec.ts
Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the lib:http-specs For issues/prs related to the @typespec/http-specs package label Jun 3, 2026
Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
Comment thread packages/http-specs/specs/encode/numeric/main.tsp Outdated
Copilot AI and others added 2 commits June 3, 2026 18:06
Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
Copilot AI changed the title Allow @encode(string) on boolean and define true|false string semantics Allow @encode(string) on boolean, define true|false string semantics, and add Spector coverage Jun 3, 2026
Copilot AI requested a review from timotheeguerin June 3, 2026 18:09
Comment thread packages/http-specs/specs/encode/boolean/main.tsp Outdated
Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>

Copilot AI commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@copilot need to regenerate the definitions

git status --porcelain
ERROR: Files above were changed during PR validation, but not included in the PR.
...

Fixed in f095d13: I regenerated compiler definitions (pnpm gen-compiler-extern-signature) and committed the updated packages/compiler/generated-defs/TypeSpec.ts. I also ran package builds during validation (@typespec/compiler, @typespec/spec-api, and repository pnpm build).

Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>

Copilot AI commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@copilot formatting also failing run formatter

Fixed in 7d5c391. I ran the formatter and committed the formatting updates for the two files flagged by CI; pnpm run format:check now passes.

@timotheeguerin

Copy link
Copy Markdown
Member

@copilot add changelogs

Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>

Copilot AI commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@copilot add changelogs

Fixed in c58089f. I added the Chronus changelog entry covering @typespec/compiler, @typespec/spec-api, and @typespec/http-specs, and chronus verify --since main now passes.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the stale Mark a PR that hasn't been recently updated and will be closed. label Jun 23, 2026
@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/compiler
  • @typespec/http-specs
  • @typespec/spec-api
Show changes

@typespec/compiler - feature ✏️

Allow @encode(string) on boolean targets, define case-insensitive true/false string semantics, and add shared case-insensitive string matcher support with encode/boolean Spector coverage.,> ,> tsp,> model FeatureFlags {,> @encode(string),> enabled: boolean;,> },>

@typespec/spec-api - feature ✏️

Allow @encode(string) on boolean targets, define case-insensitive true/false string semantics, and add shared case-insensitive string matcher support with encode/boolean Spector coverage.,> ,> tsp,> model FeatureFlags {,> @encode(string),> enabled: boolean;,> },>

@typespec/http-specs - feature ✏️

Allow @encode(string) on boolean targets, define case-insensitive true/false string semantics, and add shared case-insensitive string matcher support with encode/boolean Spector coverage.,> ,> tsp,> model FeatureFlags {,> @encode(string),> enabled: boolean;,> },>

@timotheeguerin

Copy link
Copy Markdown
Member

@copilot need to regen docs

website/src/content/docs/docs/standard-library/built-in-decorators.md

shows as modified.

Also show an example of usage in the changelog entry as per comment

Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>

Copilot AI commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

@copilot need to regen docs

website/src/content/docs/docs/standard-library/built-in-decorators.md

...

Done in da8cac9. I regenerated the docs so website/src/content/docs/docs/standard-library/built-in-decorators.md is included, and I updated the changelog entry to include a TypeSpec usage example.

@timotheeguerin
timotheeguerin added this pull request to the merge queue Jun 24, 2026
Merged via the queue into main with commit 85ff828 Jun 24, 2026
28 checks passed
@timotheeguerin
timotheeguerin deleted the copilot/relax-encode-for-boolean branch June 24, 2026 18:23
live1206 pushed a commit to live1206/typespec that referenced this pull request Jul 16, 2026
… fix boolean-as-string encoding (microsoft#11087)

Adds Python SDK mock API test coverage for the `encode/boolean` Spector
scenarios from microsoft#10875 (`@encode(string)` on boolean types). This
required updating multiple layers of the Python codegen stack.

## Generator fixes

**`types.ts` — property emission**  
Preserves boolean `@encode(string)` metadata from the compiler and maps
it to the Python runtime’s `"str"` format token.

**`primitive_types.py` — `BooleanType`**  
Detects `yaml_data["encode"] == "string"` and sets `self.encode =
"str"`, mirroring `IntegerType`.

**`model_base.py.jinja2`**  
Adds `_deserialize_bool_as_str` for case-insensitive string-to-boolean
deserialization:

```python
def _deserialize_bool_as_str(attr: str) -> bool:
    return attr.lower() == "true"
```

Also dispatches to it when `annotation is bool and rf._format == "str"`.

## Test coverage

Adds sync and async tests for Azure and unbranded clients covering:

- `trueLower` — `"true"` → `True`
- `falseLower` — `"false"` → `False`
- `trueUpper` — `"TRUE"` → `True`
- `falseMixed` — `"FaLsE"` → `False`

## Dependencies

- Uses `@typespec/compiler` 1.14, which supports boolean
`@encode(string)`.
- Uses `@typespec/http-specs` `0.1.0-alpha.39`, containing the
`encode/boolean` scenarios.
- Adds `encode/boolean` to `regenerate-common.ts`.

## Code diff:
Azure/azure-sdk-for-python#48067

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Co-authored-by: Yuchao Yan <yuchaoyan@microsoft.com>
@weidongxu-microsoft

weidongxu-microsoft commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Any service uses it?

When asking agent to impl in Java emitter (thought it be an easy fix, but...), it instead sees TCGC support is not yet ready. If this is true, TCGC require some work first.
Azure/typespec-azure#4961

<-- never mind, TCGC is doing a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:core Issues for @typespec/compiler lib:http-specs For issues/prs related to the @typespec/http-specs package meta:website TypeSpec.io updates stale Mark a PR that hasn't been recently updated and will be closed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Encode decorator applicable to boolean

4 participants