Support back-compat model constructors in the C# generator - #11503
Conversation
|
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. |
jorgerangel-msft
left a comment
There was a problem hiding this comment.
@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
commit: |
Added robust TestData-based tests in c2f7d78: |
jorgerangel-msft
left a comment
There was a problem hiding this comment.
@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 |
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Documented the required-to-optional model-property scenario in |
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
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
LastContractViewback-compat mechanism.Changes
ModelProvider.BuildConstructorsForBackCompatibility(new override) — For each public constructor inLastContractViewthat 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.OriginalNamefor codegen renames. If any dropped parameter cannot be mapped, restoration is skipped.AssertNotNullcheck.BackCompatibilityChangeCategory— AddsConstructorAddedFromLastContractandConstructorAddedFromLastContractSkippedfor emitter diagnostics.Example
Given a model where
resourceswas required and is now optional, the restored overload: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.