Skip to content

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

Description

@live1206

Description

Model-factory back-compat generation does not consistently preserve or safely adapt the previous contract's required/optional parameter boundary when reordered overloads coexist.

The generator currently has two coarse behaviors:

  1. A hidden compatibility overload created through BuildBackCompatMethodSignature(..., hideMethod: true) has every DefaultValue removed, making all parameters required (LastContractView - Make Model Factory Method Parameters Required #8650 / Avoid ambiguous backcompat methods #8674).
  2. A reordered factory method can retain the current generated method's all-optional defaults instead of selecting the minimum required prefix needed to keep the overload set unambiguous.

The first behavior can break existing calls that omitted trailing optional arguments. The second can leave reordered, fully optional overloads ambiguous. SDK customization must currently choose a safe middle ground manually: require enough leading parameters to disambiguate while retaining optional trailing parameters.

AppService example

Azure SDK for .NET PR Azure/azure-sdk-for-net#62039 retains custom wrappers in:

sdk/websites/Azure.ResourceManager.AppService/src/Customization/AppService/Models/ArmAppServiceModelFactory.ParameterCompatibility.cs

For example, SiteContainerData has reordered factory overloads. The compatibility wrapper requires the identifying leading parameters but preserves the trailing optional parameters:

public static SiteContainerData SiteContainerData(
    ResourceIdentifier id,
    string name,
    ResourceType resourceType,
    SystemData systemData,
    string image,
    string targetPort,
    bool? isMain,
    string startUpCommand = default,
    SiteContainerAuthType? authType = default,
    // additional optional parameters
    string kind = default)

Making every parameter required breaks calls that provide the leading arguments but omit optional trailing arguments. Keeping every parameter optional can make calls ambiguous with the current reordered overload.

CsmPublishingCredentialsPoliciesEntityData demonstrates the fully optional case: removing its custom wrapper regenerates two reordered, fully optional overloads. Calls that omit arguments can be ambiguous, so the SDK currently suppresses one generated overload and supplies a required forwarding overload.

Expected behavior

When generating model-factory compatibility methods, determine the minimum required parameter prefix needed for unambiguous overload resolution while preserving the previous optional trailing parameters wherever possible.

Please add tests covering:

  • Reordered model-factory overloads with required leading and optional trailing parameters.
  • Reordered fully optional overloads that would otherwise be ambiguous.
  • A compatibility overload where only the minimum parameters, rather than every parameter, need to become required.

Related:

​- by copilot

Metadata

Metadata

Labels

bugSomething isn't workingemitter:client:csharpIssue for the C# client emitter: @typespec/http-client-csharp

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions