Skip to content

C# Client Exception when invoking service - An item with the same key has already been added #4931

Description

@MMirabito
Description

When using the C# API generated by swagger codeGen I receive the following exception:

Exception when calling UserProfileApi.GetBasicAuthorizationHeaderToken: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Io.Rs.User.Api.UserProfileApi.GetUserWithHttpInfo(String format, String userId, String authorization) in D:\MyProjects\Microsoft\User\src\Io.Rs.User\Api\UserProfileApi.cs:line 841
   at Io.Rs.User.Api.UserProfileApi.GetUser(String format, String userId, String authorization) in D:\MyProjects\Microsoft\User\src\Io.Rs.User\Api\UserProfileApi.cs:line 794
   at MySampleTest.Program.Main(String[] args) in D:\MyProjects\Microsoft\User\MySampleTest\Program.cs:line 43

I think the problem might be linked to the fact that my REST endpoint is structured as follows
http://localhost:8080/myapp/api/UserProfile/v1.0/getUser/{format}/{userId}

One of our path parameters is also called format which might be colliding with the internal generated code since it's using the same Dictionary object. Following is a piece of the swagger code generated notice how line 1 always executes localVarPathParams.Add("format", "json"); and adds to the dictionary localVarPathParams. Line 2 will throw the exception.

GetUser("xml", "myid", null)

......
1) localVarPathParams.Add("format", "json");
2) if (format != null) localVarPathParams.Add("format", Configuration.ApiClient.ParameterToString(format)); // path parameter
3) if (userId != null) localVarPathParams.Add("userId", Configuration.ApiClient.ParameterToString(userId)); // path parameter
4) if (authorization != null) localVarHeaderParams.Add("Authorization", Configuration.ApiClient.ParameterToString(authorization)); // header parameter
.......

NOTE: I did not notice the same issue when using a Java Client.
UPDATE 5/3/17 22:09 EST: I was mistaken, the java client has a similar issue it also uses the "format" similar to the C# client overriding my format directive to always be "JSON". When I comment out the swagger code I get the following exception from Java

Exception in thread "main" io.rs.user.ApiException: Content type "application/xml" is not supported for type: class io.rs.user.model.UserProfile
	at io.s.user.ApiClient.deserialize(ApiClient.java:856)
	at io.rs.user.ApiClient.handleResponse(ApiClient.java:1047)
	at io.rs.user.ApiClient.execute(ApiClient.java:981)
	at io.rs.user.client.UserProfileApi.getUserWithHttpInfo(UserProfileApi.java:157)
	at io.rs.user.client.UserProfileApi.getUser(UserProfileApi.java:134)
	at test.MyTest.getUser(MyTest.java:86)
	at test.MyTest.main(MyTest.java:136)
.....

// create path and map variables
String localVarPath = "/UserProfile/v1.0/getUser/{format}/{userId}".replaceAll("\\{format\\}","json")
.replaceAll("\\{" + "format" + "\\}", apiClient.escapeString(format.toString()))
.replaceAll("\\{" + "userId" + "\\}", apiClient.escapeString(userId.toString()));`
.....

Is the assumption that all clients generated with swagger Java or C# can only handle JSON? What am I doing wrong? It looks like it was deigned to behave like this

Let me know if I am doing anything wrong that could be causing the issue.

Thanks
max

Swagger-codegen version

Swagger CodeGen 2.2.1

Swagger declaration file content or url

NA

Command line used for generation

NA

Steps to reproduce

Create a JAX-RS service with a path parameter called format then generate the c# client via codegen and invoke the API

Related issues

The following link is to the API.mustache showing how the code is generated
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/csharp/api.mustache

search for "localVarPathParams.Add("format", "json");" or got to line 373

Suggest a Fix

Consider a few alternatives:

  1. Rename codegen format variable to include underscores
  2. Use a different dictionary object for internal variables
  3. Flag potential naming conflicts when running codegen

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions