Skip to content

Commit bd76c1b

Browse files
[csharp] Update RestSharp to 110.2.0 (#16122)
* [csharp] Update RestSharp to 110.2.0 * Post './bin/generate-samples.sh bin/configs/csharp*' and './bin/utils/export_docs_generators.sh' scripts * OAuthAuthenticator: use configureSerialization * ContentType prop = RestSharp.ContentType.Json * `req` -> `request` in `Exec()` and `ExecAsync()` * Regenerate samples
1 parent 7252d1a commit bd76c1b

File tree

29 files changed

+227
-297
lines changed

29 files changed

+227
-297
lines changed

modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ namespace {{packageName}}.Client
3737
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
3838
{
3939
private readonly IReadableConfiguration _configuration;
40-
private static readonly string _contentType = "application/json";
4140
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
4241
{
4342
// OpenAPI generated types generally hide default constructors.
@@ -150,17 +149,13 @@ namespace {{packageName}}.Client
150149
public ISerializer Serializer => this;
151150
public IDeserializer Deserializer => this;
152151

153-
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept;
152+
public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
154153

155154
public SupportsContentType SupportsContentType => contentType =>
156-
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
157-
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
155+
contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
156+
contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
158157

159-
public string ContentType
160-
{
161-
get { return _contentType; }
162-
set { throw new InvalidOperationException("Not allowed to set content type."); }
163-
}
158+
public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
164159

165160
public DataFormat DataFormat => DataFormat.Json;
166161
}
@@ -434,7 +429,7 @@ namespace {{packageName}}.Client
434429
return transformed;
435430
}
436431
437-
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration)
432+
private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
438433
{
439434
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
440435
@@ -459,8 +454,8 @@ namespace {{packageName}}.Client
459454
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
460455
};
461456
462-
RestClient client = new RestClient(clientOptions)
463-
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration));
457+
RestClient client = new RestClient(clientOptions,
458+
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
464459
465460
{{#hasOAuthMethods}}
466461
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
@@ -478,22 +473,21 @@ namespace {{packageName}}.Client
478473
}
479474
480475
{{/hasOAuthMethods}}
481-
InterceptRequest(req);
476+
InterceptRequest(request);
482477
483478
RestResponse<T> response;
484479
if (RetryConfiguration.RetryPolicy != null)
485480
{
486481
var policy = RetryConfiguration.RetryPolicy;
487-
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
488-
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
482+
var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
483+
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
489484
{
490-
Request = req,
491485
ErrorException = policyResult.FinalException
492486
};
493487
}
494488
else
495489
{
496-
response = client.Execute<T>(req);
490+
response = client.Execute<T>(request);
497491
}
498492
499493
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@@ -521,7 +515,7 @@ namespace {{packageName}}.Client
521515
response.Data = (T)(object)response.Content;
522516
}
523517
524-
InterceptResponse(req, response);
518+
InterceptResponse(request, response);
525519
526520
var result = ToApiResponse(response);
527521
if (response.ErrorMessage != null)
@@ -559,7 +553,7 @@ namespace {{packageName}}.Client
559553
}
560554
561555
{{#supportsAsync}}
562-
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
556+
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
563557
{
564558
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
565559
@@ -572,8 +566,8 @@ namespace {{packageName}}.Client
572566
UseDefaultCredentials = configuration.UseDefaultCredentials
573567
};
574568
575-
RestClient client = new RestClient(clientOptions)
576-
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration));
569+
RestClient client = new RestClient(clientOptions,
570+
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
577571
578572
{{#hasOAuthMethods}}
579573
if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
@@ -591,24 +585,23 @@ namespace {{packageName}}.Client
591585
}
592586
593587
{{/hasOAuthMethods}}
594-
InterceptRequest(req);
588+
InterceptRequest(request);
595589
596590
RestResponse<T> response;
597591
{{#supportsRetry}}
598592
if (RetryConfiguration.AsyncRetryPolicy != null)
599593
{
600594
var policy = RetryConfiguration.AsyncRetryPolicy;
601-
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false);
602-
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
595+
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
596+
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
603597
{
604-
Request = req,
605598
ErrorException = policyResult.FinalException
606599
};
607600
}
608601
else
609602
{
610603
{{/supportsRetry}}
611-
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);
604+
response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
612605
{{#supportsRetry}}
613606
}
614607
{{/supportsRetry}}
@@ -627,7 +620,7 @@ namespace {{packageName}}.Client
627620
response.Data = (T)(object)response.RawBytes;
628621
}
629622
630-
InterceptResponse(req, response);
623+
InterceptResponse(request, response);
631624
632625
var result = ToApiResponse(response);
633626
if (response.ErrorMessage != null)

modules/openapi-generator/src/main/resources/csharp/auth/OAuthAuthenticator.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ namespace {{packageName}}.Client.Auth
7373
/// <returns>An authentication token.</returns>
7474
async Task<string> GetToken()
7575
{
76-
var client = new RestClient(_tokenUrl)
77-
.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration));
76+
var client = new RestClient(_tokenUrl,
77+
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(_serializerSettings, _configuration)));
7878
7979
var request = new RestRequest()
8080
.AddParameter("grant_type", _grantType)

modules/openapi-generator/src/main/resources/csharp/netcore_project.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3333
{{/useGenericHost}}
3434
{{#useRestSharp}}
35-
<PackageReference Include="RestSharp" Version="108.0.3" />
35+
<PackageReference Include="RestSharp" Version="110.2.0" />
3636
{{/useRestSharp}}
3737
{{#useGenericHost}}
3838
<PackageReference Include="Microsoft.Extensions.Http" Version="{{#lambda.first}}{{#netStandard}}5.0.0 {{/netStandard}}{{#net47}}7.0.0 {{/net47}}{{#net48}}7.0.0 {{/net48}}{{#net6.0}}6.0.0 {{/net6.0}}{{#net70OrLater}}7.0.0{{/net70OrLater}}{{/lambda.first}}" />

modules/openapi-generator/src/main/resources/csharp/nuspec.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
<dependency id="Newtonsoft.Json" version="13.0.3" />
3434
{{#useRestSharp}}
35-
<dependency id="RestSharp" version="108.0.3" />
35+
<dependency id="RestSharp" version="110.2.0" />
3636
{{/useRestSharp}}
3737
{{#useCompareNetObjects}}
3838
<dependency id="CompareNETObjects" version="4.61.0" />

samples/client/echo_api/csharp-restsharp/src/Org.OpenAPITools/Client/ApiClient.cs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ namespace Org.OpenAPITools.Client
3939
internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
4040
{
4141
private readonly IReadableConfiguration _configuration;
42-
private static readonly string _contentType = "application/json";
4342
private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
4443
{
4544
// OpenAPI generated types generally hide default constructors.
@@ -152,17 +151,13 @@ internal object Deserialize(RestResponse response, Type type)
152151
public ISerializer Serializer => this;
153152
public IDeserializer Deserializer => this;
154153

155-
public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept;
154+
public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;
156155

157156
public SupportsContentType SupportsContentType => contentType =>
158-
contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
159-
contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
157+
contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
158+
contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);
160159

161-
public string ContentType
162-
{
163-
get { return _contentType; }
164-
set { throw new InvalidOperationException("Not allowed to set content type."); }
165-
}
160+
public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;
166161

167162
public DataFormat DataFormat => DataFormat.Json;
168163
}
@@ -435,7 +430,7 @@ private ApiResponse<T> ToApiResponse<T>(RestResponse<T> response)
435430
return transformed;
436431
}
437432

438-
private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration)
433+
private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
439434
{
440435
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
441436

@@ -460,25 +455,24 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
460455
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
461456
};
462457

463-
RestClient client = new RestClient(clientOptions)
464-
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration));
458+
RestClient client = new RestClient(clientOptions,
459+
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
465460

466-
InterceptRequest(req);
461+
InterceptRequest(request);
467462

468463
RestResponse<T> response;
469464
if (RetryConfiguration.RetryPolicy != null)
470465
{
471466
var policy = RetryConfiguration.RetryPolicy;
472-
var policyResult = policy.ExecuteAndCapture(() => client.Execute(req));
473-
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
467+
var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
468+
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
474469
{
475-
Request = req,
476470
ErrorException = policyResult.FinalException
477471
};
478472
}
479473
else
480474
{
481-
response = client.Execute<T>(req);
475+
response = client.Execute<T>(request);
482476
}
483477

484478
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@@ -506,7 +500,7 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
506500
response.Data = (T)(object)response.Content;
507501
}
508502

509-
InterceptResponse(req, response);
503+
InterceptResponse(request, response);
510504

511505
var result = ToApiResponse(response);
512506
if (response.ErrorMessage != null)
@@ -543,7 +537,7 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
543537
return result;
544538
}
545539

546-
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
540+
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
547541
{
548542
var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;
549543

@@ -556,25 +550,24 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
556550
UseDefaultCredentials = configuration.UseDefaultCredentials
557551
};
558552

559-
RestClient client = new RestClient(clientOptions)
560-
.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration));
553+
RestClient client = new RestClient(clientOptions,
554+
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
561555

562-
InterceptRequest(req);
556+
InterceptRequest(request);
563557

564558
RestResponse<T> response;
565559
if (RetryConfiguration.AsyncRetryPolicy != null)
566560
{
567561
var policy = RetryConfiguration.AsyncRetryPolicy;
568-
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false);
569-
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>
562+
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
563+
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
570564
{
571-
Request = req,
572565
ErrorException = policyResult.FinalException
573566
};
574567
}
575568
else
576569
{
577-
response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);
570+
response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
578571
}
579572

580573
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
@@ -591,7 +584,7 @@ private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadabl
591584
response.Data = (T)(object)response.RawBytes;
592585
}
593586

594-
InterceptResponse(req, response);
587+
InterceptResponse(request, response);
595588

596589
var result = ToApiResponse(response);
597590
if (response.ErrorMessage != null)

samples/client/echo_api/csharp-restsharp/src/Org.OpenAPITools/Org.OpenAPITools.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<ItemGroup>
2424
<PackageReference Include="JsonSubTypes" Version="2.0.1" />
2525
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
26-
<PackageReference Include="RestSharp" Version="108.0.3" />
26+
<PackageReference Include="RestSharp" Version="110.2.0" />
2727
<PackageReference Include="Polly" Version="7.2.3" />
2828
</ItemGroup>
2929

0 commit comments

Comments
 (0)