From 3e5927a1b407f33db297d4b8e708e87f3dcdd735 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Mon, 16 Dec 2019 12:11:52 +0100 Subject: [PATCH] api-macro: generalized '{ schema: PATH }' schemas allow 'schema: PATH' outside object property context Signed-off-by: Wolfgang Bumiller --- proxmox-api-macro/src/api.rs | 12 ++++++++++++ proxmox-api-macro/tests/api2.rs | 20 ++++++++++++++++++++ proxmox-api-macro/tests/ext-schema.rs | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/proxmox-api-macro/src/api.rs b/proxmox-api-macro/src/api.rs index 75bd0c58..4c92d428 100644 --- a/proxmox-api-macro/src/api.rs +++ b/proxmox-api-macro/src/api.rs @@ -155,6 +155,7 @@ enum SchemaItem { Object(SchemaObject), Array(SchemaArray), ExternType(ExprPath), + ExternSchema(ExprPath), Inferred(Span), } @@ -162,6 +163,10 @@ impl SchemaItem { /// If there's a `type` specified, parse it as that type. Otherwise check for keys which /// uniqueply identify the type, such as "properties" for type `Object`. fn try_extract_from(obj: &mut JSONObject) -> Result { + if let Some(ext) = obj.remove("schema").map(ExprPath::try_from).transpose()? { + return Ok(SchemaItem::ExternSchema(ext)); + } + let ty = obj.remove("type").map(ExprPath::try_from).transpose()?; let ty = match ty { Some(ty) => ty, @@ -257,6 +262,13 @@ impl SchemaItem { ts.extend(quote_spanned! { path.span() => #path::API_SCHEMA }); return Ok(true); } + SchemaItem::ExternSchema(path) => { + if !properties.is_empty() { + bail!(&properties[0].0 => "additional properties not allowed on schema ref"); + } + ts.extend(quote_spanned! { path.span() => &#path }); + return Ok(true); + } SchemaItem::Inferred(span) => { bail!(*span, "failed to guess 'type' in schema definition"); } diff --git a/proxmox-api-macro/tests/api2.rs b/proxmox-api-macro/tests/api2.rs index a7db7038..beaa5a0e 100644 --- a/proxmox-api-macro/tests/api2.rs +++ b/proxmox-api-macro/tests/api2.rs @@ -31,3 +31,23 @@ pub fn hello(message: String) -> Result<(), Error> { pub async fn number(num: u32) -> Result { Ok(num) } + +#[api( + input: { + properties: { + foo: { + type: String, + description: "The great Foo", + }, + bar: { + type: String, + description: "The great Bar", + }, + }, + }, +)] +/// Return the number... +pub async fn more_async_params(param: Value) -> Result<(), Error> { + let _ = param; + Ok(()) +} diff --git a/proxmox-api-macro/tests/ext-schema.rs b/proxmox-api-macro/tests/ext-schema.rs index 372b08dc..32c07363 100644 --- a/proxmox-api-macro/tests/ext-schema.rs +++ b/proxmox-api-macro/tests/ext-schema.rs @@ -40,3 +40,22 @@ pub fn get_archive_2(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result Result<(), Error> { + let _ = param; + Ok(()) +}