Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version Mounting sees the last mounted version only #1682

Closed
kmhosny opened this issue Sep 18, 2017 · 7 comments
Closed

Version Mounting sees the last mounted version only #1682

kmhosny opened this issue Sep 18, 2017 · 7 comments
Labels

Comments

@kmhosny
Copy link

kmhosny commented Sep 18, 2017

Hi,

im trying to mount 3 different versions of API into my app, i followed the guideline but there seems to be an odd behavior, only the last mounted version gets accepted, the other versions throws invalid header version error, i tried to debug in grape's code of Header class i found that the versions array is only the last mounted version only.
it's a Rails project and my files are as below:
1- in config/routes i add the following line mount Api => '/api'
2- in app/api/api.rb

class Api < Grape::API
  format :json
  mount V1::Base
  mount V3::Base
  mount V2::Base
  add_swagger_documentation format: :json,
                            api_version: 'v1',
                            base_path: "http://#{Rails.application.secrets.host}/api",
                            hide_documentation_path: true
end

3- inside each of the V{VERSION} classes i add the version
V3::Base
version 'v3', using: :header, vendor: 'X'
V2::Base
version 'v2', using: :header, vendor: 'X'
V1::Base
version 'v1', using: :path

then in each version base class i mount the endpoints. for example

module V2
  class Base < Grape::API
    version 'v2', using: :header, vendor: 'X'
    use GrapeLogging::Logger
    helpers V2::Helpers
    include Shared::RescueHelpers
    # Mount endpoints here
    mount V2::Endpoints::Lookups
   end
end

is there something missing i need to add?

@jpmedforth
Copy link

👍

getting a similar issues. We have v1 endpoints.

class API::V1 < Grape::API
  version 'v1'

  # Mount components
  mount API::MyEndpoints
end

We also have an non versioned ping endpoints API::Ping . I mount them like:

# Mount root API to app
Grape::App.instance_eval do
  format :json

  # Mounts
  mount API::Ping
  mount API::V1
end

However the routes show my ping endpoints with a version.

@mehlah
Copy link

mehlah commented Sep 20, 2017

It seems to be breaking our specs only starting from 1.0.1. Works fine on 1.0.0

Code to it's bare minimum:

module Api
  module V5
    class Base < Grape::API
      version ['v5_1', 'v5']

      mount Api::V5::Version
    end
  end
end

module Api
  module V5_1
    class Base < Grape::API
      version 'v5_1'

      mount Api::V5_1::Version
    end
  end
end

class Api::V5::Version < Grape::API
  resource :version do
    desc 'Return the current API version'
    get do
      { version: version }
    end
  end
end

class Api::V5_1::Version < Api::V5::Version
end

# a shared spec example
shared_examples '/v5/version' do
  it 'returns the current API version' do
    get api_path(version, 'version')

    expect(response.body).to eq({ version: version }.to_json)
  end
end

describe '/api/v5/version' do
  it_behaves_like '/v5/version' do
    let(:version) { 'v5' }
  end
end

describe '/api/v5_1/version' do
  it_behaves_like '/v5/version' do
    let(:version) { 'v5_1' }
  end
end

RSpec failure

Failures:

  1) /api/v5/version behaves like /v5/version returns the current API version
     Failure/Error: get api_path(version, 'version')
     
     ActionController::RoutingError:
       No route matches [GET] "/api/v5/version"
     Shared Example Group: "/v5/version" called from ./spec/api/v5/version_spec.rb:4
     # ./spec/api/shared/v5/version.rb:5:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'

Finished in 1.06 seconds (files took 0.78781 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/api/v5/version_spec.rb:4 # /api/v5/version behaves like /v5/version returns the current API version

@dblock
Copy link
Member

dblock commented Sep 20, 2017

Please PR a failing spec, hopefully with a fix.

@dblock dblock added the bug? label Sep 20, 2017
@kmhosny
Copy link
Author

kmhosny commented Sep 20, 2017

@dblock what i noticed is, since im mounting the same APIs - same path - with different header versions then i run rake grape:routes only 1 version of the APIs mounted gets registered.

@dblock
Copy link
Member

dblock commented Sep 20, 2017

Is this a dup of #570 ?

@kmhosny
Copy link
Author

kmhosny commented Sep 21, 2017

After reading the thread, yes it is.

@dblock
Copy link
Member

dblock commented Sep 21, 2017

Closing as dup, please move any relevant info to #570.

@dblock dblock closed this as completed Sep 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants