Skip to content

Commit

Permalink
Add support for serializing shapes on the body with jsonvalue members. (
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods authored Jul 18, 2022
1 parent 8ee8aa0 commit ee96470
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 67 deletions.

This file was deleted.

113 changes: 106 additions & 7 deletions build_tools/aws-sdk-code-generator/spec/protocols/input/rest-json.json
Original file line number Diff line number Diff line change
Expand Up @@ -1229,18 +1229,60 @@
},
"shapes": {
"InputShape": {
"type":"structure",
"type": "structure",
"payload": "Body",
"members": {
"Attr": {
"HeaderField": {
"shape": "StringType",
"jsonvalue": true,
"location": "header",
"locationName": "X-Amz-Foo"
},
"QueryField": {
"shape": "StringType",
"jsonvalue": true,
"location": "querystring",
"locationName": "Bar"
},
"Body": {
"shape": "BodyStructure"
}
}
},
"StringType": {
"type": "string"
},
"ListType": {
"type": "list",
"member": {
"shape": "StringType",
"jsonvalue": true
}
},
"MapType": {
"type": "map",
"key": {
"shape": "StringType"
},
"value": {
"shape": "StringType",
"jsonvalue": true
}
},
"BodyStructure": {
"type": "structure",
"members": {
"BodyField": {
"shape": "StringType",
"jsonvalue": true
},
"BodyListField": {
"shape": "ListType"
},
"BodyMapField": {
"shape": "MapType"
}
}
}
},
"cases": [
Expand All @@ -1256,12 +1298,43 @@
"name": "OperationName"
},
"params": {
"Attr": {"Foo":"Bar"}
"HeaderField": {"Foo":"Bar"},
"QueryField": {"Foo":"Bar"},
"Body": {
"BodyField": {"Foo":"Bar"}
}
},
"serialized": {
"uri": "/?Bar=%7B%22Foo%22%3A%22Bar%22%7D",
"headers": {
"X-Amz-Foo": "eyJGb28iOiJCYXIifQ==",
"Content-Type": "application/json"
},
"body": "{\"BodyField\":\"{\\\"Foo\\\":\\\"Bar\\\"}\"}"
}
},
{
"given": {
"input": {
"shape": "InputShape"
},
"http": {
"method": "POST",
"requestUri": "/"
},
"name": "OperationName"
},
"params": {
"Body": {
"BodyListField": [{"Foo":"Bar"}]
}
},
"serialized": {
"uri": "/",
"headers": {"X-Amz-Foo": "eyJGb28iOiJCYXIifQ=="},
"body": ""
"headers": {
"Content-Type": "application/json"
},
"body": "{\"BodyListField\":[\"{\\\"Foo\\\":\\\"Bar\\\"}\"]}"
}
},
{
Expand All @@ -1276,11 +1349,37 @@
"name": "OperationName"
},
"params": {
"Body": {
"BodyMapField": {"FooBar": {"Foo":"Bar"}, "BarFoo": {"Bar": "Foo"}}
}
},
"serialized": {
"uri": "/",
"headers": {},
"body": ""
"headers": {
"Content-Type": "application/json"
},
"body": "{\"BodyMapField\": {\"FooBar\": \"{\\\"Foo\\\":\\\"Bar\\\"}\", \"BarFoo\": \"{\\\"Bar\\\":\\\"Foo\\\"}\"}}"
}
},
{
"given": {
"input": {
"shape": "InputShape"
},
"http": {
"method": "POST",
"requestUri": "/"
},
"name": "OperationName"
},
"params": {
},
"serialized": {
"uri": "/",
"headers": {
"Content-Type": "application/json"
},
"body": "{}"
}
}
]
Expand Down
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Add support for serializing shapes on the body with `jsonvalue` members.

3.131.2 (2022-06-20)
------------------

Expand Down
40 changes: 34 additions & 6 deletions gems/aws-sdk-core/lib/aws-sdk-core/plugins/jsonvalue_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,43 @@ class Handler < Seahorse::Client::Handler

def call(context)
context.operation.input.shape.members.each do |m, ref|
if ref['jsonvalue']
param_value = context.params[m]
unless param_value.respond_to?(:to_json)
raise ArgumentError, "The value of params[#{m}] is not JSON serializable."
convert_jsonvalue(m, ref, context.params, 'params')
end
@handler.call(context)
end

def convert_jsonvalue(m, ref, params, context)
return if params.nil? || !params.key?(m)

if ref['jsonvalue']
params[m] = serialize_jsonvalue(params[m], "#{context}[#{m}]")
else
case ref.shape
when Seahorse::Model::Shapes::StructureShape
ref.shape.members.each do |member_m, ref|
convert_jsonvalue(member_m, ref, params[m], "#{context}[#{m}]")
end
when Seahorse::Model::Shapes::ListShape
if ref.shape.member['jsonvalue']
params[m] = params[m].each_with_index.map do |v, i|
serialize_jsonvalue(v, "#{context}[#{m}][#{i}]")
end
end
when Seahorse::Model::Shapes::MapShape
if ref.shape.value['jsonvalue']
params[m].each do |k, v|
params[m][k] = serialize_jsonvalue(v, "#{context}[#{m}][#{k}]")
end
end
context.params[m] = param_value.to_json
end
end
@handler.call(context)
end

def serialize_jsonvalue(v, context)
unless v.respond_to?(:to_json)
raise ArgumentError, "The value of #{context} is not JSON serializable."
end
v.to_json
end

end
Expand Down
Loading

0 comments on commit ee96470

Please sign in to comment.