Apipie / apipie-rails

Ruby on Rails API documentation tool
Apache License 2.0
2.47k stars 459 forks source link

Changing the default version ignores api_base_url #383

Open zarembas opened 9 years ago

zarembas commented 9 years ago

The following code will add "api" at the beginning of every route:

Apipie.configure do |config|
  config.api_base_url            = "/"
  config.default_version = "0.6"
end

If I change the default_version back to "1.0", the routes start with "/" again.

zarembas commented 9 years ago

Moving the default_version line before the api_base_url seems to fix this.

neilgupta commented 9 years ago

This is a bug with how the base_url is stored on a per-version basis. This should be better documented, but config.api_base_url sets the base_url for the default version, which is 1.0 when that command is executed. You can either change the default version before setting api_base_url (which you discovered) or you can run config.api_base_url["0.6"] = "/" to set it for version 0.6. Relevant function below:

# set base url for default version of API
# to set it for specific version use
# config.api_base_url[version] = url
def api_base_url=(url)
  version = Apipie.configuration.default_version
  @api_base_url[version] = url
end

For apipie maintainers, a possible fix would be to add the following to lib/apipie/configuration.rb:

def default_version=(version)
  old_default_version = Apipie.configuration.default_version

  @default_version = version

  @api_base_url[version] = @api_base_url[old_default_version] unless @api_base_url[version]
  @app_info[version] = @app_info[old_default_version] unless @app_info[version]
end