Skip to content

Preserve accessible serialization constructors during back compat - #11798

Merged
JoshLove-msft merged 4 commits into
microsoft:mainfrom
JoshLove-msft:josh/csharp-preserve-serialization-ctor
Sep 1, 2026
Merged

Preserve accessible serialization constructors during back compat#11798
JoshLove-msft merged 4 commits into
microsoft:mainfrom
JoshLove-msft:josh/csharp-preserve-serialization-ctor

Conversation

@JoshLove-msft

Copy link
Copy Markdown
Contributor

Summary

  • Preserve an accessible parameterless constructor that already exists on a generated serialization partial during API compatibility processing.
  • Avoid removing a visitor-customized constructor and synthesizing a replacement that delegates to a different overload.
  • Cover both the accessible-constructor preservation path and the existing internal-constructor replacement path.

This addresses the constructor churn observed in openai/openai-dotnet#1341 when ApiCompatVersion is enabled.

Validation

  • npm run build
  • eng/scripts/Generate.ps1
  • npm test
  • npm run cop
  • Focused constructor compatibility tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 00636c74-c4f7-434a-8a70-fb0c3d221328
Copilot AI lite review requested due to automatic review settings September 1, 2026 01:20
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Sep 1, 2026
JoshLove-msft and others added 2 commits August 31, 2026 18:23
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 00636c74-c4f7-434a-8a70-fb0c3d221328
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 00636c74-c4f7-434a-8a70-fb0c3d221328
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

No changes needing a change description found.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refines the C# generator’s API-compat constructor restoration logic so that back-compat processing does not introduce a duplicate parameterless constructor when one already exists on a generated serialization partial (including cases where a visitor has made that constructor public). This directly targets the constructor churn scenario seen when ApiCompatVersion is enabled.

Changes:

  • Update model back-compat constructor restoration to treat an accessible parameterless constructor on any serialization provider partial as already satisfying the contract.
  • Add focused ClientModel tests covering:
    • preserving an accessible parameterless serialization constructor (including visitor-customized routing),
    • retaining the existing behavior of replacing an inaccessible parameterless serialization constructor by restoring one on the model partial.
  • Update backward-compat documentation to clarify the “already exists” condition and the mocking-constructor removal behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelProvider.cs Avoid restoring a parameterless constructor onto the model partial when an accessible parameterless constructor already exists on a serialization partial.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmModelProvider/TestData/ScmModelProviderTests/BackCompat_AccessibleParameterlessSerializationConstructorIsPreserved/MockInputModel.cs Adds last-contract custom code fixture defining a published public parameterless constructor.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmModelProvider/ScmModelProviderTests.cs Adds/extends tests validating preservation vs. replacement behavior for parameterless serialization constructors under back-compat.
packages/http-client-csharp/generator/docs/backward-compatibility.md Clarifies docs for parameterless-constructor restoration eligibility and mocking-constructor removal semantics.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI review requested due to automatic review settings September 1, 2026 01:26
@pkg-pr-new

pkg-pr-new Bot commented Sep 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@11798

commit: b39dc3e

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 1, 2026 16:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@JoshLove-msft
JoshLove-msft added this pull request to the merge queue Sep 1, 2026
Merged via the queue into microsoft:main with commit b6684e0 Sep 1, 2026
29 checks passed
@JoshLove-msft
JoshLove-msft deleted the josh/csharp-preserve-serialization-ctor branch September 1, 2026 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants