Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ambiguity #3616

Merged
merged 6 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions src/AutoRest.CSharp/LowLevel/Output/OperationMethodChainBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ private bool IsConvenienceMethodMeaningful()

private bool HasAmbiguityBetweenProtocolAndConvenience()
{
return _orderedParameters.Where(parameter => parameter.Convenience != KnownParameters.CancellationTokenParameter).All(parameter => IsParameterTypeHasValueOverlap(parameter.Convenience, parameter.Protocol));
var userDefinedParameters = _orderedParameters.Where(parameter => parameter.Convenience != KnownParameters.CancellationTokenParameter);
int protocolRequired = userDefinedParameters.Select(p => p.Protocol).WhereNotNull().Where(p => !p.IsOptionalInSignature).Count();
int convenienceRequired = userDefinedParameters.Select(p => p.Convenience).WhereNotNull().Where(p => !p.IsOptionalInSignature).Count();
if (protocolRequired != convenienceRequired)
{
return false;
}
return userDefinedParameters.Where(p => p.Protocol != null && !p.Protocol.IsOptionalInSignature).All(parameter => IsParameterTypeSame(parameter.Convenience, parameter.Protocol));
}

private bool ShouldRequestContextOptional()
Expand Down Expand Up @@ -150,21 +157,6 @@ private bool IsParameterTypeSame(Parameter? first, Parameter? second)
return object.Equals(first?.Type, second?.Type);
}

private bool IsParameterTypeHasValueOverlap(Parameter? first, Parameter? second)
{
if (IsParameterTypeSame(first, second))
{
return true;
}

if (first != null && second != null && first.Type.IsNullable && second.Type.IsNullable)
{
return true;
}

return false;
}

