Skip to content

Preserve model factory back-compat parameter optionality - #11703

Draft
Jorge Rangel (jorgerangel-msft) with Copilot wants to merge 26 commits into
mainfrom
copilot/http-client-csharp-preserve-back-compat-parameter
Draft

Preserve model factory back-compat parameter optionality#11703
Jorge Rangel (jorgerangel-msft) with Copilot wants to merge 26 commits into
mainfrom
copilot/http-client-csharp-preserve-back-compat-parameter

Conversation

Copilot AI commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Model-factory compatibility overloads either made every parameter required or retained fully optional reordered signatures, breaking callers or creating ambiguous overloads.

Changes

  • Compute the minimum required positional prefix across all surviving generated and custom overloads.
  • Preserve trailing defaults once parameter types distinguish the compatibility overload.
  • Preserve all defaults when no competing overload exists.
  • Cover required/optional boundaries, fully optional reorders, custom overloads, and nullable value types.
CompatibilityModel(
    string id,
    string name,
    bool? enabled,
    string description = default);

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

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@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 Aug 17, 2026
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot AI changed the title [WIP] Preserve model factory back-compat parameter optionality when unambiguous Preserve model factory back-compat parameter optionality Aug 17, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 492f39e

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 updates the http-client-csharp generator’s model-factory back-compat signature shaping so compatibility overloads preserve optional trailing parameters when doing so does not introduce ambiguous overload resolution, while still requiring the minimum positional prefix needed to disambiguate against surviving generated/custom overloads.

Changes:

  • Extend MethodSignatureHelper.BuildBackCompatMethodSignature to compute and enforce a minimum “required parameter prefix” based on competing overload signatures.
  • Update ModelFactoryProvider to pass the relevant competing overload set (including custom overloads) when constructing back-compat shims.
  • Add/adjust unit tests and golden test data to cover reordered overloads, fully-optional overloads, custom overload coexistence, and nullable value-type disambiguation.

Reviewed changes

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

Show a summary per file
File Description
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Shared/MethodSignatureHelper.cs Adds overload-aware logic to require only the minimum disambiguating parameter prefix while preserving trailing defaults when safe.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelFactoryProvider.cs Computes and supplies the current competing overload set (including custom methods) when generating back-compat model factory methods.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Shared/MethodSignatureHelperTests.cs Adds focused unit tests validating minimum-prefix computation across multiple overloads and value-type nullability distinctions.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/ModelFactoryProviderTests.cs Adds integration tests asserting required/optional boundaries for generated back-compat factory methods across key scenarios.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/TestData/ModelFactoryProviderTests/BackCompatibility_UnknownDiscriminatorReturnTypeOverloadIsGenerated.cs Updates expected output to preserve optional parameters when no competing overload exists for the compatibility overload.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/TestData/ModelFactoryProviderTests/BackCompatibility_ReorderedRequiredParametersPreserveOptionalTrailingParameters/SampleNamespaceModelFactory.cs New test asset representing a prior contract with required prefix + optional trailing defaults.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/TestData/ModelFactoryProviderTests/BackCompatibility_ReorderedFullyOptionalParametersRequireMinimumPrefix/SampleNamespaceModelFactory.cs New test asset representing a fully-optional prior contract reorder scenario.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/TestData/ModelFactoryProviderTests/BackCompatibility_ReorderedCustomOverloadRequiresMinimumPrefix(Last)/SampleNamespaceModelFactory.cs New “last contract” asset used to validate interplay with custom overloads.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/TestData/ModelFactoryProviderTests/BackCompatibility_ReorderedCustomOverloadRequiresMinimumPrefix(Custom)/SampleNamespaceModelFactory.cs New “custom” asset used to validate coexistence/minimum-prefix computation against custom overload signatures.

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

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

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 23 out of 23 changed files in this pull request and generated no new comments.

