Skip to content

Commit 1a458d3

Browse files
fix: memory leak with restclient (#13961) (#16365)
1 parent 656d2c2 commit 1a458d3

11 files changed

Lines changed: 1614 additions & 1592 deletions

File tree

  • modules/openapi-generator/src/main/resources/csharp
  • samples/client
    • echo_api/csharp-restsharp/src/Org.OpenAPITools/Client
    • others/csharp-complex-files/src/Org.OpenAPITools/Client
    • petstore
      • csharp-restsharp-name-parameter-mappings/src/Org.OpenAPITools/Client
      • csharp
        • OpenAPIClient-ConditionalSerialization/src/Org.OpenAPITools/Client
        • OpenAPIClient-net47/src/Org.OpenAPITools/Client
        • OpenAPIClient-net48/src/Org.OpenAPITools/Client
        • OpenAPIClient-net5.0/src/Org.OpenAPITools/Client
        • OpenAPIClientCoreAndNet47/src/Org.OpenAPITools/Client
        • OpenAPIClientCore/src/Org.OpenAPITools/Client
        • OpenAPIClient/src/Org.OpenAPITools/Client

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

Lines changed: 162 additions & 160 deletions
Large diffs are not rendered by default.

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

Lines changed: 127 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -455,86 +455,87 @@ private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IRea
455455
RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
456456
};
457457

458-
RestClient client = new RestClient(clientOptions,
459-
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
460-
461-
InterceptRequest(request);
462-
463-
RestResponse<T> response;
464-
if (RetryConfiguration.RetryPolicy != null)
458+
using (RestClient client = new RestClient(clientOptions,
459+
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration))))
465460
{
466-
var policy = RetryConfiguration.RetryPolicy;
467-
var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
468-
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
461+
InterceptRequest(request);
462+
463+
RestResponse<T> response;
464+
if (RetryConfiguration.RetryPolicy != null)
469465
{
470-
ErrorException = policyResult.FinalException
471-
};
472-
}
473-
else
474-
{
475-
response = client.Execute<T>(request);
476-
}
466+
var policy = RetryConfiguration.RetryPolicy;
467+
var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
468+
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
469+
{
470+
ErrorException = policyResult.FinalException
471+
};
472+
}
473+
else
474+
{
475+
response = client.Execute<T>(request);
476+
}
477477

478-
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
479-
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
480-
{
481-
try
478+
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
479+
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
482480
{
483-
response.Data = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
481+
try
482+
{
483+
response.Data = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
484+
}
485+
catch (Exception ex)
486+
{
487+
throw ex.InnerException != null ? ex.InnerException : ex;
488+
}
484489
}
485-
catch (Exception ex)
490+
else if (typeof(T).Name == "Stream") // for binary response
486491
{
487-
throw ex.InnerException != null ? ex.InnerException : ex;
492+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
493+
}
494+
else if (typeof(T).Name == "Byte[]") // for byte response
495+
{
496+
response.Data = (T)(object)response.RawBytes;
497+
}
498+
else if (typeof(T).Name == "String") // for string response
499+
{
500+
response.Data = (T)(object)response.Content;
488501
}
489-
}
490-
else if (typeof(T).Name == "Stream") // for binary response
491-
{
492-
response.Data = (T)(object)new MemoryStream(response.RawBytes);
493-
}
494-
else if (typeof(T).Name == "Byte[]") // for byte response
495-
{
496-
response.Data = (T)(object)response.RawBytes;
497-
}
498-
else if (typeof(T).Name == "String") // for string response
499-
{
500-
response.Data = (T)(object)response.Content;
501-
}
502502

503-
InterceptResponse(request, response);
503+
InterceptResponse(request, response);
504504

505-
var result = ToApiResponse(response);
506-
if (response.ErrorMessage != null)
507-
{
508-
result.ErrorText = response.ErrorMessage;
509-
}
505+
var result = ToApiResponse(response);
506+
if (response.ErrorMessage != null)
507+
{
508+
result.ErrorText = response.ErrorMessage;
509+
}
510510

511-
if (response.Cookies != null && response.Cookies.Count > 0)
512-
{
513-
if (result.Cookies == null) result.Cookies = new List<Cookie>();
514-
foreach (var restResponseCookie in response.Cookies.Cast<Cookie>())
511+
if (response.Cookies != null && response.Cookies.Count > 0)
515512
{
516-
var cookie = new Cookie(
517-
restResponseCookie.Name,
518-
restResponseCookie.Value,
519-
restResponseCookie.Path,
520-
restResponseCookie.Domain
521-
)
513+
if (result.Cookies == null) result.Cookies = new List<Cookie>();
514+
foreach (var restResponseCookie in response.Cookies.Cast<Cookie>())
522515
{
523-
Comment = restResponseCookie.Comment,
524-
CommentUri = restResponseCookie.CommentUri,
525-
Discard = restResponseCookie.Discard,
526-
Expired = restResponseCookie.Expired,
527-
Expires = restResponseCookie.Expires,
528-
HttpOnly = restResponseCookie.HttpOnly,
529-
Port = restResponseCookie.Port,
530-
Secure = restResponseCookie.Secure,
531-
Version = restResponseCookie.Version
532-
};
533-
534-
result.Cookies.Add(cookie);
516+
var cookie = new Cookie(
517+
restResponseCookie.Name,
518+
restResponseCookie.Value,
519+
restResponseCookie.Path,
520+
restResponseCookie.Domain
521+
)
522+
{
523+
Comment = restResponseCookie.Comment,
524+
CommentUri = restResponseCookie.CommentUri,
525+
Discard = restResponseCookie.Discard,
526+
Expired = restResponseCookie.Expired,
527+
Expires = restResponseCookie.Expires,
528+
HttpOnly = restResponseCookie.HttpOnly,
529+
Port = restResponseCookie.Port,
530+
Secure = restResponseCookie.Secure,
531+
Version = restResponseCookie.Version
532+
};
533+
534+
result.Cookies.Add(cookie);
535+
}
535536
}
537+
return result;
536538
}
537-
return result;
538539
}
539540

