From f5b92f5507c2f99a334095615f947ee026d58d34 Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Thu, 3 Sep 2020 17:14:40 +0300
Subject: [PATCH] Introduce API content type feature flag

When enabled APi will use json as default content type.
Requires application restart to take effect.

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Signed-off-by: Dmytro Zaporozhets <dzaporozhets@gitlab.com>
---
 .../feature_flags/development/api_json_content_type.yml  | 7 +++++++
 lib/api/api.rb                                           | 9 ++++++++-
 lib/api/ci/runner.rb                                     | 2 ++
 lib/api/go_proxy.rb                                      | 2 ++
 lib/api/repositories.rb                                  | 2 ++
 5 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 config/feature_flags/development/api_json_content_type.yml

diff --git a/config/feature_flags/development/api_json_content_type.yml b/config/feature_flags/development/api_json_content_type.yml
new file mode 100644
index 0000000000000..bd153550635b5
--- /dev/null
+++ b/config/feature_flags/development/api_json_content_type.yml
@@ -0,0 +1,7 @@
+---
+name: api_json_content_type
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/42229
+rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/270067
+group: group::ecosystem
+type: development
+default_enabled: false
diff --git a/lib/api/api.rb b/lib/api/api.rb
index 417d4d66aca26..4300477bdb2d5 100644
--- a/lib/api/api.rb
+++ b/lib/api/api.rb
@@ -115,7 +115,14 @@ class API < Grape::API::Instance
 
     format :json
     formatter :json, Gitlab::Json::GrapeFormatter
-    content_type :txt, "text/plain"
+
+    # There is a small chance some users depend on the old behavior.
+    # We this change under a feature flag to see if affects GitLab.com users.
+    if Feature.enabled?(:api_json_content_type)
+      content_type :json, 'application/json'
+    else
+      content_type :txt, 'text/plain'
+    end
 
     # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
     helpers ::API::Helpers
diff --git a/lib/api/ci/runner.rb b/lib/api/ci/runner.rb
index f86e18676c14c..9d2e251caf826 100644
--- a/lib/api/ci/runner.rb
+++ b/lib/api/ci/runner.rb
@@ -5,6 +5,8 @@ module Ci
     class Runner < Grape::API::Instance
       helpers ::API::Helpers::Runner
 
+      content_type :txt, 'text/plain'
+
       resource :runners do
         desc 'Registers a new Runner' do
           success Entities::RunnerRegistrationDetails
diff --git a/lib/api/go_proxy.rb b/lib/api/go_proxy.rb
index c0207f9169c83..897b31cad488f 100755
--- a/lib/api/go_proxy.rb
+++ b/lib/api/go_proxy.rb
@@ -9,6 +9,8 @@ class GoProxy < Grape::API::Instance
 
     MODULE_VERSION_REQUIREMENTS = { module_version: MODULE_VERSION_REGEX }.freeze
 
+    content_type :txt, 'text/plain'
+
     before { require_packages_enabled! }
 
     helpers do
diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb
index 81702f8f02a19..abdf0fef8c0ec 100644
--- a/lib/api/repositories.rb
+++ b/lib/api/repositories.rb
@@ -6,6 +6,8 @@ module API
   class Repositories < Grape::API::Instance
     include PaginationParams
 
+    content_type :txt, 'text/plain'
+
     helpers ::API::Helpers::HeadersHelpers
 
     before { authorize! :download_code, user_project }
-- 
GitLab