Suppressed comments (2)

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Shared/MethodSignatureHelper.cs:82

  • BuildBackCompatMethodSignature(previous, hideMethod, currentMethodSignatures, ...) currently calls RequireMinimumParameterPrefix(previousMethodSignature, ...), which mutates the last-contract signature in place (it nulls DefaultValue on its ParameterProvider instances). In ModelFactoryProvider those previous signatures are reused while processing other overloads, so this introduces order-dependent behavior and can affect later comparisons.

Consider applying the required-prefix adjustment only to the new back-compat signature (leaving the previous-contract signature unchanged).

        internal static MethodSignature BuildBackCompatMethodSignature(
            MethodSignature previousMethodSignature,
            bool hideMethod,
            IReadOnlyList<MethodSignature> currentMethodSignatures,
            bool shouldNotBeAsync = false)

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Shared/MethodSignatureHelper.cs:89

  • The XML doc for RequireMinimumParameterPrefix says that when no overloads are supplied "every parameter becomes required", but the implementation only forces all parameters required when currentMethodSignatures is null (an empty list results in requiredParameterCount = 0). Updating the wording would avoid misleading future maintainers.
        /// Removes the default values from the leading parameters of <paramref name="signature"/> so it
        /// can no longer be called with fewer arguments than the prefix that distinguishes it from
        /// <paramref name="currentMethodSignatures"/>. When no overloads are supplied there is nothing to
        /// compare against and every parameter becomes required.
        /// </summary>

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

Found a source-compatibility regression in the overload analysis.

@JoshLove-msft
JoshLove-msft dismissed their stale review August 19, 2026 21:28

Review state corrected; PR reviews should be posted as comments, not changes requested.

Copilot AI review requested due to automatic review settings August 20, 2026 19:41

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 32 out of 32 changed files in this pull request and generated no new comments.

Suppressed comments (1)

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Shared/MethodSignatureHelper.cs:196

  • GetMinimumRequiredParameterCount uses CSharpType.AreNamesEqual(...) to detect the first distinguishing parameter type, but AreNamesEqual ignores nullability. In C#, T vs T? for value types are distinct overloads (see MethodSignatureBase.cs:179-187), so nullable value type differences should be treated as a distinguishing type here as well. Otherwise, overloads that differ only by nullable value type will be treated as identical and may force all parameters to become required unnecessarily, breaking the intended “minimum required prefix” behavior.
            for (int i = 0; i < overlappingParameterCount; i++)
            {
                if (!targetMethodSignature.Parameters[i].Type.AreNamesEqual(competingMethodSignature.Parameters[i].Type))
                {
                    return Math.Max(i + 1, targetMinimumArgumentCount);
                }

Copilot AI review requested due to automatic review settings August 20, 2026 22:54

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 34 out of 34 changed files in this pull request and generated no new comments.

Suppressed comments (2)

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Shared/MethodSignatureHelperTests.cs:12

  • using Microsoft.TypeSpec.Generator.Tests.Common; is unused in this test file (no Helpers/other symbols referenced), which can introduce avoidable warnings and makes the imports noisier.
    packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Shared/MethodSignatureHelper.cs:88
  • The XML doc for RequireMinimumParameterPrefix says that when “no overloads are supplied” every parameter becomes required, but the implementation only does that when currentMethodSignatures is null. When an empty list is passed (e.g., “no competing overload exists”), GetMinimumRequiredParameterCount returns 0 and defaults are preserved. Updating the doc would prevent callers from misusing null vs empty lists.
        /// <summary>
        /// Removes the default values from the leading parameters of <paramref name="signature"/> so it
        /// can no longer be called with fewer arguments than the prefix that distinguishes it from
        /// <paramref name="currentMethodSignatures"/>. When no overloads are supplied there is nothing to
        /// compare against and every parameter becomes required.

@jorgerangel-msft
Jorge Rangel (jorgerangel-msft) marked this pull request as draft August 21, 2026 15:47

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 42 out of 42 changed files in this pull request and generated 2 comments.

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
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.

[http-client-csharp] Preserve model factory back-compat parameter optionality when unambiguous

4 participants