OpenAPITools / OpenAPITools/openapi-generator
[BUG][csharp-netcore] No provision to pass cookies to the ApiClient
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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);
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache and inspect how Configuration.DefaultHeaders are added to requests. Verify that a cookie header is converted into request cookies while other headers, such as origin, continue to be added normally. Done means cookies supplied through DefaultHeaders are present in the generated client's request.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100