grape-swagger icon indicating copy to clipboard operation
grape-swagger copied to clipboard

After updating grape to 1.2.5 target_class options seems not working

Open jozarz opened this issue 6 years ago • 2 comments

Hello! After we updated grape to 1.2.5 we can no longer mount grape (inside rails application), here is minimal failing example:

# frozen_string_literal: true

module API
  module V2
    class Root < Grape::API
      get :path do
      end
    end

    class Documentation < Grape::API
      add_swagger_documentation(
        target_class: ::API::V2::Root,
      )
    end
  end
end

and here is stack trace:

8: from /.../.rvm/gems/ruby-2.6.5/gems/grape-1.2.5/lib/grape/api.rb:40:in `block (2 levels) in override_all_methods!'
7: from /.../.rvm/gems/ruby-2.6.5/gems/grape-1.2.5/lib/grape/api.rb:144:in `add_setup'
6: from /.../.rvm/gems/ruby-2.6.5/gems/grape-1.2.5/lib/grape/api.rb:144:in `each'
5: from /.../.rvm/gems/ruby-2.6.5/gems/grape-1.2.5/lib/grape/api.rb:145:in `block in add_setup'
4: from /.../.rvm/gems/ruby-2.6.5/gems/grape-1.2.5/lib/grape/api.rb:153:in `replay_step_on'
3: from /.../.rvm/gems/ruby-2.6.5/gems/grape-swagger-0.33.0/lib/grape-swagger.rb:129:in `add_swagger_documentation'
2: from /.../.rvm/gems/ruby-2.6.5/gems/grape-swagger-0.33.0/lib/grape-swagger.rb:27:in `combine_routes'
1: from /.../.rvm/gems/ruby-2.6.5/gems/grape-swagger-0.33.0/lib/grape-swagger.rb:27:in `each'
/.../.rvm/gems/ruby-2.6.5/gems/grape-swagger-0.33.0/lib/grape-swagger.rb:41:in `block in combine_routes': undefined method `unshift' for nil:NilClass (NoMethodError)

jozarz avatar Dec 05 '19 13:12 jozarz

the target_class option was an always undocumented feature, introduced uneeded complexity and was broken after the last dependency updates

to achiev the same behaviour, documenting only a specific Api class, simple move the add_swagger_documentation into it

besides that, an PR to fix it is always welcome

LeFnord avatar Feb 19 '20 10:02 LeFnord

Hello I tried to build an separated endpoint with specific auth checks.

module API
  class Root < Grape::API
    get :path do; end
  end

  class Documentation < Grape::API
    before do
      # token check
    end
    
    add_swagger_documentation(target_class: ::API::Root)
  end
end

I've come across a similar issue with the target_class option in grape-swagger, as discussed in this thread. Specifically, I noticed that methods like @target_class.combined_routes return a new object on each invocation. This seems to be a result of the evaluate_arguments method in Grape causing mutations (grape/api.rb#L178-L190) that affect in the grape-swagger (for example here).

This behavior implies that any modifications to the objects returned by @target_class methods are lost, as each call generates a new object.

My setup: grape (1.8.0), grape-entity (1.0.0), grape-swagger (1.6.1), grape-swagger-entity (0.5.2).

numbata avatar Nov 23 '23 14:11 numbata