Skip to content

Commit

Permalink
Addressed an issue serializing structure request params.
Browse files Browse the repository at this point in the history
Resolved an issue where structure parameter values, such as those
returned in responses, could not be serialized as input parameters
when parameter conversion is disabled.

This fix resolves the issue by changing the serializes to all enumerate
structure/hash values in a consistent manor, using #each_pair. Also
added a few minor helpers to `Aws::Structure` making it quack more
like a Hash object.

Fixes #815
  • Loading branch information
trevorrowe committed May 15, 2015
1 parent 07aa3ca commit 8db1ec5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion aws-sdk-core/lib/aws-sdk-core/json/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def to_json(params)

def structure(ref, values)
shape = ref.shape
values.each.with_object({}) do |(key, value), data|
values.each_pair.with_object({}) do |(key, value), data|
if shape.member?(key) && !value.nil?
member_ref = shape.member(key)
member_name = member_ref.location_name || key
Expand Down
2 changes: 1 addition & 1 deletion aws-sdk-core/lib/aws-sdk-core/query/param_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def apply(ref, params)

def structure(ref, values, prefix)
shape = ref.shape
values.each do |name, value|
values.each_pair do |name, value|
next if value.nil?
member_ref = shape.member(name)
format(member_ref, value, prefix + query_name(member_ref))
Expand Down
11 changes: 11 additions & 0 deletions aws-sdk-core/lib/aws-sdk-core/structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ class Structure < Struct
alias orig_to_h to_h
end

# @return [Boolean] Returns `true` if this structure has a value
# set for the given member.
def key?(member_name)
!self[member_name].nil?
end

# @return [Boolean] Returns `true` if all of the member values are `nil`.
def empty?
values.compact == []
end

# Deeply converts the Structure into a hash. Structure members that
# are `nil` are omitted from the resultant hash.
#
Expand Down
14 changes: 12 additions & 2 deletions aws-sdk-core/spec/aws/json/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ module Json

let(:shapes) { ApiHelper.sample_shapes }

let(:rules) {
Api::ShapeMap.new(shapes).shape_ref('shape' => 'StructureShape')
}

def json(params)
shape_map = Api::ShapeMap.new(shapes)
rules = shape_map.shape_ref('shape' => 'StructureShape')
Builder.new(rules).to_json(params)
end

it 'builds an empty JOSN document when there are no params' do
expect(json({})).to eq('{}')
end

it 'can serialize structures' do
params = Structure.new(*rules.shape.member_names).new
params.boolean = true
params.integer = 123
params.string = 'abc'
expect(json(params)).to eq('{"Boolean":true,"Integer":123,"String":"abc"}')
end

it 'supports locationName traits on structure members' do
shapes['StructureShape']['members']['String']['locationName'] = 'str'
expect(json(string: 'abc')).to eq('{"str":"abc"}')
Expand Down
19 changes: 17 additions & 2 deletions aws-sdk-core/spec/aws/query/param_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ module Query

let(:shapes) { ApiHelper.sample_shapes }

def query(params = {})
let(:rules) {
shape_map = Api::ShapeMap.new(shapes)
rules = shape_map.shape_ref('shape' => 'StructureShape')
shape_map.shape_ref('shape' => 'StructureShape')
}

def query(params = {})
param_list = ParamList.new
ParamBuilder.new(param_list).apply(rules, params)
param_list.map { |param| [param.name, param.value ] }.sort
Expand All @@ -18,6 +21,18 @@ def query(params = {})
expect(query({})).to eq([])
end

it 'can serialize structures' do
params = Structure.new(*rules.shape.member_names).new
params.boolean = true
params.integer = 123
params.string = 'abc'
expect(query(params)).to eq([
['Boolean', 'true'],
['Integer', '123'],
['String', 'abc'],
])
end

it 'does not serialize nil values' do
params = {
string: 'abc',
Expand Down
18 changes: 16 additions & 2 deletions aws-sdk-core/spec/aws/xml/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,30 @@ module Xml

let(:ref) {{ 'shape' => 'StructureShape', 'locationName' => 'xml' }}

let(:rules) { Api::ShapeMap.new(shapes).shape_ref(ref) }

def xml(params)
shape_map = Api::ShapeMap.new(shapes)
rules = shape_map.shape_ref(ref)
Builder.new(rules).to_xml(params)
end

it 'serializes empty values as empty elements' do
expect(xml({})).to eq("<xml/>\n")
end

it 'can serialize structures' do
params = Structure.new(*rules.shape.member_names).new
params.boolean = true
params.integer = 123
params.string = 'abc'
expect(xml(params)).to eq(<<-XML)
<xml>
<Boolean>true</Boolean>
<Integer>123</Integer>
<String>abc</String>
</xml>
XML
end

it 'orders xml elements by members order' do
params = {
string: 'a',
Expand Down

0 comments on commit 8db1ec5

Please sign in to comment.