Skip to content

[csharp] Circular dependency prevents ApiClient instantiation #1893

@jimschubert

Description

@jimschubert

Generated csharp-client from current http://editor.swagger.io for the petstore-simple example.

Demo of the problem here: https://github.com/jimschubert/bug-swagger-codegen-cscirc

git clone https://github.com/jimschubert/bug-swagger-codegen-cscirc
cd bug-swagger-codegen-cscirc
git checkout 58400545d5 # build & run -> broken

Build and run this commit and you'll receive the following exception:

System.TypeInitializationException was unhandled
  HResult=-2146233036
  Message=The type initializer for 'IO.Swagger.Client.ApiClient' threw an exception.
  Source=PetStoreSimple
  TypeName=IO.Swagger.Client.ApiClient
  StackTrace:
       at IO.Swagger.Client.ApiClient..ctor(String basePath)
       at IO.Swagger.Api.DefaultApi..ctor(String basePath) in C:\Users\jrs\Desktop\bug swagger-codegen\csharp-client\ConsoleApplication\PetStoreSimple\IO\Swagger\Api\DefaultApi.cs:line 195
       at ConsoleApplication.Program.Main(String[] args) in C:\Users\jrs\Desktop\bug swagger-codegen\csharp-client\ConsoleApplication\ConsoleApplication\Program.cs:line 14
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2146233036
       Message=The type initializer for 'IO.Swagger.Client.Configuration' threw an exception.
       Source=PetStoreSimple
       TypeName=IO.Swagger.Client.Configuration
       StackTrace:
            at IO.Swagger.Client.ApiClient..cctor() in C:\Users\jrs\Desktop\bug swagger-codegen\csharp-client\ConsoleApplication\PetStoreSimple\IO\Swagger\Client\ApiClient.cs:line 64
       InnerException: 
            HResult=-2147467261
            Message=Object reference not set to an instance of an object.
            Source=PetStoreSimple
            StackTrace:
                 at IO.Swagger.Client.Configuration.set_Timeout(Int32 value) in C:\Users\jrs\Desktop\bug swagger-codegen\csharp-client\ConsoleApplication\PetStoreSimple\IO\Swagger\Client\Configuration.cs:line 95
                 at IO.Swagger.Client.Configuration..ctor(ApiClient apiClient, Dictionary`2 defaultHeader, String username, String password, String accessToken, Dictionary`2 apiKey, Dictionary`2 apiKeyPrefix, String tempFolderPath, String dateTimeFormat, Int32 timeout) in C:\Users\jrs\Desktop\bug swagger-codegen\csharp-client\ConsoleApplication\PetStoreSimple\IO\Swagger\Client\Configuration.cs:line 58
                 at IO.Swagger.Client.Configuration..cctor() in C:\Users\jrs\Desktop\bug swagger-codegen\csharp-client\ConsoleApplication\PetStoreSimple\IO\Swagger\Client\Configuration.cs:line 83
            InnerException: 

The problem is that ApiClient(string) uses Configuration.Default, which attempts to construct a new ApiClient (... then Configuration.Default). The exception is thrown on setting the Timeout property of Configuration, which doesn't perform guards against the ApiClient property not existing.

I'm able to work around this (see jimschubert/bug-swagger-codegen-cscirc@d9e7e7e), but I think it should just work.

NOTE: There's a separate issue if you look at the commits of the demo, but it's related to the example for petstore-simple generating against and older version of the api. The exception is unrelated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions