Skip to content

fix(typespec-ts): make read-only request model properties optional - #5622

Open
Glenn Harper (glharper) wants to merge 1 commit into
Azure:mainfrom
glharper:fix/nested-readonly-model-properties
Open

Glenn Harper (glharper) wants to merge 1 commit into
Azure:mainfrom
glharper:fix/nested-readonly-model-properties

Conversation

@glharper

Copy link
Copy Markdown
Member

Fixes #5621.

Summary

The default emitter shares model interfaces between requests and responses, but currently emits required @visibility(Lifecycle.Read) properties as required TypeScript fields even on request-used models. This forces callers to supply fields that the request serializers omit, including fields on nested models.

Use one optionality helper for named interfaces and inline model expressions. Read-only properties become optional only when the containing model has UsageFlags.Input and experimental visibility splitting is disabled. Writable required properties, response-only models, and the opt-in split-model response contracts remain unchanged. The SDK model metadata is not mutated, preserving serializer/deserializer behavior and nullable response values.

Add regression coverage for the reported create-body example, arrays, dictionaries, nullable/union nesting, inheritance, cycles, inline models, response-only contracts, and visibility splitting. The reproduction also type-checks the generated TypeScript with a create payload that omits both top-level and nested read-only fields. Update two directly affected inherited/flattened model snapshots and add a package bug-fix change entry.

Validation

The regression tests were run against unmodified main first: six input/nesting assertions failed because read-only fields were required; the two preservation assertions passed. The inline-model regression separately failed before that emission path was fixed.

  • pnpm -r --filter "@azure-tools/typespec-ts..." build — passed.
  • pnpm --filter @azure-tools/typespec-ts build — passed after the fix.
  • pnpm exec vitest run --project unit-modular --maxWorkers=4 (from packages/typespec-ts) — passed: 744 tests in 49 files, including all ten new regression tests.
  • pnpm test-next (from packages/typespec-ts) — passed: 268 tests in 17 files.
  • pnpm format — passed.
  • pnpm lint — passed.
  • pnpm exec prettier --check .chronus/changes/typespec-ts-optional-readonly-input.md packages/typespec-ts/src/modular/emit-models.ts packages/typespec-ts/src/modular/helpers/visibility-helpers.ts packages/typespec-ts/src/modular/type-expressions/get-model-expression.ts packages/typespec-ts/test/modular-unit/readonly-properties.test.ts packages/typespec-ts/test/modular-unit/scenarios/models/property-flatten/singleLayer.md — passed.

Spector integration tests and the unrelated full-workspace test/build suites were not run.

Fixes Azure#5621.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 40bd2bb4-54cf-4b12-b492-b278172dd8c7
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • ✅ @azure-tools/typespec-ts
Show changes

@azure-tools/typespec-ts - fix ✏️

Make read-only properties optional on shared request models, including nested and inline models, while preserving required response-only and visibility-split properties.

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

📦 Package size report

✅ No notable package size changes compared to the base branch.

14 package(s) with no notable change
Package Packed (base → head) Δ Packed Unpacked (base → head) Δ Unpacked
@azure-tools/typespec-ts 540.85 KB → 541.32 KB +484 B (+0.1%) 2.60 MB → 2.60 MB +2.49 KB (+0.1%)
@azure-tools/typespec-java 13.51 MB → 13.51 MB +204 B (+0.0%) 15.04 MB → 15.04 MB —
@azure-tools/azure-http-specs 148.36 KB → 148.36 KB — 1.17 MB → 1.17 MB —
@azure-tools/typespec-autorest 81.98 KB → 81.98 KB — 400.59 KB → 400.59 KB —
@azure-tools/typespec-autorest-canonical 7.42 KB → 7.42 KB — 26.00 KB → 26.00 KB —
@azure-tools/typespec-azure-core 134.19 KB → 134.19 KB — 724.18 KB → 724.18 KB —
@azure-tools/typespec-azure-examples 57.94 KB → 57.94 KB — 223.83 KB → 223.83 KB —
@azure-tools/typespec-azure-portal-core 42.48 KB → 42.48 KB — 193.20 KB → 193.20 KB —
@azure-tools/typespec-azure-resource-manager 184.05 KB → 184.05 KB — 1.11 MB → 1.11 MB —
@azure-tools/typespec-azure-rulesets 5.50 KB → 5.50 KB — 33.67 KB → 33.67 KB —
@azure-tools/typespec-client-generator-core 240.28 KB → 240.28 KB — 1.28 MB → 1.28 MB —
@azure-tools/typespec-go 263.56 KB → 263.56 KB — 1.35 MB → 1.35 MB —
@azure-tools/typespec-metadata 15.91 KB → 15.91 KB — 62.26 KB → 62.26 KB —
@azure-tools/typespec-python 42.22 KB → 42.22 KB — 164.91 KB → 164.91 KB —

Packed = gzipped .tgz published to npm. Unpacked = total extracted size. 🆕 added, 🗑️ removed. Packages from the core/ submodule are not included.
🔴 grew · 🟢 shrank — only changes of at least 512 B and 0.5% are marked.

@pkg-pr-new

pkg-pr-new Bot commented Oct 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@azure-tools/typespec-ts@5622

commit: ca11cd1

@azure-sdk-automation

Copy link
Copy Markdown
Contributor

You can try these changes here

🛝 Playground 🌐 Website

This branch has not been deployed

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

Labels

emitter:typescript Issues for @azure-tools/typespec-ts emitter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[typespec-ts] Read-only fields remain required in nested request models

1 participant