Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for serializing shapes on the body with jsonvalue members. #2727

Merged
merged 5 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
mullermp marked this conversation as resolved.
Show resolved Hide resolved
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
1 change: 0 additions & 1 deletion gems/aws-sdk-s3/spec/presigner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ module S3
expires_in: 86_400,
whitelist_headers: ['user-agent']
)
puts CGI.parse(actual_url)
expect(actual_url).to include(
'&X-Amz-SignedHeaders=host%3Buser-agent'
)
Expand Down