diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-17 00:10:05 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-17 00:10:05 +0300 |
commit | 831b6108d2aa46aca9bdce39a9bda33718d61fa7 (patch) | |
tree | 7578cc0f30fb75af82238cf7bf66c92a1c07e052 /lib/gitlab/json.rb | |
parent | 3a9076e0a4c28af9a1a40ed5e181b70fb1b659de (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/json.rb')
-rw-r--r-- | lib/gitlab/json.rb | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/lib/gitlab/json.rb b/lib/gitlab/json.rb index 6c254e171dc..21f837c58bb 100644 --- a/lib/gitlab/json.rb +++ b/lib/gitlab/json.rb @@ -67,6 +67,15 @@ module Gitlab ::JSON.pretty_generate(object, opts) end + # Feature detection for using Oj instead of the `json` gem. + # + # @return [Boolean] + def enable_oj? + return false unless feature_table_exists? + + Feature.enabled?(:oj_json, default_enabled: true) + end + private # Convert JSON string into Ruby through toggleable adapters. @@ -176,13 +185,6 @@ module Gitlab raise parser_error if INVALID_LEGACY_TYPES.any? { |type| data.is_a?(type) } end - # @return [Boolean] - def enable_oj? - return false unless feature_table_exists? - - Feature.enabled?(:oj_json, default_enabled: true) - end - # There are a variety of database errors possible when checking the feature # flags at the wrong time during boot, e.g. during migrations. We don't care # about these errors, we just need to ensure that we skip feature detection @@ -195,5 +197,28 @@ module Gitlab false end end + + # GrapeFormatter is a JSON formatter for the Grape API. + # This is set in lib/api/api.rb + + class GrapeFormatter + # Convert an object to JSON. + # + # This will default to the built-in Grape formatter if either :oj_json or :grape_gitlab_json + # flags are disabled. + # + # The `env` param is ignored because it's not needed in either our formatter or Grape's, + # but it is passed through for consistency. + # + # @param object [Object] + # @return [String] + def self.call(object, env = nil) + if Gitlab::Json.enable_oj? && Feature.enabled?(:grape_gitlab_json, default_enabled: true) + Gitlab::Json.dump(object) + else + Grape::Formatter::Json.call(object, env) + end + end + end end end |