Skip to content

Commit

Permalink
Ensure keys in query maps do not overwrite modeled query keys
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods committed Mar 4, 2024
1 parent 805c6f7 commit 2001eaf
Showing 1 changed file with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,30 @@ class QuerystringBuilder
#
# @return [String] Returns a built querystring
def build(params)
# keys in query maps must NOT override other keys
query_keys = query_keys(params)
params.map do |(shape_ref, param_value)|
build_part(shape_ref, param_value)
end.join('&')
build_part(shape_ref, param_value, query_keys)
end.reject { |p| p.nil? || p.empty? }.join('&')
end

private

def build_part(shape_ref, param_value)
def query_keys(params)
keys = Set.new
params.each do |(shape_ref, _)|
keys << shape_ref.location_name unless shape_ref.shape.is_a?(MapShape)
end
keys
end

def build_part(shape_ref, param_value, query_keys)
case shape_ref.shape
# supported scalar types
when *SUPPORTED_TYPES
"#{shape_ref.location_name}=#{query_value(shape_ref, param_value)}"
when MapShape
generate_query_map(shape_ref, param_value)
generate_query_map(shape_ref, param_value, query_keys)
when ListShape
generate_query_list(shape_ref, param_value)
else
Expand Down Expand Up @@ -80,31 +90,33 @@ def generate_query_list(ref, values)
end
end

def generate_query_map(ref, value)
def generate_query_map(ref, value, query_keys)
case ref.shape.value.shape
when StringShape
query_map_of_string(value)
query_map_of_string(value, query_keys)
when ListShape
query_map_of_string_list(value)
query_map_of_string_list(value, query_keys)
else
msg = 'Only map of string and string list supported'
raise NotImplementedError, msg
end
end

def query_map_of_string(hash)
def query_map_of_string(hash, query_keys)
list = []
hash.each_pair do |key, value|
list << "#{escape(key)}=#{escape(value)}"
key = escape(key)
list << "#{key}=#{escape(value)}" unless query_keys.include?(key)
end
list
end

def query_map_of_string_list(hash)
def query_map_of_string_list(hash, query_keys)
list = []
hash.each_pair do |key, values|
key = escape(key)
values.each do |value|
list << "#{escape(key)}=#{escape(value)}"
list << "#{key}=#{escape(value)}" unless query_keys.include?(key)
end
end
list
Expand Down

0 comments on commit 2001eaf

Please sign in to comment.