lib/grape/dsl/inside_route.rb in grape-1.4.0 vs lib/grape/dsl/inside_route.rb in grape-1.5.0
- old
+ new
@@ -56,11 +56,11 @@
next unless options[:include_missing] || passed_params.key?(declared_parent_param)
passed_children_params = passed_params[declared_parent_param] || passed_params.class.new
memo_key = optioned_param_key(declared_parent_param, options)
- memo[memo_key] = handle_passed_param(passed_children_params, params_nested_path_dup) do
+ memo[memo_key] = handle_passed_param(params_nested_path_dup, passed_children_params.any?) do
declared(passed_children_params, options, declared_children_params, params_nested_path_dup)
end
end
else
# If it is not a Hash then it does not have children.
@@ -68,61 +68,47 @@
has_renaming = route_setting(:renamed_params) && route_setting(:renamed_params).find { |current| current[declared_param] }
param_renaming = has_renaming[declared_param] if has_renaming
next unless options[:include_missing] || passed_params.key?(declared_param) || (param_renaming && passed_params.key?(param_renaming))
- if param_renaming
- memo[optioned_param_key(param_renaming, options)] = passed_params[param_renaming]
- else
- memo[optioned_param_key(declared_param, options)] = passed_params[declared_param]
+ memo_key = optioned_param_key(param_renaming || declared_param, options)
+ passed_param = passed_params[param_renaming || declared_param]
+
+ params_nested_path_dup = params_nested_path.dup
+ params_nested_path_dup << declared_param.to_s
+ memo[memo_key] = passed_param || handle_passed_param(params_nested_path_dup) do
+ passed_param
end
end
end
end
- def handle_passed_param(passed_children_params, params_nested_path, &_block)
- if should_be_empty_hash?(passed_children_params, params_nested_path)
+ def handle_passed_param(params_nested_path, has_passed_children = false, &_block)
+ return yield if has_passed_children
+
+ key = params_nested_path[0]
+ key += '[' + params_nested_path[1..-1].join('][') + ']' if params_nested_path.size > 1
+
+ route_options_params = options[:route_options][:params] || {}
+ type = route_options_params.dig(key, :type)
+ has_children = route_options_params.keys.any? { |k| k != key && k.start_with?(key) }
+
+ if type == 'Hash' && !has_children
{}
- elsif should_be_empty_array?(passed_children_params, params_nested_path)
+ elsif type == 'Array' || type&.start_with?('[')
[]
+ elsif type == 'Set' || type&.start_with?('#<Set')
+ Set.new
else
yield
end
end
- def should_be_empty_array?(passed_children_params, params_nested_path)
- passed_children_params.empty? && declared_param_is_array?(params_nested_path)
- end
-
- def declared_param_is_array?(params_nested_path)
- key = route_options_params_key(params_nested_path)
- route_options_params[key] && route_options_params[key][:type] == 'Array'
- end
-
- def should_be_empty_hash?(passed_children_params, params_nested_path)
- passed_children_params.empty? && declared_param_is_hash?(params_nested_path)
- end
-
- def declared_param_is_hash?(params_nested_path)
- key = route_options_params_key(params_nested_path)
- route_options_params[key] && route_options_params[key][:type] == 'Hash'
- end
-
- def route_options_params
- options[:route_options][:params] || {}
- end
-
def optioned_param_key(declared_param, options)
options[:stringify] ? declared_param.to_s : declared_param.to_sym
end
- def route_options_params_key(params_nested_path)
- key = params_nested_path[0]
- key += '[' + params_nested_path[1..-1].join('][') + ']' if params_nested_path.size > 1
- key
- end
-
def optioned_declared_params(**options)
declared_params = if options[:include_parent_namespaces]
# Declared params including parent namespaces
route_setting(:declared_params)
else
@@ -326,10 +312,12 @@
#
# See:
# * https://github.com/rack/rack/blob/99293fa13d86cd48021630fcc4bd5acc9de5bdc3/lib/rack/chunked.rb
# * https://github.com/rack/rack/blob/99293fa13d86cd48021630fcc4bd5acc9de5bdc3/lib/rack/etag.rb
def stream(value = nil)
+ return if value.nil? && @stream.nil?
+
header 'Content-Length', nil
header 'Transfer-Encoding', nil
header 'Cache-Control', 'no-cache' # Skips ETag generation (reading the response up front)
if value.is_a?(String)
file_body = Grape::ServeStream::FileBody.new(value)
@@ -337,11 +325,11 @@
elsif value.respond_to?(:each)
@stream = Grape::ServeStream::StreamResponse.new(value)
elsif !value.is_a?(NilClass)
raise ArgumentError, 'Stream object must respond to :each.'
else
- instance_variable_defined?(:@stream) ? @stream : nil
+ @stream
end
end
# Allows you to make use of Grape Entities by setting
# the response body to the serializable hash of the
@@ -431,10 +419,10 @@
# @return the representation of the given object as done through
# the given entity_class.
def entity_representation_for(entity_class, object, options)
embeds = { env: env }
embeds[:version] = env[Grape::Env::API_VERSION] if env[Grape::Env::API_VERSION]
- entity_class.represent(object, embeds.merge(options))
+ entity_class.represent(object, **embeds.merge(options))
end
end
end
end