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

[csharp-netcore][generichost] Used default cancellation token #15594

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
// Project Variable, determined from target framework. Not intended to be user-settable.
protected static final String TARGET_FRAMEWORK_VERSION = "targetFrameworkVersion";

protected static final String NET_STANDARD_14_OR_LATER = "netstandard14OrLater";
protected static final String NET_STANDARD_15_OR_LATER = "netstandard15OrLater";
protected static final String NET_STANDARD_16_OR_LATER = "netstandard16OrLater";
protected static final String NET_STANDARD_20_OR_LATER = "netstandard20OrLater";
protected static final String NET_STANDARD_21_OR_LATER = "netstandard21OrLater";
protected static final String NET_CORE_APP_31_OR_LATER = "netcoreapp31OrLater";
protected static final String NET_47_OR_LATER = "net47OrLater";
protected static final String NET_48_OR_LATER = "net48OrLater";
protected static final String NET_60_OR_LATER = "net60OrLater";
protected static final String NET_70_OR_LATER = "net70OrLater";

@SuppressWarnings("hiding")
private final Logger LOGGER = LoggerFactory.getLogger(CSharpClientCodegen.class);
private static final List<FrameworkStrategy> frameworkStrategies = Arrays.asList(
Expand Down Expand Up @@ -1401,6 +1412,77 @@ protected void configureAdditionalPropertiesForFrameworks(final Map<String, Obje
for (FrameworkStrategy frameworkStrategy : frameworkStrategies) {
properties.put(frameworkStrategy.name, strategies.stream().anyMatch(s -> s.name.equals(frameworkStrategy.name)));
}

if (strategies.stream().anyMatch(p -> Boolean.FALSE.equals(p.name == "netstandard1.3"))) {
if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard1.4"))) {
properties.put(NET_STANDARD_14_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard1.5"))) {
properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard1.6"))) {
properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard2.0"))) {
properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard2.1"))) {
properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true);
properties.put(NET_STANDARD_21_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netcoreapp3.1"))) {
properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true);
properties.put(NET_STANDARD_21_OR_LATER, true);
properties.put(NET_CORE_APP_31_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "net47"))) {
properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true);
properties.put(NET_STANDARD_21_OR_LATER, true);
properties.put(NET_CORE_APP_31_OR_LATER, true);
properties.put(NET_47_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "net48"))) {
properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true);
properties.put(NET_STANDARD_21_OR_LATER, true);
properties.put(NET_CORE_APP_31_OR_LATER, true);
properties.put(NET_47_OR_LATER, true);
properties.put(NET_48_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "net6.0"))) {
properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true);
properties.put(NET_STANDARD_21_OR_LATER, true);
properties.put(NET_CORE_APP_31_OR_LATER, true);
properties.put(NET_47_OR_LATER, true);
properties.put(NET_48_OR_LATER, true);
properties.put(NET_60_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "net7.0"))){
properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true);
properties.put(NET_STANDARD_21_OR_LATER, true);
properties.put(NET_CORE_APP_31_OR_LATER, true);
properties.put(NET_47_OR_LATER, true);
properties.put(NET_48_OR_LATER, true);
properties.put(NET_60_OR_LATER, true);
properties.put(NET_70_OR_LATER, true);
} else {
throw new RuntimeException("Unhanlded case");
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace {{packageName}}.{{clientPackage}}
/// <param name="request"></param>
/// <param name="requestBody"></param>
/// <param name="cancellationToken"></param>
internal Dictionary<string, string> GetHttpSignedHeader(System.Net.Http.HttpRequestMessage request, string requestBody, System.Threading.CancellationToken? cancellationToken = null)
internal Dictionary<string, string> GetHttpSignedHeader(System.Net.Http.HttpRequestMessage request, string requestBody, System.Threading.CancellationToken cancellationToken = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}})
{
if (request.RequestUri == null)
throw new NullReferenceException("The request URI was null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace {{packageName}}.{{clientPackage}}
/// <param name="request"></param>
/// <param name="requestBody"></param>
/// <param name="cancellationToken"></param>
public void UseInHeader(System.Net.Http.HttpRequestMessage request, string requestBody, CancellationToken? cancellationToken = null)
public void UseInHeader(System.Net.Http.HttpRequestMessage request, string requestBody, CancellationToken cancellationToken = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}})
{
var signedHeaders = _configuration.GetHttpSignedHeader(request, requestBody, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#lambda.joinWithComma}}{{#allParams}}{{^notRequiredOrIsNullable}}{{#lambda.required}}{{{dataType}}}{{/lambda.required}}{{/notRequiredOrIsNullable}}{{#notRequiredOrIsNullable}}{{#lambda.optional}}{{{dataType}}}{{/lambda.optional}}{{/notRequiredOrIsNullable}} {{paramName}}{{^requiredAndNotNullable}} = null{{/requiredAndNotNullable}} {{/allParams}}System.Threading.CancellationToken? cancellationToken = null{{/lambda.joinWithComma}}
{{#lambda.joinWithComma}}{{#allParams}}{{^notRequiredOrIsNullable}}{{#lambda.required}}{{{dataType}}}{{/lambda.required}}{{/notRequiredOrIsNullable}}{{#notRequiredOrIsNullable}}{{#lambda.optional}}{{{dataType}}}{{/lambda.optional}}{{/notRequiredOrIsNullable}} {{paramName}}{{^requiredAndNotNullable}} = null{{/requiredAndNotNullable}} {{/allParams}}System.Threading.CancellationToken cancellationToken = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}}{{/lambda.joinWithComma}}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ namespace {{packageName}}.{{clientPackage}} {{^netStandard}}
for (int i = 0; i < _tokens.Length; i++)
_tokens[i].TokenBecameAvailable += ((sender) => AvailableTokens.Writer.TryWrite((TTokenBase) sender));
}

internal override async System.Threading.Tasks.ValueTask<TTokenBase> GetAsync(System.Threading.CancellationToken? cancellation = null)
=> await AvailableTokens.Reader.ReadAsync(cancellation.GetValueOrDefault()).ConfigureAwait(false);
internal override async System.Threading.Tasks.ValueTask<TTokenBase> GetAsync(System.Threading.CancellationToken cancellation = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}})
=> await AvailableTokens.Reader.ReadAsync(cancellation).ConfigureAwait(false);
}
} {{/netStandard}}{{#netStandard}}
{
Expand Down Expand Up @@ -77,7 +76,7 @@ namespace {{packageName}}.{{clientPackage}} {{^netStandard}}
}
}

internal override async System.Threading.Tasks.ValueTask<TTokenBase> GetAsync(System.Threading.CancellationToken? cancellation = null)
internal override async System.Threading.Tasks.ValueTask<TTokenBase> GetAsync(System.Threading.CancellationToken cancellation = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}})
{
await _semaphore.WaitAsync().ConfigureAwait(false);

Expand Down Expand Up @@ -105,4 +104,4 @@ namespace {{packageName}}.{{clientPackage}} {{^netStandard}}
}
}
}
} {{/netStandard}}
}{{/netStandard}}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace {{packageName}}
/// </summary>
protected TTokenBase[] _tokens;

