Skip to content

LastContractView - Restore Removed Property Setters When Safe #12591

Open

Description

Clear and concise description of the problem

When a generated model property had a public setter in the last contract but the current generation emits it without one, the setter removal is a source and binary breaking change (set_X no longer exists). This usually happens for incidental reasons rather than an intentional change to the property's writability, because the generator derives setter presence from model usage and requiredness (PropertyProvider.PropertyHasSetter):

  • Usage change: a round-trip model becomes output-only (all setters dropped) or input-only (required properties lose their setters and move to the constructor), e.g. after an operation is removed or a request body is reshaped.
  • Requiredness change: an optional property becomes required on an input-only model.
  • Collection nullability change: a nullable collection property becomes non-nullable.
  • Generator migration: a library moving to this generator can hit different setter rules than the generator that produced its last contract.

Today the only remedies are accepting the break in the ApiCompat baseline or re-declaring the entire property in custom code (see #11804).

Proposal

Use the model's LastContractView to restore a missing property setter, with the same accessibility it had in the last contract, only when it is safe to do so. The last contract is already loaded with full metadata, and NamedTypeSymbolProvider records Body.HasSetter, the setter accessibility, and IsInitOnly for each property, so the required information is available.

⚠️ Safety requirement

A restored setter must never introduce unintended behavior. If assigning through the setter would not behave the way a caller expects — for example, the value would be silently dropped, could produce an invalid request, or the generated code would not compile — the setter must not be restored, and the breaking change should continue to surface through ApiCompat as it does today.

Restore the setter only when all of the following hold:

  • The last-contract setter was part of the public API (public or protected). Private/internal setters are also imported from the last contract and must be ignored.
  • The last-contract setter was not init.
  • The property is not read-only in the spec (InputProperty.IsReadOnly, e.g. @visibility(Lifecycle.Read)). Read-only properties are only serialized when options.Format != "W", so a value assigned through a restored setter would be silently dropped from service requests.
  • The property is not a required, non-nullable constant/literal. A setter would allow callers to send a value the service does not accept.
  • The enclosing type is not a readonly struct.
  • The generated serialization correctly handles every value the setter can accept (e.g. a non-nullable collection whose serialization assumes a non-null value).
  • The property type matches the last contract (after the existing property type back-compat is applied).
  • The property is not already defined in custom code.
  • The setter removal has not been accepted in the ApiCompat baseline.

When a setter is restored, emit an informational back-compat diagnostic. When a setter is intentionally not restored because one of the safety checks failed, emit a diagnostic explaining why.

Implementation notes

  • Apply the decision where the property body is built (the PropertyProvider constructor already resolves the matching last-contract property for name preservation) so that docs ("Gets or sets"), constructors, serialization, the model factory, and base/derived property override handling all observe the same setter state. Properties with exact names also need a last-contract lookup.
  • Tests should cover a restored setter and each of the skipped cases above.

Parent: #7241
Related: #11804

Checklist

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Activity

  1. github-actions commented on Nov 16, 2026

    @github-actions
    Contributor

    🎯 Issue Triage

    This requests C# client emitter back-compat support that restores removed public or protected model property setters from LastContractView only when assignment remains safe and behaviorally valid.

    Category: Feature Request
    Area: emitter:client:csharp

    🔍 Analysis

    The affected component is @typespec/http-client-csharp, particularly PropertyProvider, last-contract symbol metadata, generated constructors/documentation, model factories, inheritance handling, serialization, diagnostics, and ApiCompat baselines. The setter loss is caused by current-generation usage, requiredness, or nullability rules diverging from the previously shipped contract; the proposal correctly requires preserving the break when restoration could drop values, permit invalid requests, or produce invalid code.

    No strong duplicate was found. #7241 is the broader parent for back-compat scenarios, while #11804 is related custom-property support rather than the same setter-restoration request.

    📝 Suggested next steps
    • Define the setter-restoration decision centrally while building the property body.
    • Add diagnostics for restored setters and each safety-based rejection.
    • Add tests for successful restoration and every listed exclusion, including ApiCompat-baseline acceptance.

    Generated by Agentic Triage

  2. jorgerangel-msft commented on Nov 17, 2026

    @jorgerangel-msft
    ContributorAuthor

    Safe to restore

    • The model became response-only. The property still serializes on write, so a value set through the setter is kept and written out. This matters for tests and mocks.
    • The model became request-only, and a required property moved into the constructor. Restoring the setter gives the same shape the property had when the model was used in both directions. The constructor change is a separate break, which constructor back-compat handles.
    • An optional property became required on a request-only model. The result has the same shape as a required property on a model used in both directions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    emitter:client:csharpIssue for the C# client emitter: @typespec/http-client-csharpfeatureNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions