wyyerd / stripe-rs

Rust API bindings for the Stripe HTTP API.
Apache License 2.0
224 stars 88 forks source link

Fails to compile with serde v1.0.119 #158

Closed c1wren closed 3 years ago

c1wren commented 3 years ago

In serde v1.0.119, it is no longer possible to compile because serde::private is now a private module. The compiler fails at use serde::private::de::{Content, ContentRefDeserializer}; which is this line: https://github.com/wyyerd/stripe-rs/blob/58c06e798d52bf8a6398b1d556be93e9dce95f7b/src/resources/payment_source.rs#L30

seanpianka commented 3 years ago

This issue has a workaround.

jonahbron commented 3 years ago

To be more explicit, the workaround (or what worked for me) was to point to @seanpianka 's fork, and update my own project to depend on the same Serde versions.

stripe-rust = { git = "https://github.com/seanpianka/stripe-rs.git", branch = "fix-serde" }
serde = "<1.0.118, >=1.0.79"
serde_derive = "<1.0.118, >=1.0.79"
seanpianka commented 3 years ago

It doesn't look like the maintainers have been active recently with this project, so I'll maintain this fork until this fix is merged to avoid the specific broken version. Once the maintainers merge in this fix, I will delete that fork... but I'll warn here before I do.

marcusbuffett commented 3 years ago

It seems like as long as you pin serde to a version before 1.0.119, things work out, ex I have this in my project:

serde = { version = "=1.0.118", features = ["derive"] }
rrrodzilla commented 3 years ago

{ version = "=1.0.118", features = ["derive"] }

@marcusbuffett Yep, that works great. Thanks for that!

seanpianka commented 3 years ago

The fix for this has been merged to master, along with Tokio 1.0 updates:

stripe-rust = { git = "https://https://github.com/wyyerd/stripe-rs.git", branch = "master" }
arlyon commented 3 years ago

As far as I can tell, we can avoid using the internal apis altogether (and delete the manual serialize / deserialize code) by using serde untagged right? If I am understanding right, I'll open a PR

diff --git a/src/resources/payment_source.rs b/src/resources/payment_source.rs
index 6fe9704..cc05822 100644
--- a/src/resources/payment_source.rs
+++ b/src/resources/payment_source.rs
@@ -10,7 +10,8 @@ use serde_derive::{Deserialize, Serialize};
 ///
 /// Not to be confused with `SourceParams` which is used by `Source::create`
 /// to create a source that is not necessarily attached to a customer.
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Serialize, Deserialize)]
+#[serde(untagged)]
 pub enum PaymentSourceParams {
     /// Creates a payment method (e.g. card or bank account) from tokenized data,
     /// using a token typically received from Stripe Elements.
@@ -21,48 +22,15 @@ pub enum PaymentSourceParams {
     Source(SourceId),
 }

-impl<'de> ::serde::Deserialize<'de> for PaymentSourceParams {
-    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
-    where
-        D: ::serde::de::Deserializer<'de>,
-    {
-        use serde::de::{Deserialize, Error};
-        use serde::private::de::{Content, ContentRefDeserializer};
-        let content = <Content<'_> as Deserialize>::deserialize(deserializer)?;
-        let deserializer = ContentRefDeserializer::<D::Error>::new(&content);
-        if let Ok(ok) = <SourceId as Deserialize>::deserialize(deserializer) {
-            return Ok(PaymentSourceParams::Source(ok));
-        }
-        let deserializer = ContentRefDeserializer::<D::Error>::new(&content);
-        if let Ok(ok) = <TokenId as Deserialize>::deserialize(deserializer) {
-            return Ok(PaymentSourceParams::Token(ok));
-        }
-
-        Err(Error::custom("data did not match any variant of enum PaymentSourceParams"))
-    }
-}
-
-impl<'a> ::serde::Serialize for PaymentSourceParams {
-    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
-    where
-        S: ::serde::ser::Serializer,
-    {
-        match self {
-            PaymentSourceParams::Source(id) => id.serialize(serializer),
-            PaymentSourceParams::Token(id) => id.serialize(serializer),
-        }
-    }
-}
-
seenickcode commented 3 years ago

@wyyerd it may be a good idea to give certain contributors write access to this repo so we can ensure it's maintained promptly.

seanpianka commented 3 years ago

Perhaps we can open another issue about this? I agree that we need a community member with write access to this repository...

seenickcode commented 3 years ago

Done. https://github.com/wyyerd/stripe-rs/issues/168

seanpianka commented 3 years ago

@arlyon Did you open a PR or make these changes in a fork somewhere? I think it would be valuable to support newer versions of Serde.

Edit: Opened #169