Swagger-codegen: Setting Configuration.DefaultApiClient is ignored

Created on 26 Jan 2020  路  3Comments  路  Source: swagger-api/swagger-codegen

The code below, from Configuration.cs is causing problems.

public static ApiClient DefaultApiClient = new ApiClient();

because ApiClient is defined like:

public ApiClient(String basePath="/")

When I want to set my valid url, static DefaultApiClient initialise ApiClient with "/" and complains about it.

var baseUrl = "my-valid-url";
Configuration.DefaultApiClient = new ApiClient(baseUrl);

Unhandled exception. System.TypeInitializationException: The type initializer for 'IO.Swagger.Client.Configuration' threw an exception.
---> System.UriFormatException: Invalid URI: The format of the URI could not be determined.

Possible fix, define it like this in Configuration.cs:

public static ApiClient DefaultApiClient;

Most helpful comment

I think the problem is the side effect of the Configuration.Timeout property. It forces the ApiClientcreation which forces RestClient creation with default base path which is always "/".

The above prevents creating Configuration instance from scratch because RestClient expects absolute Uri for base path.

Configuration constructor must not have such side effects.

All 3 comments

I think the problem is the side effect of the Configuration.Timeout property. It forces the ApiClientcreation which forces RestClient creation with default base path which is always "/".

The above prevents creating Configuration instance from scratch because RestClient expects absolute Uri for base path.

Configuration constructor must not have such side effects.

This is a pretty nasty issue. It prevents me from using the generated code as-is, without having to fix it manually. I don't know how this passes any unit tests or even a manual smoke test. Unless you specify Servers (base path), you will get / for basePath and there is no way to get an instance of Configuration without encountering this exception.

In short, the generated C# code does not work.

I did a fix of this one in swagger-codegen-generators repo, PR https://github.com/swagger-api/swagger-codegen-generators/pull/664

Was this page helpful?
0 / 5 - 0 ratings