[BUG][csharp-netcore] No provision to pass cookies to the ApiClient
Created by: vvb
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
Their is a provision to pass headers in the configuration using DefaultHeaders. But passing cookie
here does not seem to work.
For the below code, a wire capture shows the origin
header, but cookie
param is not found in the message. It gets saved into DefaultParameters but not consumed.
Org.OpenAPITools.Client.Configuration config = new Org.OpenAPITools.Client.Configuration();
var api = new Org.OpenAPITools.Api.NtpApi(config);
var url = endpoint + "ntp/Policies";
var intersightHeaders = new Dictionary<string, string>()
{
{ "cookie", "Param="+Value },
{ "origin", url }
};
foreach (var h in intersightHeaders)
{
api.Configuration.DefaultHeaders.Add(h.Key, h.Value);
}
openapi-generator version
latest master
OpenAPI declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix
We could make a change in the ApiClient, if it comes across a header with Name "Cookie", then it could do the below instead.
diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache
index 49b81c72e0..8592f55720 100644
--- a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache
+++ b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache
@@ -271,6 +271,21 @@ namespace {{packageName}}.Client
{
foreach (var headerParam in configuration.DefaultHeaders)
{
+ if (String.Equals(headerParam.Key, "cookie", StringComparison.OrdinalIgnoreCase))
+ {
+ string[] cookieSeparators = new string[] {";"};
+ string[] EqualSeparator = new string[] {"="};
+ string[] rawCookieParams = headerParam.Value.Split(cookieSeparators, StringSplitOptions.None);
+ foreach (string paramset in rawCookieParams)
+ {
+ String[] param = paramset.Split(EqualSeparator, StringSplitOptions.None);
+ if (param.Length == 2)
+ {
+ request.AddCookie(param[0], param[1]);
+ }
+ }
+ continue;
+ }
request.AddHeader(headerParam.Key, headerParam.Value);
}
}