diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 07:08:36 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 07:08:36 +0000 |
commit | 48aff82709769b098321c738f3444b9bdaa694c6 (patch) | |
tree | e00c7c43e2d9b603a5a6af576b1685e400410dee /lib/api/api_guard.rb | |
parent | 879f5329ee916a948223f8f43d77fba4da6cd028 (diff) | |
download | gitlab-ce-48aff82709769b098321c738f3444b9bdaa694c6.tar.gz |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'lib/api/api_guard.rb')
-rw-r--r-- | lib/api/api_guard.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/api/api_guard.rb b/lib/api/api_guard.rb index bf5044b4832..0a486307653 100644 --- a/lib/api/api_guard.rb +++ b/lib/api/api_guard.rb @@ -19,6 +19,7 @@ module API end use AdminModeMiddleware + use ResponseCoercerMiddleware helpers HelperMethods @@ -188,6 +189,44 @@ module API end end + # Prior to Rack v2.1.x, returning a body of [nil] or [201] worked + # because the body was coerced to a string. However, this no longer + # works in Rack v2.1.0+. The Rack spec + # (https://github.com/rack/rack/blob/master/SPEC.rdoc#the-body-) + # says: + # + # The Body must respond to `each` and must only yield String values + # + # Because it's easy to return the wrong body type, this middleware + # will: + # + # 1. Inspect each element of the body if it is an Array. + # 2. Coerce each value to a string if necessary. + # 3. Flag a test and development error. + class ResponseCoercerMiddleware < ::Grape::Middleware::Base + def call(env) + response = super(env) + + status = response[0] + body = response[2] + + return response if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY[status] + return response unless body.is_a?(Array) + + body.map! do |part| + if part.is_a?(String) + part + else + err = ArgumentError.new("The response body should be a String, but it is of type #{part.class}") + Gitlab::ErrorTracking.track_and_raise_for_dev_exception(err) + part.to_s + end + end + + response + end + end + class AdminModeMiddleware < ::Grape::Middleware::Base def after # Use a Grape middleware since the Grape `after` blocks might run |