[C#][httpclient] Honor Configuration.RemoteCertificateValidationCallback in ApiClient
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
HttpClientandHttpClientHandlertoApiClient(HttpClient, HttpClientHandler), they can setServerCertificateCustomValidationCallbackon that handler themselves. - In the normal flow,
new ApiClient(basePath)or an API class built from aConfiguration, the client creates its own handler internally. There was no way to apply a certificate validation callback, and setting it onConfigurationdid nothing.
For code that relies onConfiguration.RemoteCertificateValidationCallback, for example when moving from the RestSharp client tohttpclient, 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
InvalidOperationExceptionis thrown if the client was created without anHttpClientHandler, consistent with the existingProxyandClientCertificateshandling.
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:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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
Configurationis honored; a cleared callback falls back to default validation. - Uses an
Interlockedguard so the handler callback is installed exactly once, and reads a volatile field so later callback changes stay effective. - Throws an
InvalidOperationExceptionwhen the client was created without anHttpClientHandler, aNotSupportedExceptionwhen targeting .NET Framework 4.7, and a clearInvalidOperationExceptionwhen 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
ServerCertificateCustomValidationCallbackis never overwritten.