[C#][httpclient] Honor Configuration.RemoteCertificateValidationCallback in ApiClient

NA
1d ago
+372-12

Summary

The C# httpclient library now applies Configuration.RemoteCertificateValidationCallback to the HttpClientHandler it uses, the same way it already applies Proxy and ClientCertificates.

Problem

Configuration.RemoteCertificateValidationCallback is a public property shared by all C# libraries, and the RestSharp-based client honors it. The httpclient library accepted the setting but never used it, so it was silently ignored.

  • If a consumer passes their own HttpClient and HttpClientHandler to ApiClient(HttpClient, HttpClientHandler), they can set ServerCertificateCustomValidationCallback on that handler themselves.
  • In the normal flow, new ApiClient(basePath) or an API class built from a Configuration, the client creates its own handler internally. There was no way to apply a certificate validation callback, and setting it on Configuration did nothing.
    For code that relies on Configuration.RemoteCertificateValidationCallback, for example when moving from the RestSharp client to httpclient, certificate validation therefore silently stopped working.

Change

In libraries/httpclient/ApiClient.mustache, when Configuration.RemoteCertificateValidationCallback is non-null:

  • it is assigned to HttpClientHandler.ServerCertificateCustomValidationCallback;
  • it is only assigned if the handler does not already have a callback, so a callback the consumer set on their own handler is not overwritten (handler properties are frozen after the first request);
  • an InvalidOperationException is thrown if the client was created without an HttpClientHandler, consistent with the existing Proxy and ClientCertificates handling.
    Nothing changes when the property is not set, so existing consumers are unaffected.

Usage

var config = new Configuration
{
BasePath = "https://localhost:5001",
RemoteCertificateValidationCallback = (request, certificate, chain, errors) => true // development only
};
var api = new PetApi(config);

Files

  • modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache
  • Regenerated samples/client/petstore/csharp/httpclient/**/ApiClient.cs

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @devhl-labs @mandrean @shibayan @Blackclaws @lucamazzanti
  • (https://github.com/openapitools/openapi-generator/#62---openapi-generator-technical-committee) members, so they are more likely to review the pull request.

Summary by cubic

The C# httpclient library now applies Configuration.RemoteCertificateValidationCallback to its internal HttpClientHandler, so certificate validation no longer silently stops working when migrating from the RestSharp client.

  • Applies the callback only when the handler does not already have one, and refreshes it on every request so a replaced Configuration is honored; a cleared callback falls back to default validation.
  • Uses an Interlocked guard so the handler callback is installed exactly once, and reads a volatile field so later callback changes stay effective.
  • Throws an InvalidOperationException when the client was created without an HttpClientHandler, a NotSupportedException when targeting .NET Framework 4.7, and a clear InvalidOperationException when the handler has already sent requests and its properties are frozen.
  • Existing consumers are unaffected when the property is not set; the handler's own ServerCertificateCustomValidationCallback is never overwritten.