Skip to content

Commit aca02c3

Browse files
committed
Updating ads common to support choice type in requests.
1 parent e98084a commit aca02c3

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

ads_common/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
0.9.6:
2+
- Support for 'choice' types in API requests.
23

34
0.9.5:
45
- Updated user-agent generation for default string.

ads_common/lib/ads_common/parameters_validator.rb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#
2020
# This class validates input parameters before passing them to Savon.
2121

22+
require 'ads_common/utils'
23+
2224
module AdsCommon
2325
class ParametersValidator
2426
# Savon special keys.
@@ -71,12 +73,49 @@ def validate_arguments(args_hash, fields_list, type_ns = nil)
7173
item_type = get_full_type_signature(field[:type])
7274
item_ns = field[:ns] || type_ns
7375
key = handle_namespace_override(args_hash, key, item_ns) if item_ns
74-
validate_arg(item, args_hash, key, item_type)
76+
77+
# Separate validation for choice types as we need to inject nodes into
78+
# the tree. Validate as usual if not a choice type.
79+
unless validate_choice_argument(item, args_hash, key, item_type)
80+
validate_arg(item, args_hash, key, item_type)
81+
end
7582
end
7683
end
7784
return args_hash
7885
end
7986

87+
# Special handling for choice types. Goes over each item, checks xsi_type
88+
# is set and correct and injects new node for it into the tree. After that,
89+
# recurces with the correct item type.
90+
def validate_choice_argument(item, parent, key, item_type)
91+
result = false
92+
if item_type.kind_of?(Hash) && item_type.include?(:choices)
93+
new_root = {}
94+
choice_items = arrayize(item)
95+
choice_items.each do |choice_item|
96+
choice_type = choice_item.delete(:xsi_type)
97+
choice_item_type =
98+
find_choice_by_xsi_type(choice_type, item_type[:choices])
99+
if choice_type.nil? || choice_item_type.nil?
100+
raise AdsCommon::Errors::TypeMismatchError.new(
101+
'choice subtype', choice_type, choice_item.to_s())
102+
end
103+
new_root[choice_type] ||= []
104+
new_root[choice_type] << choice_item
105+
type_signature = get_full_type_signature(choice_type)
106+
validate_arg(choice_item, new_root, choice_type, type_signature)
107+
end
108+
parent[key] = new_root
109+
result = true
110+
end
111+
return result
112+
end
113+
114+
def find_choice_by_xsi_type(xsi_type, item_def)
115+
index = item_def.index {|item| xsi_type.eql?(item[:original_name])}
116+
return index.nil? ? nil : item_def[index]
117+
end
118+
80119
# Checks if no extra fields provided outside of known ones.
81120
def check_extra_fields(args_hash, known_fields)
82121
extra_fields = args_hash.keys - known_fields - IGNORED_HASH_KEYS

0 commit comments

Comments
 (0)