Skip to content

Investigate whether we can use Grape::API instead of Grape::API::Instance

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

From !27276 (comment 328515256), Grape v1.2.x changed the behavior of how Grape::API is used. In https://github.com/ruby-grape/grape/blob/master/UPGRADING.md#upgrading-to--120:

In an effort to make APIs re-mountable, The class Grape::API no longer refers to an API instance, rather, what used to be Grape::API is now Grape::API::Instance and Grape::API was replaced with a class that can contain several instances of Grape::API.

This changes were done in such a way that no code-changes should be required. However, if experiencing problems, or relying on private methods and internal behaviour too deeply, it is possible to restore the prior behaviour by replacing the references from Grape::API to Grape::API::Instance.

Without changing to Grape::API::Instance, the pipeline failed in https://gitlab.com/gitlab-org/gitlab/-/jobs/472169354:

 NoMethodError: undefined method `allow_access_with_scope' for #<Class:0x0000562dad517030>
/builds/gitlab-org/gitlab/lib/api/files.rb:84:in `block in <class:Files>'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api/instance.rb:120:in `instance_eval'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api/instance.rb:120:in `block in evaluate_as_instance_with_configuration'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/util/lazy_block.rb:11:in `evaluate_from'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api/instance.rb:127:in `evaluate_as_instance_with_configuration'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api/instance.rb:107:in `block in nest'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api/instance.rb:107:in `each'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api/instance.rb:107:in `nest'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/dsl/routing.rb:172:in `block in namespace'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/dsl/settings.rb:160:in `within_namespace'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/dsl/routing.rb:169:in `namespace'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api.rb:155:in `replay_step_on'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api.rb:147:in `block in add_setup'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api.rb:146:in `each'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api.rb:146:in `add_setup'
/builds/gitlab-org/gitlab/vendor/ruby/2.6.0/gems/grape-1.3.1/lib/grape/api.rb:42:in `block (2 levels) in override_all_methods!'
/builds/gitlab-org/gitlab/lib/api/files.rb:83:in `<class:Files>'
/builds/gitlab-org/gitlab/lib/api/files.rb:4:in `<module:API>'
/builds/gitlab-org/gitlab/lib/api/files.rb:3:in `<top (required)>'

In lib/api/api_guard.rb we define class methods inside the API: https://gitlab.com/gitlab-org/gitlab/blob/120caec4bc742990b3eeefd64b968ae5b7704dc6/lib/api/api_guard.rb#L27-33.

We can revisit whether we can remove the use of Grape::API::Instance in favor of Grape::API, but for now let's introduce a Rubocop rule to ban the use of the latter.

Note we also upgraded grape-api-helpers to use Grape::API::Instance.

Edited by 🤖 GitLab Bot 🤖