internal abstract System.Threading.Tasks.ValueTask<TTokenBase> GetAsync(System.Threading.CancellationToken? cancellation = null);
internal abstract System.Threading.Tasks.ValueTask<TTokenBase> GetAsync(System.Threading.CancellationToken cancellation = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}});

/// <summary>
/// Instantiates a TokenProvider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ namespace {{packageName}}.{{apiPackage}}

tokenBaseLocalVars.Add(httpSignatureTokenLocalVar);

string requestBodyLocalVar = await httpRequestMessageLocalVar.Content{{nrt!}}.ReadAsStringAsync({{^netStandard}}{{^netcoreapp3.1}}cancellationToken.GetValueOrDefault(){{/netcoreapp3.1}}{{/netStandard}}).ConfigureAwait(false);
string requestBodyLocalVar = await httpRequestMessageLocalVar.Content{{nrt!}}.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);

httpSignatureTokenLocalVar.UseInHeader(httpRequestMessageLocalVar, requestBodyLocalVar, cancellationToken);{{/isHttpSignature}}{{/authMethods}}{{#consumes}}{{#-first}}

Expand Down Expand Up @@ -407,11 +407,11 @@ namespace {{packageName}}.{{apiPackage}}

DateTime requestedAtLocalVar = DateTime.UtcNow;

using (HttpResponseMessage httpResponseMessageLocalVar = await HttpClient.SendAsync(httpRequestMessageLocalVar, cancellationToken.GetValueOrDefault()).ConfigureAwait(false))
using (HttpResponseMessage httpResponseMessageLocalVar = await HttpClient.SendAsync(httpRequestMessageLocalVar, cancellationToken).ConfigureAwait(false))
{
OnApiResponded(new ApiResponseEventArgs(requestedAtLocalVar, DateTime.UtcNow, httpResponseMessageLocalVar.StatusCode, "{{path}}", uriBuilderLocalVar.Path));

string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync({{^netStandard}}{{^netcoreapp3.1}}cancellationToken.GetValueOrDefault(){{/netcoreapp3.1}}{{/netStandard}}).ConfigureAwait(false);
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);

ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}object{{/returnType}}> apiResponseLocalVar = new ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}object{{/returnType}}>(httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, _jsonSerializerOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface IAnotherFakeApi : IApi
/// <param name="modelClient">client model</param>
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
/// <returns>Task&lt;ApiResponse&lt;ModelClient&gt;&gt;</returns>
Task<ApiResponse<ModelClient>> Call123TestSpecialTagsAsync(ModelClient modelClient, System.Threading.CancellationToken? cancellationToken = null);
Task<ApiResponse<ModelClient>> Call123TestSpecialTagsAsync(ModelClient modelClient, System.Threading.CancellationToken cancellationToken = default);

/// <summary>
/// To test special tags
Expand All @@ -50,7 +50,7 @@ public interface IAnotherFakeApi : IApi
/// <param name="modelClient">client model</param>
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
/// <returns>Task&lt;ApiResponse&gt;ModelClient&gt;?&gt;</returns>
Task<ApiResponse<ModelClient>?> Call123TestSpecialTagsOrDefaultAsync(ModelClient modelClient, System.Threading.CancellationToken? cancellationToken = null);
Task<ApiResponse<ModelClient>?> Call123TestSpecialTagsOrDefaultAsync(ModelClient modelClient, System.Threading.CancellationToken cancellationToken = default);
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ protected virtual void OnErrorCall123TestSpecialTags(Exception exception, string
/// <param name="modelClient">client model</param>
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
/// <returns><see cref="Task"/>&lt;<see cref="ApiResponse{T}"/>&gt; where T : <see cref="ModelClient"/></returns>
public async Task<ApiResponse<ModelClient>?> Call123TestSpecialTagsOrDefaultAsync(ModelClient modelClient, System.Threading.CancellationToken? cancellationToken = null)
public async Task<ApiResponse<ModelClient>?> Call123TestSpecialTagsOrDefaultAsync(ModelClient modelClient, System.Threading.CancellationToken cancellationToken = default)
{
try
{
Expand All @@ -193,7 +193,7 @@ protected virtual void OnErrorCall123TestSpecialTags(Exception exception, string
/// <param name="modelClient">client model</param>
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
/// <returns><see cref="Task"/>&lt;<see cref="ApiResponse{T}"/>&gt; where T : <see cref="ModelClient"/></returns>
public async Task<ApiResponse<ModelClient>> Call123TestSpecialTagsAsync(ModelClient modelClient, System.Threading.CancellationToken? cancellationToken = null)
public async Task<ApiResponse<ModelClient>> Call123TestSpecialTagsAsync(ModelClient modelClient, System.Threading.CancellationToken cancellationToken = default)
{
UriBuilder uriBuilderLocalVar = new UriBuilder();

Expand Down Expand Up @@ -238,11 +238,11 @@ public async Task<ApiResponse<ModelClient>> Call123TestSpecialTagsAsync(ModelCli

DateTime requestedAtLocalVar = DateTime.UtcNow;

using (HttpResponseMessage httpResponseMessageLocalVar = await HttpClient.SendAsync(httpRequestMessageLocalVar, cancellationToken.GetValueOrDefault()).ConfigureAwait(false))
using (HttpResponseMessage httpResponseMessageLocalVar = await HttpClient.SendAsync(httpRequestMessageLocalVar, cancellationToken).ConfigureAwait(false))
{
OnApiResponded(new ApiResponseEventArgs(requestedAtLocalVar, DateTime.UtcNow, httpResponseMessageLocalVar.StatusCode, "/another-fake/dummy", uriBuilderLocalVar.Path));

string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync(cancellationToken.GetValueOrDefault()).ConfigureAwait(false);
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);

ApiResponse<ModelClient> apiResponseLocalVar = new ApiResponse<ModelClient>(httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, _jsonSerializerOptions);

Expand Down
Loading