Skip to content

Commit 0cf4490

Browse files
Pass null for required nullable (#9635)
1 parent 5f56de6 commit 0cf4490

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,12 @@ private IReadOnlyList<ValueExpression> GetProtocolMethodArguments(Dictionary<str
644644
else
645645
{
646646
requireNamedArgs = true;
647+
// The protocol parameter might be required due to our need to avoid ambiguity with overloads. In this
648+
// case, the parameter should have also been made nullable.
649+
if (protocolParam.DefaultValue == null && protocolParam.Type.IsNullable)
650+
{
651+
AddArgument(protocolParam, Null);
652+
}
647653
}
648654
continue;
649655
}

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,46 @@ public async Task MissingOperationParamsResultInNamedArgsForSubsequent()
16561656
Assert.AreEqual(Helpers.GetExpectedFromFile(), methodBody);
16571657
}
16581658

1659+
[Test]
1660+
public async Task MissingRequiredOperationParamsResultInPassingNull()
1661+
{
1662+
var idParam = InputFactory.PathParameter("id", InputPrimitiveType.String, isRequired: true);
1663+
// This will be made required in the protocol method due to method overload ambiguity
1664+
var takeParam = InputFactory.QueryParameter("take", InputPrimitiveType.Int32, isRequired: false);
1665+
var filterParam = InputFactory.QueryParameter("filter", InputPrimitiveType.String, isRequired: false);
1666+
var orderParam = InputFactory.HeaderParameter("order", InputPrimitiveType.String, isRequired: false);
1667+
1668+
var serviceMethod = InputFactory.BasicServiceMethod(
1669+
"TestOp",
1670+
InputFactory.Operation(
1671+
"TestOp",
1672+
parameters: [idParam, filterParam, orderParam, takeParam],
1673+
responses: [InputFactory.OperationResponse([200])]),
1674+
parameters:
1675+
[
1676+
InputFactory.MethodParameter("id", InputPrimitiveType.String, isRequired: true, location: InputRequestLocation.Path),
1677+
InputFactory.MethodParameter("take", InputPrimitiveType.Int32, isRequired: false, location: InputRequestLocation.Query),
1678+
InputFactory.MethodParameter("order", InputPrimitiveType.Int32, isRequired: false, location: InputRequestLocation.Header),
1679+
]);
1680+
1681+
var inputClient = InputFactory.Client("TestClient", methods: [serviceMethod]);
1682+
await MockHelpers.LoadMockGeneratorAsync(clients: () => [inputClient]);
1683+
1684+
var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient);
1685+
Assert.IsNotNull(client);
1686+
1687+
var methodCollection = new ScmMethodProviderCollection(serviceMethod, client!);
1688+
Assert.IsNotNull(methodCollection);
1689+
1690+
var convenienceMethod = methodCollection.FirstOrDefault(m
1691+
=> m.Signature.Parameters.All(p => p.Name != "options")
1692+
&& m.Signature.Name == "TestOp");
1693+
Assert.IsNotNull(convenienceMethod);
1694+
1695+
var methodBody = convenienceMethod!.BodyStatements!.ToDisplayString();
1696+
Assert.AreEqual(Helpers.GetExpectedFromFile(), methodBody);
1697+
}
1698+
16591699
[Test]
16601700
public async Task CombinedMissingOperationParamsAndNonBodyModelParams()
16611701
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global::Sample.Argument.AssertNotNullOrEmpty(id, nameof(id));
2+
3+
return this.TestOp(id, filter: null, order: order, take: take, options: cancellationToken.ToRequestOptions());

0 commit comments

Comments
 (0)