Search code examples
c#sslopenapi-generator

How-to use C# openapi-generator without SSL


I'm using openapi-generator to create a C# class for accessing a REST API. This REST API is http and does not have a SSL certificate.

The generated C# class was done by this command

java -jar ./openapi-generator-cli.jar generate 
-g csharp 
-i ../../openapi.yaml 
-o cn_http_client_csharp 
--additional-properties=artifactId=http-client 
--additional-properties=io.swagger.parser.util.RemoteUrl.trustAll=true 
--additional-properties=io.swagger.v3.parser.util.RemoteUrl.trustAll=true 
--skip-validate-spec

When testing this class with another C# application, I always get the following exception "The SSL connection could not be established, see inner exception."

I tried to use some tipps from openapi-generator wiki, but I can't fix this issue


Solution

  • I found solution.

    • updated my local openapi generator to version 7.0.1
    • generate solution by shell script by pointed targetFramework net6.0
    #!/bin/bash
    export JAVA_OPTS="-Dio.swagger.parser.util.RemoteUrl.trustAll=true -Dio.swagger.v3.parser.util.RemoteUrl.trustAll=true"
    java -jar /home/user/Projects/OpenApiGenerator/openapi-generator-cli-7.0.1.jar generate -g csharp -i /home/user/Projects/swagger.json -o /home/user/Projects/SwaggerCodeGen --additional-properties=useDateTimeOffset=true,targetFramework=net6.0
    
    • then add builded .dll's in my project
    • create apiInstance
    var apiInstance = new AuthApi(new Configuration { 
                BasePath = _gatewayBasePath, 
                RemoteCertificateValidationCallback = (_, _, _, _) => { return true;}
            });
    
    • Warning: this solution not work with asynchronous methods of apiInstance, because openapi repository have bug