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

[http-client-csharp] adopt http\resiliency\srv-driven #5142

Merged
merged 7 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ public class ScmMethodProviderCollection : MethodProviderCollection
private string _cleanOperationName;
private readonly MethodProvider _createRequestMethod;

private readonly string _createRequestMethodName;

private ClientProvider Client { get; }

public ScmMethodProviderCollection(InputOperation operation, TypeProvider enclosingType)
: base(operation, enclosingType)
{
_cleanOperationName = operation.Name.ToCleanName();
_createRequestMethodName = "Create" + _cleanOperationName + "Request";
Client = enclosingType as ClientProvider ?? throw new InvalidOperationException("Scm methods can only be built for client types.");
_createRequestMethod = Client.RestClient.GetCreateRequestMethod(Operation);
}
Expand Down Expand Up @@ -388,6 +385,7 @@ private ScmMethodProvider BuildProtocolMethod(MethodProvider createRequestMethod

var requiredParameters = new List<ParameterProvider>();
var optionalParameters = new List<ParameterProvider>();

for (var i = 0; i < ProtocolMethodParameters.Count; i++)
{
var parameter = ProtocolMethodParameters[i];
Expand All @@ -407,14 +405,17 @@ private ScmMethodProvider BuildProtocolMethod(MethodProvider createRequestMethod
{
// If there are optional parameters, but the request options parameter is not optional, make the optional parameters nullable required.
// This is to ensure that the request options parameter is always the last parameter.
foreach (var parameter in optionalParameters)

for (var i = 0; i < optionalParameters.Count; i++)
{
parameter.DefaultValue = null;
parameter.Type = parameter.Type.WithNullable(true);
// Ensure a new copy is made to avoid modifying the original reference.
var optionalParam = optionalParameters[i].ToPublicInputParameter();
jorgerangel-msft marked this conversation as resolved.
Show resolved Hide resolved

optionalParam.DefaultValue = null;
optionalParam.Type = optionalParameters[i].Type.WithNullable(true);
requiredParameters.Add(optionalParam);
}
// Now, the request options parameter can be optional due to the above changes to the method signature.
requestOptionsParameter = ScmKnownParameters.OptionalRequestOptions;
requiredParameters.AddRange(optionalParameters);

optionalParameters.Clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ public static IEnumerable<TestCaseData> RequestOptionsParameterInSignatureTestCa
]), false, false);

// Protocol & convenience methods will have the same parameters.
// One of the parameter is optional, so it should be make required in the protocol method, and RequestOptions can be optional.
// One of the parameter is optional, so it should be made required in the protocol method.
yield return new TestCaseData(
InputFactory.Operation(
"TestOperation",
Expand All @@ -755,7 +755,7 @@ public static IEnumerable<TestCaseData> RequestOptionsParameterInSignatureTestCa
InputPrimitiveType.Int64,
location: RequestLocation.None,
isRequired: true),
]), true, true);
]), false, true);

// Protocol & convenience methods will have the same parameters.
// One of the parameter is optional value type, so it should be made nullable required in the protocol method, and RequestOptions can be optional.
Expand All @@ -774,7 +774,7 @@ public static IEnumerable<TestCaseData> RequestOptionsParameterInSignatureTestCa
InputPrimitiveType.Int64,
location: RequestLocation.None,
isRequired: true),
]), true, true);
]), false, true);

// convenience method only has a body param, so RequestOptions should be optional in protocol method.
yield return new TestCaseData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"http-resiliency-srv-driven-v1": {
"commandLineArgs": "$(SolutionDir)/TestProjects/CadlRanch/http/resiliency/srv-driven/v1 -p StubLibraryPlugin",
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"http-routes": {
"commandLineArgs": "$(SolutionDir)/TestProjects/CadlRanch/http/routes -p StubLibraryPlugin",
"commandName": "Executable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ private ParameterProvider BuildInputVariant()
wireInfo: WireInfo,
validation: Validation)
{
_asVariable = AsExpression
_asVariable = AsExpression,
SpreadSource = SpreadSource
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Task AddOptionalParamFromOneOptional_V1Client_V1Service_WithApiVersionV1(
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV1, options);
var response = await client.FromOneOptionalAsync("optional", cancellationToken: default);
var response = await client.FromOneOptionalAsync("optional");

Assert.AreEqual(204, response.GetRawResponse().Status);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ public partial class ResiliencyServiceDrivenClient

public virtual Task<ClientResult> AddOperationAsync(CancellationToken cancellationToken = default) => throw null;

public virtual ClientResult FromNone(string newParameter, RequestOptions options = null) => throw null;
public virtual ClientResult FromNone(string newParameter, RequestOptions options) => throw null;

public virtual Task<ClientResult> FromNoneAsync(string newParameter, RequestOptions options = null) => throw null;
public virtual Task<ClientResult> FromNoneAsync(string newParameter, RequestOptions options) => throw null;

public virtual ClientResult FromNone(string newParameter = null) => throw null;

public virtual Task<ClientResult> FromNoneAsync(string newParameter = null, CancellationToken cancellationToken = default) => throw null;

public virtual ClientResult FromOneRequired(string parameter, string newParameter, RequestOptions options = null) => throw null;
public virtual ClientResult FromOneRequired(string parameter, string newParameter, RequestOptions options) => throw null;

public virtual Task<ClientResult> FromOneRequiredAsync(string parameter, string newParameter, RequestOptions options = null) => throw null;
public virtual Task<ClientResult> FromOneRequiredAsync(string parameter, string newParameter, RequestOptions options) => throw null;

public virtual ClientResult FromOneRequired(string parameter, string newParameter = null) => throw null;

public virtual Task<ClientResult> FromOneRequiredAsync(string parameter, string newParameter = null, CancellationToken cancellationToken = default) => throw null;

public virtual ClientResult FromOneOptional(string parameter, string newParameter, RequestOptions options = null) => throw null;
public virtual ClientResult FromOneOptional(string parameter, string newParameter, RequestOptions options) => throw null;

public virtual Task<ClientResult> FromOneOptionalAsync(string parameter, string newParameter, RequestOptions options = null) => throw null;
public virtual Task<ClientResult> FromOneOptionalAsync(string parameter, string newParameter, RequestOptions options) => throw null;

public virtual ClientResult FromOneOptional(string parameter = null, string newParameter = null) => throw null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public partial class ResiliencyServiceDrivenClient

public virtual Task<ClientResult> FromOneRequiredAsync(string parameter, CancellationToken cancellationToken = default) => throw null;

public virtual ClientResult FromOneOptional(string parameter, RequestOptions options = null) => throw null;
public virtual ClientResult FromOneOptional(string parameter, RequestOptions options) => throw null;

public virtual Task<ClientResult> FromOneOptionalAsync(string parameter, RequestOptions options = null) => throw null;
public virtual Task<ClientResult> FromOneOptionalAsync(string parameter, RequestOptions options) => throw null;

public virtual ClientResult FromOneOptional(string parameter = null) => throw null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public UnbrandedTypeSpecClient(Uri endpoint, ApiKeyCredential keyCredential, Unb
/// <exception cref="ArgumentNullException"> <paramref name="headParameter"/> or <paramref name="queryParameter"/> is null. </exception>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
public virtual ClientResult SayHi(string headParameter, string queryParameter, string optionalQuery, RequestOptions options = null)
public virtual ClientResult SayHi(string headParameter, string queryParameter, string optionalQuery, RequestOptions options)
{
Argument.AssertNotNull(headParameter, nameof(headParameter));
Argument.AssertNotNull(queryParameter, nameof(queryParameter));
Expand All @@ -95,7 +95,7 @@ public virtual ClientResult SayHi(string headParameter, string queryParameter, s
/// <exception cref="ArgumentNullException"> <paramref name="headParameter"/> or <paramref name="queryParameter"/> is null. </exception>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
public virtual async Task<ClientResult> SayHiAsync(string headParameter, string queryParameter, string optionalQuery, RequestOptions options = null)
public virtual async Task<ClientResult> SayHiAsync(string headParameter, string queryParameter, string optionalQuery, RequestOptions options)
{
Argument.AssertNotNull(headParameter, nameof(headParameter));
Argument.AssertNotNull(queryParameter, nameof(queryParameter));
Expand Down