public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox] client: fix optional data for errors
Date: Thu, 14 Sep 2023 12:41:30 +0200	[thread overview]
Message-ID: <20230914104130.59082-1-d.csapak@proxmox.com> (raw)

previously we changed the internal type of the 'data' property
from Option<T> to T in the assumption the api always returns
'data:null'.

this is actually only the case when the api call succeeds. in an error
case there is no data property at all.

to fix this issue while behaving the same for 'data:null' we have to
revert to Option<T> for RawApiResponse but instead of always throwing an
error for 'data:null' in 'check' we now try there to deserialize from
Value::Null for T if there was no data. This will succeed for the Type
'()' which was the motivation for the original change.

The only downside is that the RawApiResponse now has a trait bound that
T is deserializeable, but was a requirement for using it anyway
(as there was no other way of constructing it)

Fixes: 271a55f ("client: remove option from inner RawApiResponse")
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox-client/src/lib.rs | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/proxmox-client/src/lib.rs b/proxmox-client/src/lib.rs
index 9aa7144..8161cc0 100644
--- a/proxmox-client/src/lib.rs
+++ b/proxmox-client/src/lib.rs
@@ -106,7 +106,7 @@ impl HttpApiResponse {
 
     /// Expect that the API call did *not* return any data in the `data` field.
     pub fn nodata(self) -> Result<(), Error> {
-        let response = serde_json::from_slice::<RawApiResponse<Option<()>>>(&self.body)
+        let response = serde_json::from_slice::<RawApiResponse<()>>(&self.body)
             .map_err(|err| Error::bad_api("unexpected api response", err))?;
 
         if response.data.is_some() {
@@ -131,7 +131,7 @@ struct RawApiResponse<T> {
     message: Option<String>,
     #[serde(default, deserialize_with = "proxmox_login::parse::deserialize_bool")]
     success: Option<bool>,
-    data: T,
+    data: Option<T>,
 
     #[serde(default)]
     errors: HashMap<String, String>,
@@ -140,7 +140,10 @@ struct RawApiResponse<T> {
     attribs: HashMap<String, Value>,
 }
 
-impl<T> RawApiResponse<T> {
+impl<T> RawApiResponse<T>
+where
+    T: for<'de> Deserialize<'de>,
+{
     fn check_success(mut self) -> Result<Self, Error> {
         if self.success == Some(true) {
             return Ok(self);
@@ -163,8 +166,16 @@ impl<T> RawApiResponse<T> {
     fn check(self) -> Result<ApiResponseData<T>, Error> {
         let this = self.check_success()?;
 
+        // RawApiResponse has no data, but this also happens for Value::Null, and T
+        // might be deserializeable from that, so try here again
+        let data = match this.data {
+            Some(data) => data,
+            None => serde_json::from_value(Value::Null)
+                .map_err(|_| Error::BadApi("api returned no data".to_string(), None))?,
+        };
+
         Ok(ApiResponseData {
-            data: this.data,
+            data,
             attribs: this.attribs,
         })
     }
-- 
2.39.2




             reply	other threads:[~2023-09-14 10:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-14 10:41 Dominik Csapak [this message]
2023-09-27  9:30 ` [pbs-devel] applied: " Wolfgang Bumiller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230914104130.59082-1-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal