swagger-api / swagger-api/swagger-codegen

[CSHARP] Duplicate headers causing exception

Open
#12,529 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

When returning the ApiResponse, the method ToDictionary is used to construct a response object from a raw API response. The method ToDictionary will expect only unique keys, but this is not always the case. For example, headers could repeat which would cause the code to end in an exception.

Here is an example from the generated code:

            return new ApiResponse<KeycloakTokenResponse>(localVarStatusCode,
                localVarResponse.Headers.ToDictionary(x => x.Name, x => string.Join(",", x.Value)),
                (KeycloakTokenResponse) this.Configuration.ApiClient.Deserialize(localVarResponse, typeof(KeycloakTokenResponse)));
nResponse) this.Configuration.ApiClient.Deserialize(localVarResponse, typeof(KeycloakTokenResponse)));

and here is the modification I had to make:

            return new ApiResponse<KeycloakTokenResponse>(localVarStatusCode,
                localVarResponse.Headers.GroupBy(x => x.Name)
                    .ToDictionary(g => g.Key, g => g.Select(x => x.Value).ToString()),
                (KeycloakTokenResponse) this.Configuration.ApiClient.Deserialize(localVarResponse, typeof(KeycloakTokenResponse)));

This workaround means that duplicates are allowed in the response headers. I believe this should be part of the generated code, but I am asking for any contrasting opinion.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the generated C# ApiResponse construction using localVarResponse.Headers.ToDictionary, and trace how this output is produced. Verify the generated response-header handling with repeated header names, ensuring the response no longer fails because of duplicate keys; the issue does not name a source file or test to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.