Skip to content

Support back-compat model constructors in the C# generator - #11503

Merged
jorgerangel-msft merged 20 commits into
mainfrom
copilot/lastcontractview-back-compat-ctors
Aug 5, 2026
Merged

Support back-compat model constructors in the C# generator#11503
jorgerangel-msft merged 20 commits into
mainfrom
copilot/lastcontractview-back-compat-ctors

Conversation

Copilot AI commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

When a previously-required model property is relaxed to optional, the generator drops the corresponding parameter from the model's initialization constructor — a source-breaking change for callers that construct the model positionally (issue #11460). This restores the previously-published public constructor via the LastContractView back-compat mechanism.

Changes

  • ModelProvider.BuildConstructorsForBackCompatibility (new override) — For each public constructor in LastContractView that has no current equivalent, reconstructs it as a public overload that chains to the closest current public constructor (: this(...)) and assigns the extra properties in its body.
    • Chain target is the current public constructor whose parameters form an in-order subsequence of the previous ones (closest match preferred).
    • Each dropped parameter must map to a settable property with an unchanged type — matched by property name, or by OriginalName for codegen renames. If any dropped parameter cannot be mapped, restoration is skipped.
    • Kept parameters forward to the chained constructor (validation stripped, since the target validates); non-nullable reference-type extras reinstate their AssertNotNull check.
  • BackCompatibilityChangeCategory — Adds ConstructorAddedFromLastContract and ConstructorAddedFromLastContractSkipped for emitter diagnostics.

Example

Given a model where resources was required and is now optional, the restored overload:

public MockInputModel(string name, string resources) : this(name)
{
    Argument.AssertNotNull(resources, nameof(resources));
    Resources = resources;
}

Tests

Added unit tests covering the restore case (required→optional), and the negative cases where the property was removed entirely or no last contract exists.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI linked an issue Jul 30, 2026 that may be closed by this pull request
@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 Jul 30, 2026

@jorgerangel-msft jorgerangel-msft 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.

@copilot please add robust unit tests to test our changes. We should use TestData to validate the changes, and follow the existing pattern for validating back compat scenarios

@pkg-pr-new

pkg-pr-new Bot commented Jul 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 93bfb97

Copilot AI changed the title [WIP] Fix LastContractView to support back compat model constructors Support back-compat model constructors in the C# generator Jul 30, 2026
Copilot AI requested a review from jorgerangel-msft July 30, 2026 23:09

Copilot AI commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please add robust unit tests to test our changes. We should use TestData to validate the changes, and follow the existing pattern for validating back compat scenarios

Added robust TestData-based tests in c2f7d78: BackCompat_RequiredToOptionalConstructorIsRestored and BackCompat_OptionalValueTypeConstructorParameterIsRestored now validate the full generated model (restored constructor + chaining + assignment) against expected .cs snapshots, and a new BackCompat_RenamedPropertyConstructorIsRestored test covers a [CodeGenMember] rename to exercise the CanonicalView.Properties/OriginalName matching, alongside the existing negative cases.

@jorgerangel-msft jorgerangel-msft 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.

@copilot I"d like to see some unit tests where there are ctors in the back compat view, but also the ctor is replaced with custom code.

Copilot AI commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

@copilot I"d like to see some unit tests where there are ctors in the back compat view, but also the ctor is replaced with custom code.

Added in 255c02e — the new BackCompat_ConstructorNotRestoredWhenReplacedByCustomCode test provides the (name, resources) constructor in the last-contract view but also replaces it with a custom-code partial. The existence check now also scans CanonicalView.Constructors (which includes custom code), so the generator no longer emits a colliding overload. The test asserts the custom ctor is present in the canonical view and that no generated overload is added.

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>

Copilot AI commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot lets update https://github.com/microsoft/typespec/blob/main/packages/http-client-csharp/generator/docs/backward-compatibility.md#model-constructors to include a sub-section explaining the new...

Documented the required-to-optional model-property scenario in 88060f1a, including restoration behavior, example output, eligibility constraints, custom constructors, renames, and ApiCompat baseline handling.

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Comment thread packages/http-client-csharp/generator/docs/backward-compatibility.md Outdated
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@jorgerangel-msft
jorgerangel-msft added this pull request to the merge queue Aug 5, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 5, 2026
@jorgerangel-msft
jorgerangel-msft added this pull request to the merge queue Aug 5, 2026
Merged via the queue into main with commit f31cecc Aug 5, 2026
29 checks passed
@jorgerangel-msft
jorgerangel-msft deleted the copilot/lastcontractview-back-compat-ctors branch August 5, 2026 18:06
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.

LastContractView - Consider Supporting Back Compat Model Ctors

4 participants