540541
private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
@@ -550,75 +551,76 @@ private ApiResponse<T> Exec<T>(RestRequest request, RequestOptions options, IRea
550551
UseDefaultCredentials = configuration.UseDefaultCredentials
551552
};
552553

553-
RestClient client = new RestClient(clientOptions,
554-
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)));
555-
556-
InterceptRequest(request);
557-
558-
RestResponse<T> response;
559-
if (RetryConfiguration.AsyncRetryPolicy != null)
554+
using (RestClient client = new RestClient(clientOptions,
555+
configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration))))
560556
{
561-
var policy = RetryConfiguration.AsyncRetryPolicy;
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)
557+
InterceptRequest(request);
558+
559+
RestResponse<T> response;
560+
if (RetryConfiguration.AsyncRetryPolicy != null)
564561
{
565-
ErrorException = policyResult.FinalException
566-
};
567-
}
568-
else
569-
{
570-
response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
571-
}
562+
var policy = RetryConfiguration.AsyncRetryPolicy;
563+
var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
564+
response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) : new RestResponse<T>(request)
565+
{
566+
ErrorException = policyResult.FinalException
567+
};
568+
}
569+
else
570+
{
571+
response = await client.ExecuteAsync<T>(request, cancellationToken).ConfigureAwait(false);
572+
}
572573

573-
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
574-
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
575-
{
576-
response.Data = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
577-
}
578-
else if (typeof(T).Name == "Stream") // for binary response
579-
{
580-
response.Data = (T)(object)new MemoryStream(response.RawBytes);
581-
}
582-
else if (typeof(T).Name == "Byte[]") // for byte response
583-
{
584-
response.Data = (T)(object)response.RawBytes;
585-
}
574+
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
575+
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
576+
{
577+
response.Data = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
578+
}
579+
else if (typeof(T).Name == "Stream") // for binary response
580+
{
581+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
582+
}
583+
else if (typeof(T).Name == "Byte[]") // for byte response
584+
{
585+
response.Data = (T)(object)response.RawBytes;
586+
}
586587

587-
InterceptResponse(request, response);
588+
InterceptResponse(request, response);
588589

589-
var result = ToApiResponse(response);
590-
if (response.ErrorMessage != null)
591-
{
592-
result.ErrorText = response.ErrorMessage;
593-
}
590+
var result = ToApiResponse(response);
591+
if (response.ErrorMessage != null)
592+
{
593+
result.ErrorText = response.ErrorMessage;
594+
}
594595

595-
if (response.Cookies != null && response.Cookies.Count > 0)
596-
{
597-
if (result.Cookies == null) result.Cookies = new List<Cookie>();
598-
foreach (var restResponseCookie in response.Cookies.Cast<Cookie>())
596+
if (response.Cookies != null && response.Cookies.Count > 0)
599597
{
600-
var cookie = new Cookie(
601-
restResponseCookie.Name,
602-
restResponseCookie.Value,
603-
restResponseCookie.Path,
604-
restResponseCookie.Domain
605-
)
598+
if (result.Cookies == null) result.Cookies = new List<Cookie>();
599+
foreach (var restResponseCookie in response.Cookies.Cast<Cookie>())
606600
{
607-
Comment = restResponseCookie.Comment,
608-
CommentUri = restResponseCookie.CommentUri,
609-
Discard = restResponseCookie.Discard,
610-
Expired = restResponseCookie.Expired,
611-
Expires = restResponseCookie.Expires,
612-
HttpOnly = restResponseCookie.HttpOnly,
613-
Port = restResponseCookie.Port,
614-
Secure = restResponseCookie.Secure,
615-
Version = restResponseCookie.Version
616-
};
617-
618-
result.Cookies.Add(cookie);
601+
var cookie = new Cookie(
602+
restResponseCookie.Name,
603+
restResponseCookie.Value,
604+
restResponseCookie.Path,
605+
restResponseCookie.Domain
606+
)
607+
{
608+
Comment = restResponseCookie.Comment,
609+
CommentUri = restResponseCookie.CommentUri,
610+
Discard = restResponseCookie.Discard,
611+
Expired = restResponseCookie.Expired,
612+
Expires = restResponseCookie.Expires,
613+
HttpOnly = restResponseCookie.HttpOnly,
614+
Port = restResponseCookie.Port,
615+
Secure = restResponseCookie.Secure,
616+
Version = restResponseCookie.Version
617+
};
618+
619+
result.Cookies.Add(cookie);
620+
}
619621
}
622+
return result;
620623
}
621-
return result;
622624
}
623625

624626
#region IAsynchronousClient

0 commit comments

Comments
 (0)