Skip to content

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

Open
nagabalaji-b wants to merge 5 commits into
OpenAPITools:masterfrom
nagabalaji-b:feature/csharp-httpclient-remote-cert-callback
Open

nagabalaji-b wants to merge 5 commits into
OpenAPITools:masterfrom
nagabalaji-b:feature/csharp-httpclient-remote-cert-callback

Conversation

@nagabalaji-b

@nagabalaji-b nagabalaji-b commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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.

Written for commit 8c5109e. Summary will update on new commits.

View guided diff

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 6 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache:517">
P1: This lazy assignment can throw after the handler has already sent a request. Configure the callback before the first request, or create a stable handler callback during client construction instead of attempting to mutate the handler on every request.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

{
if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
// HttpClientHandler properties are frozen after the first request, so only assign when not already set.
if (_httpClientHandler.ServerCertificateCustomValidationCallback == null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: This lazy assignment can throw after the handler has already sent a request. Configure the callback before the first request, or create a stable handler callback during client construction instead of attempting to mutate the handler on every request.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache, line 517:

<comment>This lazy assignment can throw after the handler has already sent a request. Configure the callback before the first request, or create a stable handler callback during client construction instead of attempting to mutate the handler on every request.</comment>

<file context>
@@ -510,6 +510,18 @@ namespace {{packageName}}.Client
+                {
+                    if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
+                    // HttpClientHandler properties are frozen after the first request, so only assign when not already set.
+                    if (_httpClientHandler.ServerCertificateCustomValidationCallback == null)
+                    {
+                        _httpClientHandler.ServerCertificateCustomValidationCallback =
</file context>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 6 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache:533">
P2: The stored callback is never cleared, so a request whose configuration has no `RemoteCertificateValidationCallback` still runs the callback from an earlier request. Also, a request using a different configuration overwrites the field that in-flight requests read. Clear the field when the configured callback is null, or document that the callback is sticky for the lifetime of the client.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

#else
// Refresh the callback on every request; the delegate installed below reads this field,
// so a later request (or a replaced Configuration) is not stuck with the first callback.
_remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The stored callback is never cleared, so a request whose configuration has no RemoteCertificateValidationCallback still runs the callback from an earlier request. Also, a request using a different configuration overwrites the field that in-flight requests read. Clear the field when the configured callback is null, or document that the callback is sticky for the lifetime of the client.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache, line 533:

<comment>The stored callback is never cleared, so a request whose configuration has no `RemoteCertificateValidationCallback` still runs the callback from an earlier request. Also, a request using a different configuration overwrites the field that in-flight requests read. Clear the field when the configured callback is null, or document that the callback is sticky for the lifetime of the client.</comment>

<file context>
@@ -513,13 +524,39 @@ namespace {{packageName}}.Client
+#else
+                    // Refresh the callback on every request; the delegate installed below reads this field,
+                    // so a later request (or a replaced Configuration) is not stuck with the first callback.
+                    _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback;
+                    // Install the handler delegate only once. HttpClientHandler properties are frozen after the
+                    // first request, so the callback must be configured before this client sends its first request
</file context>

The httpclient ApiClient accepted Configuration.RemoteCertificateValidationCallback
but never applied it, so the setting was silently ignored. The callback is now
assigned to the HttpClientHandler, with the following behavior:

- Stored in a field refreshed on each request. If a later request has no callback,
  the stored one is cleared, so default validation applies.
- Installed once; a callback set on a consumer-provided handler is not overwritten.
- Throws a clear InvalidOperationException if the handler is already frozen.
- Not supported on net47 (ServerCertificateCustomValidationCallback is unavailable there).

The handler is shared by all requests of a client, so the most recent request's
configuration wins. No change when the callback is not set.

Regenerated httpclient samples.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant