swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Setting Configuration.DefaultApiClient is ignored

Open michalkubis opened this issue 6 years ago • 3 comments

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;

michalkubis avatar Jan 26 '20 12:01 michalkubis

I think the problem is the side effect of the Configuration.Timeout property. It forces the ApiClient creation 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.

dmilov avatar Apr 24 '20 13:04 dmilov

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.

hyankov avatar Aug 07 '20 03:08 hyankov

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

dmilov avatar Sep 10 '20 05:09 dmilov