2020-04-21 11:21:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
# This is a GitLab-specific JSON interface. You should use this instead
|
|
|
|
# of using `JSON` directly. This allows us to swap the adapter and handle
|
|
|
|
# legacy issues.
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
module Gitlab
|
|
|
|
module Json
|
2020-05-06 17:10:00 -04:00
|
|
|
INVALID_LEGACY_TYPES = [String, TrueClass, FalseClass].freeze
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
class << self
|
2020-07-13 11:09:08 -04:00
|
|
|
# Parse a string and convert it to a Ruby object
|
|
|
|
#
|
|
|
|
# @param string [String] the JSON string to convert to Ruby objects
|
|
|
|
# @param opts [Hash] an options hash in the standard JSON gem format
|
|
|
|
# @return [Boolean, String, Array, Hash]
|
|
|
|
# @raise [JSON::ParserError] raised if parsing fails
|
|
|
|
def parse(string, opts = {})
|
2022-03-02 19:20:18 -05:00
|
|
|
# Parse nil as nil
|
|
|
|
return if string.nil?
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
# First we should ensure this really is a string, not some other
|
|
|
|
# type which purports to be a string. This handles some legacy
|
|
|
|
# usage of the JSON class.
|
|
|
|
string = string.to_s unless string.is_a?(String)
|
|
|
|
|
|
|
|
legacy_mode = legacy_mode_enabled?(opts.delete(:legacy_mode))
|
2020-08-21 08:10:22 -04:00
|
|
|
data = adapter_load(string, **opts)
|
2020-05-06 17:10:00 -04:00
|
|
|
|
|
|
|
handle_legacy_mode!(data) if legacy_mode
|
|
|
|
|
|
|
|
data
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
alias_method :parse!, :parse
|
2022-03-02 19:20:18 -05:00
|
|
|
alias_method :load, :parse
|
2020-07-13 11:09:08 -04:00
|
|
|
|
|
|
|
# Restricted method for converting a Ruby object to JSON. If you
|
|
|
|
# need to pass options to this, you should use `.generate` instead,
|
|
|
|
# as the underlying implementation of this varies wildly based on
|
|
|
|
# the adapter in use.
|
|
|
|
#
|
|
|
|
# @param object [Object] the object to convert to JSON
|
|
|
|
# @return [String]
|
|
|
|
def dump(object)
|
|
|
|
adapter_dump(object)
|
|
|
|
end
|
2020-07-09 20:09:13 -04:00
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
# Generates JSON for an object. In Oj this takes fewer options than .dump,
|
|
|
|
# in the JSON gem this is the only method which takes an options argument.
|
|
|
|
#
|
|
|
|
# @param object [Hash, Array, Object] must be hash, array, or an object that responds to .to_h or .to_json
|
|
|
|
# @param opts [Hash] an options hash with fewer supported settings than .dump
|
|
|
|
# @return [String]
|
|
|
|
def generate(object, opts = {})
|
|
|
|
adapter_generate(object, opts)
|
|
|
|
end
|
2020-07-09 20:09:13 -04:00
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
# Generates JSON for an object and makes it look purdy
|
|
|
|
#
|
|
|
|
# The Oj variant in this looks seriously weird but these are the settings
|
|
|
|
# needed to emulate the style generated by the JSON gem.
|
|
|
|
#
|
|
|
|
# NOTE: This currently ignores Oj, because Oj doesn't generate identical
|
|
|
|
# formatting, issue: https://github.com/ohler55/oj/issues/608
|
|
|
|
#
|
|
|
|
# @param object [Hash, Array, Object] must be hash, array, or an object that responds to .to_h or .to_json
|
|
|
|
# @param opts [Hash] an options hash with fewer supported settings than .dump
|
|
|
|
# @return [String]
|
|
|
|
def pretty_generate(object, opts = {})
|
|
|
|
::JSON.pretty_generate(object, opts)
|
2020-07-09 20:09:13 -04:00
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2022-03-02 19:20:18 -05:00
|
|
|
# The standard parser error we should be returning. Defined in a method
|
|
|
|
# so we can potentially override it later.
|
|
|
|
#
|
|
|
|
# @return [JSON::ParserError]
|
|
|
|
def parser_error
|
|
|
|
::JSON::ParserError
|
|
|
|
end
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# Convert JSON string into Ruby through toggleable adapters.
|
|
|
|
#
|
|
|
|
# Must rescue adapter-specific errors and return `parser_error`, and
|
|
|
|
# must also standardize the options hash to support each adapter as
|
|
|
|
# they all take different options.
|
|
|
|
#
|
|
|
|
# @param string [String] the JSON string to convert to Ruby objects
|
|
|
|
# @param opts [Hash] an options hash in the standard JSON gem format
|
|
|
|
# @return [Boolean, String, Array, Hash]
|
|
|
|
# @raise [JSON::ParserError]
|
|
|
|
def adapter_load(string, *args, **opts)
|
|
|
|
opts = standardize_opts(opts)
|
|
|
|
|
2020-10-27 14:08:59 -04:00
|
|
|
Oj.load(string, opts)
|
2020-07-13 11:09:08 -04:00
|
|
|
rescue Oj::ParseError, Encoding::UndefinedConversionError => ex
|
2021-05-04 11:10:36 -04:00
|
|
|
raise parser_error, ex
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
# Take a Ruby object and convert it to a string. This method varies
|
|
|
|
# based on the underlying JSON interpreter. Oj treats this like JSON
|
|
|
|
# treats `.generate`. JSON.dump takes no options.
|
|
|
|
#
|
|
|
|
# This supports these options to ensure this difference is recorded here,
|
|
|
|
# as it's very surprising. The public interface is more restrictive to
|
|
|
|
# prevent adapter-specific options being passed.
|
|
|
|
#
|
|
|
|
# @overload adapter_dump(object, opts)
|
|
|
|
# @param object [Object] the object to convert to JSON
|
|
|
|
# @param opts [Hash] options as named arguments, only supported by Oj
|
|
|
|
#
|
|
|
|
# @overload adapter_dump(object, anIO, limit)
|
|
|
|
# @param object [Object] the object, will have JSON.generate called on it
|
|
|
|
# @param anIO [Object] an IO-like object that responds to .write, default nil
|
|
|
|
# @param limit [Fixnum] the nested array/object limit, default nil
|
|
|
|
# @raise [ArgumentError] when depth limit exceeded
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def adapter_dump(object, *args, **opts)
|
2020-10-27 14:08:59 -04:00
|
|
|
Oj.dump(object, opts)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
# Generates JSON for an object but with fewer options, using toggleable adapters.
|
|
|
|
#
|
|
|
|
# @param object [Hash, Array, Object] must be hash, array, or an object that responds to .to_h or .to_json
|
|
|
|
# @param opts [Hash] an options hash with fewer supported settings than .dump
|
|
|
|
# @return [String]
|
|
|
|
def adapter_generate(object, opts = {})
|
|
|
|
opts = standardize_opts(opts)
|
|
|
|
|
2020-10-27 14:08:59 -04:00
|
|
|
Oj.generate(object, opts)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
# Take a JSON standard options hash and standardize it to work across adapters
|
|
|
|
# An example of this is Oj taking :symbol_keys instead of :symbolize_names
|
|
|
|
#
|
|
|
|
# @param opts [Hash, Nil]
|
|
|
|
# @return [Hash]
|
|
|
|
def standardize_opts(opts)
|
|
|
|
opts ||= {}
|
2020-10-27 14:08:59 -04:00
|
|
|
opts[:mode] = :rails
|
|
|
|
opts[:symbol_keys] = opts[:symbolize_keys] || opts[:symbolize_names]
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
opts
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2020-05-06 17:10:00 -04:00
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
# @param [Nil, Boolean] an extracted :legacy_mode key from the opts hash
|
|
|
|
# @return [Boolean]
|
2020-05-06 17:10:00 -04:00
|
|
|
def legacy_mode_enabled?(arg_value)
|
|
|
|
arg_value.nil? ? false : arg_value
|
|
|
|
end
|
|
|
|
|
2020-07-13 11:09:08 -04:00
|
|
|
# If legacy mode is enabled, we need to raise an error depending on the values
|
|
|
|
# provided in the string. This will be deprecated.
|
|
|
|
#
|
|
|
|
# @param data [Boolean, String, Array, Hash, Object]
|
|
|
|
# @return [Boolean, String, Array, Hash, Object]
|
|
|
|
# @raise [JSON::ParserError]
|
2020-05-06 17:10:00 -04:00
|
|
|
def handle_legacy_mode!(data)
|
2020-07-13 11:09:08 -04:00
|
|
|
return data unless feature_table_exists?
|
2022-05-06 11:09:03 -04:00
|
|
|
return data unless Feature.enabled?(:json_wrapper_legacy_mode)
|
2020-05-06 17:10:00 -04:00
|
|
|
|
|
|
|
raise parser_error if INVALID_LEGACY_TYPES.any? { |type| data.is_a?(type) }
|
|
|
|
end
|
2020-07-13 11:09:08 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
# if they will fail.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def feature_table_exists?
|
|
|
|
Feature::FlipperFeature.table_exists?
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2020-07-13 11:09:08 -04:00
|
|
|
false
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2020-07-16 17:10:05 -04:00
|
|
|
|
|
|
|
# GrapeFormatter is a JSON formatter for the Grape API.
|
|
|
|
# This is set in lib/api/api.rb
|
|
|
|
|
|
|
|
class GrapeFormatter
|
|
|
|
# Convert an object to JSON.
|
|
|
|
#
|
|
|
|
# The `env` param is ignored because it's not needed in either our formatter or Grape's,
|
|
|
|
# but it is passed through for consistency.
|
|
|
|
#
|
2021-03-17 11:09:03 -04:00
|
|
|
# If explicitly supplied with a `PrecompiledJson` instance it will skip conversion
|
|
|
|
# and return it directly. This is mostly used in caching.
|
|
|
|
#
|
2020-07-16 17:10:05 -04:00
|
|
|
# @param object [Object]
|
|
|
|
# @return [String]
|
|
|
|
def self.call(object, env = nil)
|
2021-03-17 11:09:03 -04:00
|
|
|
return object.to_s if object.is_a?(PrecompiledJson)
|
|
|
|
|
2022-02-16 16:12:25 -05:00
|
|
|
Gitlab::Json.dump(object)
|
2020-07-16 17:10:05 -04:00
|
|
|
end
|
|
|
|
end
|
2020-08-12 11:10:02 -04:00
|
|
|
|
2021-03-17 11:09:03 -04:00
|
|
|
# Wrapper class used to skip JSON dumping on Grape endpoints.
|
|
|
|
|
|
|
|
class PrecompiledJson
|
|
|
|
UnsupportedFormatError = Class.new(StandardError)
|
|
|
|
|
|
|
|
# @overload PrecompiledJson.new("foo")
|
|
|
|
# @param value [String]
|
|
|
|
#
|
|
|
|
# @overload PrecompiledJson.new(["foo", "bar"])
|
|
|
|
# @param value [Array<String>]
|
|
|
|
def initialize(value)
|
|
|
|
@value = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert the value to a String. This will invoke
|
|
|
|
# `#to_s` on the members of the value if it's an array.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
# @raise [NoMethodError] if the objects in an array doesn't support to_s
|
|
|
|
# @raise [PrecompiledJson::UnsupportedFormatError] if the value is neither a String or Array
|
|
|
|
def to_s
|
|
|
|
return @value if @value.is_a?(String)
|
|
|
|
return "[#{@value.join(',')}]" if @value.is_a?(Array)
|
|
|
|
|
|
|
|
raise UnsupportedFormatError
|
|
|
|
end
|
2021-06-22 05:07:12 -04:00
|
|
|
|
|
|
|
def render_in(_view_context)
|
|
|
|
to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def format
|
|
|
|
:json
|
|
|
|
end
|
2021-03-17 11:09:03 -04:00
|
|
|
end
|
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
class LimitedEncoder
|
|
|
|
LimitExceeded = Class.new(StandardError)
|
|
|
|
|
|
|
|
# Generates JSON for an object or raise an error if the resulting json string is too big
|
|
|
|
#
|
|
|
|
# @param object [Hash, Array, Object] must be hash, array, or an object that responds to .to_h or .to_json
|
|
|
|
# @param limit [Integer] max size of the resulting json string
|
|
|
|
# @return [String]
|
|
|
|
# @raise [LimitExceeded] if the resulting json string is bigger than the specified limit
|
|
|
|
def self.encode(object, limit: 25.megabytes)
|
2021-06-07 14:10:23 -04:00
|
|
|
buffer = StringIO.new
|
2020-08-12 11:10:02 -04:00
|
|
|
buffer_size = 0
|
|
|
|
|
|
|
|
::Yajl::Encoder.encode(object) do |data_chunk|
|
|
|
|
chunk_size = data_chunk.bytesize
|
|
|
|
|
|
|
|
raise LimitExceeded if buffer_size + chunk_size > limit
|
|
|
|
|
|
|
|
buffer << data_chunk
|
|
|
|
buffer_size += chunk_size
|
|
|
|
end
|
|
|
|
|
2021-06-07 14:10:23 -04:00
|
|
|
buffer.string
|
2020-08-12 11:10:02 -04:00
|
|
|
end
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|