private ReturnTypeChain BuildReturnTypes()
{
var operationBodyTypes = Operation.Responses.Where(r => !r.IsErrorResponse).Select(r => r.BodyType).Distinct().ToArray();
Expand Down
12 changes: 6 additions & 6 deletions test/AutoRest.TestServer.Tests/ConvenienceMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public void ProtocolRequiredQueryScenario()
[Test]
public void ProtocolOptionalModelScenario()
{
var protocolInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ProtocolOptionalModel");
var convenienceInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ProtocolOptionalModelValue");
var protocolInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ProtocolOptionalModel", new[] { typeof(RequestContent), typeof(RequestContext) });
var convenienceInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ProtocolOptionalModel", new[] { typeof(Model), typeof(CancellationToken) });
Assert.AreEqual(true, protocolInUpdate.GetParameters().Last().IsOptional);
Assert.AreEqual(false, protocolInUpdate.GetParameters().First().IsOptional); // This is RequestContent, which is actually optional.
Assert.AreEqual(true, convenienceInUpdate.GetParameters().First().IsOptional);
Expand Down Expand Up @@ -165,8 +165,8 @@ public void ConvenienceRequiredQueryWithRequiredScenario()
[Test]
public void ConvenienceOptionalModelWithOptionalScenario()
{
var protocolInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ConvenienceOptionalModelWithOptional");
var convenienceInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ConvenienceOptionalModelWithOptionalValue");
var protocolInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ConvenienceOptionalModelWithOptional", new[] { typeof(RequestContent), typeof(RequestContext) });
var convenienceInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ConvenienceOptionalModelWithOptional", new[] { typeof(Model), typeof(CancellationToken) });
Assert.AreEqual(true, protocolInUpdate.GetParameters().Last().IsOptional);
Assert.AreEqual(false, protocolInUpdate.GetParameters().First().IsOptional); // This is RequestContent, which is actually optional.
Assert.AreEqual(true, convenienceInUpdate.GetParameters().First().IsOptional);
Expand All @@ -189,7 +189,7 @@ public void ConvenienceOptionalModelWithRequiredScenario()
{
var protocolInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ConvenienceOptionalModelWithRequired", new[] { typeof(RequestContent), typeof(RequestContext) });
var convenienceInUpdate = typeof(ConvenienceInCadlClient).GetMethod("ConvenienceOptionalModelWithRequired", new[] { typeof(Model), typeof(CancellationToken) });
Assert.AreEqual(false, protocolInUpdate.GetParameters().Last().IsOptional);
Assert.AreEqual(true, protocolInUpdate.GetParameters().Last().IsOptional);
Assert.AreEqual(false, protocolInUpdate.GetParameters().First().IsOptional); // This is RequestContent, which is actually optional.
Assert.AreEqual(true, convenienceInUpdate.GetParameters().First().IsOptional);
}
Expand Down Expand Up @@ -232,7 +232,7 @@ public void NoConvenienceRequiredBodyScenario()
public void NoConvenienceOptionalBodyScenario()
{
var protocolInUpdate = typeof(ConvenienceInCadlClient).GetMethod("NoConvenienceOptionalBody");
Assert.AreEqual(false, protocolInUpdate.GetParameters().Last().IsOptional);
Assert.AreEqual(true, protocolInUpdate.GetParameters().Last().IsOptional);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ParameterSequenceTests
{
[TestCase(typeof(ParametersLowlevelClient), "OptionalPathParameters", new Type[] { typeof(int), typeof(string), typeof(int), typeof(RequestContext) }, new string[] { "id", "name", "skip", "context" }, new bool[] { false, false, false, true })]
[TestCase(typeof(ParametersLowlevelClient), "OptionalPathParametersWithMixedSequence", new Type[] { typeof(int), typeof(string), typeof(int), typeof(RequestContext) }, new string[] { "id", "name", "skip", "context" }, new bool[] { false, false, false, true })]
[TestCase(typeof(ParametersLowlevelClient), "OptionalPathBodyParametersWithMixedSequence", new Type[] { typeof(int), typeof(string), typeof(int), typeof(int), typeof(RequestContent), typeof(int), typeof(RequestContext) }, new string[] { "id", "name", "skip", "max", "content", "top", "context" }, new bool[] { false, false, false, false, false, false, false })]
[TestCase(typeof(ParametersLowlevelClient), "OptionalPathBodyParametersWithMixedSequence", new Type[] { typeof(int), typeof(string), typeof(int), typeof(int), typeof(RequestContent), typeof(int), typeof(RequestContext) }, new string[] { "id", "name", "skip", "max", "content", "top", "context" }, new bool[] { false, false, false, false, false, true, true })]
[TestCase(typeof(ParametersCadlClient), "Operation", new Type[] { typeof(int), typeof(int), typeof(CancellationToken) }, new string[] { "start", "end", "cancellationToken" }, new bool[] { false, true, true })]
[TestCase(typeof(ParametersCadlClient), "Operation", new Type[] { typeof(int), typeof(int), typeof(RequestContext) }, new string[] { "start", "end", "context" }, new bool[] { false, false, false })]
[TestCase(typeof(ParametersCadlClient), "Operation2", new Type[] { typeof(int), typeof(int), typeof(CancellationToken) }, new string[] { "end", "start", "cancellationToken" }, new bool[] { false, true, true })]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void RequestBodyResponseBody()
Assert.AreEqual(parameters[0].ParameterType, typeof(RequestContent));
Assert.AreEqual(parameters[0].IsOptional, false);
Assert.AreEqual(parameters[1].ParameterType, typeof(RequestContext));
Assert.AreEqual(parameters[1].IsOptional, false);
Assert.AreEqual(parameters[1].IsOptional, true);
}

[Test]
Expand All @@ -76,7 +76,7 @@ public void RequestBodyNoResponseBody()
Assert.AreEqual(parameters[0].ParameterType, typeof(RequestContent));
Assert.AreEqual(parameters[0].IsOptional, false);
Assert.AreEqual(parameters[1].ParameterType, typeof(RequestContext));
Assert.AreEqual(parameters[1].IsOptional, false);
Assert.AreEqual(parameters[1].IsOptional, true);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
<members>
<member name="OperationAsync(RequestContent,RequestContext)">
<example>
This sample shows how to call OperationAsync with required parameters and request content.
This sample shows how to call OperationAsync with required request content.
<code><![CDATA[
var client = new AccessibilityClient();

var data = "<String>";

Response response = await client.OperationAsync(RequestContent.Create(data), new RequestContext());
Response response = await client.OperationAsync(RequestContent.Create(data));
Console.WriteLine(response.Status);
]]></code>
</example>
</member>
<member name="Operation(RequestContent,RequestContext)">
<example>
This sample shows how to call Operation with required parameters and request content.
This sample shows how to call Operation with required request content.
<code><![CDATA[
var client = new AccessibilityClient();

var data = "<String>";

Response response = client.Operation(RequestContent.Create(data), new RequestContext());
Response response = client.Operation(RequestContent.Create(data));
Console.WriteLine(response.Status);
]]></code>
</example>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Example_Operation()

var data = "<String>";

Response response = client.Operation(RequestContent.Create(data), new RequestContext());
Response response = client.Operation(RequestContent.Create(data));
Console.WriteLine(response.Status);
}

Expand All @@ -39,7 +39,7 @@ public void Example_Operation_AllParameters()

var data = "<String>";

Response response = client.Operation(RequestContent.Create(data), new RequestContext());
Response response = client.Operation(RequestContent.Create(data));
Console.WriteLine(response.Status);
}

Expand All @@ -51,7 +51,7 @@ public async Task Example_Operation_Async()

var data = "<String>";

Response response = await client.OperationAsync(RequestContent.Create(data), new RequestContext());
Response response = await client.OperationAsync(RequestContent.Create(data));
Console.WriteLine(response.Status);
}

Expand All @@ -63,7 +63,7 @@ public async Task Example_Operation_AllParameters_Async()

var data = "<String>";

Response response = await client.OperationAsync(RequestContent.Create(data), new RequestContext());
Response response = await client.OperationAsync(RequestContent.Create(data));
Console.WriteLine(response.Status);
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
<members>
<member name="OperationAsync(RequestContent,RequestContext)">
<example>
This sample shows how to call OperationAsync with required parameters and request content.
This sample shows how to call OperationAsync with required request content.
<code><![CDATA[
var credential = new DefaultAzureCredential();
var client = new AccessibilityClient(credential);

var data = "<String>";

Response response = await client.OperationAsync(RequestContent.Create(data), new RequestContext());
Response response = await client.OperationAsync(RequestContent.Create(data));
Console.WriteLine(response.Status);
]]></code>
</example>
</member>
<member name="Operation(RequestContent,RequestContext)">
<example>
This sample shows how to call Operation with required parameters and request content.
This sample shows how to call Operation with required request content.
<code><![CDATA[
var credential = new DefaultAzureCredential();
var client = new AccessibilityClient(credential);

var data = "<String>";

Response response = client.Operation(RequestContent.Create(data), new RequestContext());
Response response = client.Operation(RequestContent.Create(data));
Console.WriteLine(response.Status);
]]></code>
</example>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Example_Operation()

var data = "<String>";

Response response = client.Operation(RequestContent.Create(data), new RequestContext());
Response response = client.Operation(RequestContent.Create(data));
Console.WriteLine(response.Status);
}

Expand All @@ -41,7 +41,7 @@ public void Example_Operation_AllParameters()

var data = "<String>";

Response response = client.Operation(RequestContent.Create(data), new RequestContext());
Response response = client.Operation(RequestContent.Create(data));
Console.WriteLine(response.Status);
}

Expand All @@ -54,7 +54,7 @@ public async Task Example_Operation_Async()

var data = "<String>";

Response response = await client.OperationAsync(RequestContent.Create(data), new RequestContext());
Response response = await client.OperationAsync(RequestContent.Create(data));
Console.WriteLine(response.Status);
}

Expand All @@ -67,7 +67,7 @@ public async Task Example_Operation_AllParameters_Async()

var data = "<String>";

Response response = await client.OperationAsync(RequestContent.Create(data), new RequestContext());
Response response = await client.OperationAsync(RequestContent.Create(data));
Console.WriteLine(response.Status);
}
}
Expand Down
Loading