Allow empty request / response if all body parameters are optional

This commit is contained in:
Jonas Platte 2020-12-06 22:33:14 +01:00
parent f17c5fc619
commit cc07d26af6
No known key found for this signature in database
GPG Key ID: 7D261D771D915378

View File

@ -161,11 +161,20 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
let request_body: <
RequestBody #body_lifetimes
as #ruma_api_import::exports::ruma_serde::Outgoing
>::Incoming =
>::Incoming = {
// If the request body is completely empty, pretend it is an empty JSON object
// instead. This allows requests with only optional body parameters to be
// deserialized in that case.
let json = match request.body().as_slice() {
b"" => b"{}",
body => body,
};
#ruma_api_import::try_deserialize!(
request,
#ruma_api_import::exports::serde_json::from_slice(request.body().as_slice())
);
#ruma_api_import::exports::serde_json::from_slice(json)
)
};
}
} else {
TokenStream::new()
@ -189,18 +198,26 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
TokenStream::new()
};
let typed_response_body_decl = if api.response.has_body_fields()
|| api.response.newtype_body_field().is_some()
{
let typed_response_body_decl =
if api.response.has_body_fields() || api.response.newtype_body_field().is_some() {
quote! {
let response_body: <
ResponseBody
as #ruma_api_import::exports::ruma_serde::Outgoing
>::Incoming =
>::Incoming = {
// If the reponse body is completely empty, pretend it is an empty JSON object
// instead. This allows reponses with only optional body parameters to be
// deserialized in that case.
let json = match response.body().as_slice() {
b"" => b"{}",
body => body,
};
#ruma_api_import::try_deserialize!(
response,
#ruma_api_import::exports::serde_json::from_slice(response.body().as_slice()),
);
#ruma_api_import::exports::serde_json::from_slice(json),
)
};
}
} else {
TokenStream::new()