From 48aff82709769b098321c738f3444b9bdaa694c6 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 21 Oct 2020 07:08:36 +0000 Subject: Add latest changes from gitlab-org/gitlab@13-5-stable-ee --- lib/api/api_guard.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'lib/api/api_guard.rb') 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 -- cgit v1.2.1