api-macro: don't return Null without return schema
serde_json turns () into Null anyway, so there's no need to check this! Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
f9d775924d
commit
c282c8ce23
@ -352,7 +352,7 @@ fn is_value_type(ty: &syn::Type) -> bool {
|
||||
|
||||
fn create_wrapper_function(
|
||||
_input_schema: &Schema,
|
||||
returns_schema: &Option<Schema>,
|
||||
_returns_schema: &Option<Schema>,
|
||||
param_list: Vec<(FieldName, ParameterType)>,
|
||||
func: &syn::ItemFn,
|
||||
wrapper_ts: &mut TokenStream,
|
||||
@ -364,7 +364,6 @@ fn create_wrapper_function(
|
||||
|
||||
let mut body = TokenStream::new();
|
||||
let mut args = TokenStream::new();
|
||||
let mut return_stmt = TokenStream::new();
|
||||
|
||||
for (name, param) in param_list {
|
||||
let span = name.span();
|
||||
@ -404,17 +403,6 @@ fn create_wrapper_function(
|
||||
}
|
||||
}
|
||||
|
||||
if returns_schema.is_some() {
|
||||
return_stmt.extend(quote! {
|
||||
Ok(::serde_json::to_value(output)?)
|
||||
});
|
||||
} else {
|
||||
return_stmt.extend(quote! {
|
||||
let _ = output;
|
||||
Ok(::serde_json::Value::Null)
|
||||
});
|
||||
}
|
||||
|
||||
// build the wrapping function:
|
||||
let func_name = &func.sig.ident;
|
||||
wrapper_ts.extend(quote! {
|
||||
@ -425,8 +413,7 @@ fn create_wrapper_function(
|
||||
) -> Result<::serde_json::Value, ::failure::Error> {
|
||||
if let ::serde_json::Value::Object(ref mut input_map) = &mut input_params {
|
||||
#body
|
||||
let output = #func_name(#args)?;
|
||||
#return_stmt
|
||||
Ok(::serde_json::to_value(#func_name(#args)?)?)
|
||||
} else {
|
||||
::failure::bail!("api function wrapper called with a non-object json value");
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
//! This should test the usage of "external" schemas. If a property is declared with a path instead
|
||||
//! of an object, we expect the path to lead to a schema.
|
||||
|
||||
use proxmox::api::schema;
|
||||
use proxmox::api::{schema, RpcEnvironment};
|
||||
use proxmox_api_macro::api;
|
||||
|
||||
use failure::Error;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
pub const NAME_SCHEMA: schema::Schema = schema::StringSchema::new("Archive name.")
|
||||
//.format(&FILENAME_FORMAT)
|
||||
@ -24,3 +25,19 @@ pub fn get_archive(archive_name: String) -> Result<(), Error> {
|
||||
let _ = archive_name;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[api(
|
||||
input: {
|
||||
properties: {
|
||||
"archive-name": {
|
||||
schema: NAME_SCHEMA,
|
||||
}
|
||||
}
|
||||
}
|
||||
)]
|
||||
/// Get an archive.
|
||||
pub fn get_archive_2(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
|
||||
let _ = param;
|
||||
let _ = rpcenv;
|
||||
Ok(json!("test"))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user