swagger-api / swagger-api/swagger-codegen

ruby client sdk generation - host and base_path issues

Open
#10,892 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When generating a ruby client SDK from an OpenAPI V: 3.0.3 yaml api documentation, the swagger_client configuration.rb module (SwaggerClient) contains the Configuration class with the default variables host and base_path as follows:
@host = 'localhost'
@base_path = 'http://localhost:4010'

When executing any client app that uses any endpoint provided by the generated client the it fails to connect to the base_path (http://localhost:4010) the swagger client attempts to connect to the localhost port 80 instead, which ultimately fails. A mock server is running on port 4010

Swagger-codegen version

Swagger Codegen version: 3.0.24

Swagger declaration file content or url

Array of Server Objects which provide connectivity information to target servers.
Only using the localhost:4010

servers:
  - url: "http://localhost:4010"
  - url: "https://localhost:4433"
Command line used for generation

Generated the client SDK via the Swagger Editor

Steps to reproduce
irb(main):008:0> api_instance = SwaggerClient::TestApi.new
=> #<SwaggerClient::TestApi:0x00000000067f01b0 @api_client=#<SwaggerClient::ApiClient:0x00000000067f0188 @config=#<SwaggerClient::Configuration:0x0000000005e87b00 @scheme="http", @host="localhost", @base_path="http://localhost:4010", @api_key={"api-key"=>"dsfg-dsfg-sdfg-sdfg-45dfg-dfgdr44", "user-uid"=>"1234567"}, @api_key_prefix={}, @timeout=0, @client_side_validation=true, @verify_ssl=true, @verify_ssl_host=true, @params_encoding=nil, @cert_file=nil, @key_file=nil, @debugging=true, @inject_format=false, @force_ending_format=false, @logger=#<Logger:0x0000000005e879c0 @level=0, @progname=nil, @default_formatter=#<Logger::Formatter:0x0000000005e87970 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x0000000005e87920 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mon_mutex=#<Thread::Mutex:0x0000000005e878a8>, @mon_mutex_owner_object_id=49560720, @mon_owner=nil, @mon_count=0>>>, @user_agent="Swagger-Codegen/1.0.0/ruby", @default_headers={"Content-Type"=>"application/json", "User-Agent"=>"Swagger-Codegen/1.0.0/ruby"}>>
irb(main):009:0> begin
irb(main):010:1>   #Test connection to the API
irb(main):011:1>   result = api_instance.test_connection
irb(main):012:1>   p result
irb(main):013:1> rescue SwaggerClient::ApiError => e
irb(main):014:1>   puts "Exception when calling TestApi->test_connection: #{e}"
irb(main):015:1> end
D, [2021-02-03T11:29:33.843728 #4936] DEBUG -- : Calling API: TestApi.test_connection ...
  Trying 127.0.0.1:80...
  Trying ::1:80...
connect to 127.0.0.1 port 80 failed: Connection refused
connect to ::1 port 80 failed: Connection refused
Failed to connect to localhost port 80: Connection refused
Closing connection 0
D, [2021-02-03T11:29:37.098151 #4936] DEBUG -- : HTTP response body ~BEGIN~

~END~

Exception when calling TestApi->test_connection: Couldn't connect to server
HTTP status code: 0
=> nil
Related issues/PRs
Suggest a fix/enhancement

To rectify the problem the def initialize was modify as follows:

    def initialize
      @scheme = 'http'
      #@host = 'localhost'
      #@base_path = 'http://localhost:4010'
      @host = 'localhost:4010'
      @base_path = '/'       
      @api_key = {}
      @api_key_prefix = {}
      @timeout = 0
      @client_side_validation = true
      @verify_ssl = true
      @verify_ssl_host = true
      @params_encoding = nil
      @cert_file = nil
      @key_file = nil
      @debugging = false
      @inject_format = false
      @force_ending_format = false
      @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)

      yield(self) if block_given?
    End

After modifying these variables the client connects to the defined host (localhost) on the defined port (4010).
Here is the results after making the changes:

irb(main):023:0> begin
irb(main):024:1>   #Test connection to the API
irb(main):025:1>   result = api_instance.test_connection
irb(main):026:1>   p result
irb(main):027:1> rescue SwaggerClient::ApiError => e
irb(main):028:1>   puts "Exception when calling TestApi->test_connection: #{e}"
irb(main):029:1> end
D, [2021-02-03T12:03:14.694226 #18528] DEBUG -- : Calling API: TestApi.test_connection ...
  Trying 127.0.0.1:4010...
Connected to localhost (127.0.0.1) port 4010 (#0)
GET /api HTTP/1.1
Host: localhost:4010
User-Agent: Swagger-Codegen/1.0.0/ruby
Content-Type: application/json
Accept: application/json
api-key: f32516cc-57d3-43f5-9e16-8f86780a4cce
user-uid: 1647389405

Mark bundle as not supporting multiuse
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: *
Content-type: application/json
Content-Length: 45
Date: Wed, 03 Feb 2021 18:03:14 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Connection #0 to host localhost left intact
D, [2021-02-03T12:03:14.767184 #18528] DEBUG -- : HTTP response body ~BEGIN~
{"data":{"success":true},"meta":{"code":200}}
~END~

D, [2021-02-03T12:03:14.767752 #18528] DEBUG -- : API called: TestApi#test_connection
Data: #<SwaggerClient::Test:0x0000000006891c18 @meta={:code=>200}, @data={:success=>true}>
Status code: 200
Headers: {"Access-Control-Allow-Origin"=>"*", "Access-Control-Allow-Headers"=>"*", "Access-Control-Allow-Credentials"=>"true", "Access-Control-Expose-Headers"=>"*", "Content-type"=>"application/json", "Content-Length"=>"45", "Date"=>"Wed, 03 Feb 2021 18:03:14 GMT", "Connection"=>"keep-alive", "Keep-Alive"=>"timeout=5"}
#<SwaggerClient::Test:0x0000000006891c18 @meta={:code=>200}, @data={:success=>true}>
=> #<SwaggerClient::Test:0x0000000006891c18 @meta={:code=>200}, @data={:success=>true}>

Is there any miss configuration in the yaml that is causing the issue?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated swagger_client configuration.rb and the TestApi#test_connection entry point. Reproduce generation from the shown OpenAPI 3.0.3 servers entry, then trace how the Ruby client assembles the request URL. Done means requests use localhost:4010 rather than port 80 without requiring manual edits to the generated configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.