Allow empty request / response if all body parameters are optional
This commit is contained in:
parent
f17c5fc619
commit
cc07d26af6
@ -161,11 +161,20 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
|
|||||||
let request_body: <
|
let request_body: <
|
||||||
RequestBody #body_lifetimes
|
RequestBody #body_lifetimes
|
||||||
as #ruma_api_import::exports::ruma_serde::Outgoing
|
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!(
|
#ruma_api_import::try_deserialize!(
|
||||||
request,
|
request,
|
||||||
#ruma_api_import::exports::serde_json::from_slice(request.body().as_slice())
|
#ruma_api_import::exports::serde_json::from_slice(json)
|
||||||
);
|
)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TokenStream::new()
|
TokenStream::new()
|
||||||
@ -189,18 +198,26 @@ pub fn expand_all(api: Api) -> syn::Result<TokenStream> {
|
|||||||
TokenStream::new()
|
TokenStream::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
let typed_response_body_decl = if api.response.has_body_fields()
|
let typed_response_body_decl =
|
||||||
|| api.response.newtype_body_field().is_some()
|
if api.response.has_body_fields() || api.response.newtype_body_field().is_some() {
|
||||||
{
|
|
||||||
quote! {
|
quote! {
|
||||||
let response_body: <
|
let response_body: <
|
||||||
ResponseBody
|
ResponseBody
|
||||||
as #ruma_api_import::exports::ruma_serde::Outgoing
|
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!(
|
#ruma_api_import::try_deserialize!(
|
||||||
response,
|
response,
|
||||||
#ruma_api_import::exports::serde_json::from_slice(response.body().as_slice()),
|
#ruma_api_import::exports::serde_json::from_slice(json),
|
||||||
);
|
)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TokenStream::new()
|
TokenStream::new()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user