serde-wasm-bindgen icon indicating copy to clipboard operation
serde-wasm-bindgen copied to clipboard

#[serde(alias = "somename")] stopped working after 0.6

Open VegarRingdalAibel opened this issue 1 year ago • 3 comments

After when going from 0.5 to 0.6 it looks like alias is broken

Using:

wasm-bindgen = "0.2.91"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
serde-wasm-bindgen = "0.6.3" //0.5 works... fails if i go higher
console_error_panic_hook="0.1.7"
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FilterAction {
    Append,
    Remove,
    Keep,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FilterType {
    Standard,
    Boundingbox,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FilterInput {
    #[serde(alias = "filtertype")]
    pub filter_action: FilterAction,
    #[serde(alias = "type")]
    pub filter_type: FilterType,
    ...

This is my input image

get error:

panicked at src\lib.rs:228:58:
called `Result::unwrap()` on an `Err` value: Error(JsValue(Error: unknown variant `Append`, expected `Standard` or `Boundingbox`
Error: unknown variant `Append`, expected `Standard` or `Boundingbox`

Its like its setting values from json filtertype to rust filter_type when it should have used json type.

Is it a bug or have I just been lucky it been working ?

VegarRingdalAibel avatar Feb 17 '24 18:02 VegarRingdalAibel

Hm... the last change that transitioned from 0.5 to 0.6 was #40, but I don't see why it would cause this particular breakage.

RReverser avatar Feb 19 '24 21:02 RReverser

To be clear, does it break on transition to any 0.6 or only 0.6.3? Did you try pinning exact versions so that Cargo doesn't fetch the latest 0.6.x? That might help tracking it down further.

Alternatively, you can do git bisect on this repo and add it as a path dependency in Cargo.toml.

RReverser avatar Feb 19 '24 21:02 RReverser

I'll do a new try later this week, so I know better when it breaks. I can try and create a simple sample repo with the bug. 👍

VegarRingdalAibel avatar Feb 20 '24 07:02 VegarRingdalAibel

@RReverser Made a small sample containing 0.5.0(wasm1) and 0.6.0 (wasm2)

https://github.com/vegarringdal/wasm_test

image

VegarRingdalAibel avatar Feb 20 '24 13:02 VegarRingdalAibel

You can see from the Cargo.lock that wasm2 is not 0.6.0 but 0.6.3: https://github.com/vegarringdal/wasm_test/blob/9238388d3217b95fd3cd86bc4a6598c05af72c63/wasm2/Cargo.lock#L87-L89

That's what I suspected and why I mentioned pinning exact version to narrow it down further. I believe you can do that by specifying version as =0.6.0 instead of 0.6.0.

RReverser avatar Feb 20 '24 13:02 RReverser

Ill have a new look, though it would use the version I had here https://github.com/vegarringdal/wasm_test/blob/9238388d3217b95fd3cd86bc4a6598c05af72c63/wasm2/Cargo.toml#L14

VegarRingdalAibel avatar Feb 20 '24 13:02 VegarRingdalAibel

Yeah, no, Cargo auto-updates the version to whatever is compatible according to semver. It's usually the right choice, but not very helpful when debugging an issue.

RReverser avatar Feb 20 '24 13:02 RReverser

I have clearly missed this part 😂

VegarRingdalAibel avatar Feb 20 '24 13:02 VegarRingdalAibel

Error is after version 0.6.2, update repo

VegarRingdalAibel avatar Feb 20 '24 13:02 VegarRingdalAibel

Error is after version 0.6.2, update repo

What do you mean by "update repo"? Looks like you didn't push any new changes?

RReverser avatar Feb 21 '24 15:02 RReverser

That said, if it's 0.6.2+ rather than after 0.6.2 (0.6.3+), then I suspect it might be due to this change https://github.com/RReverser/serde-wasm-bindgen/commit/ce7669e1d175d999a733497e87152f55808ee8f2

Not sure if Serde supports field indices in combination with aliases.

RReverser avatar Feb 21 '24 15:02 RReverser

HI I pushed this change :-)

https://github.com/vegarringdal/wasm_test/commit/0b2fa8a8e28f78150b2a2737fe3c6785164d2241

image

That said, if it's 0.6.2+ rather than after 0.6.2 (0.6.3+), then I suspect it might be due to this change > > https://github.com/RReverser/serde-wasm-bindgen/commit/ce7669e1d175d999a733497e87152f55808ee8f2

Not sure if Serde supports field indices in combination with aliases.

I dont know :-)

VegarRingdalAibel avatar Feb 22 '24 14:02 VegarRingdalAibel

Yup, looking at generated code from serde::Deserialize, the indices it expects don't match the order of fields:

                const FIELDS: &'static [&'static str] =
                    &["filter_action", "filtertype", "filter_type", "type"];

...


                    fn visit_u64<__E>(self, __value: u64)
                        -> _serde::__private::Result<Self::Value, __E> where
                        __E: _serde::de::Error {
                        match __value {
                            0u64 => _serde::__private::Ok(__Field::__field0),
                            1u64 => _serde::__private::Ok(__Field::__field1),
                            _ => _serde::__private::Ok(__Field::__ignore),
                        }
                    }

...

                                __Field::__field0 => {
                                    if _serde::__private::Option::is_some(&__field0) {
                                            return _serde::__private::Err(<__A::Error as
                                                            _serde::de::Error>::duplicate_field("filter_action"));
                                        }
                                    __field0 =
                                        _serde::__private::Some(_serde::de::MapAccess::next_value::<u32>(&mut __map)?);
                                }
                                __Field::__field1 => {
                                    if _serde::__private::Option::is_some(&__field1) {
                                            return _serde::__private::Err(<__A::Error as
                                                            _serde::de::Error>::duplicate_field("filter_type"));
                                        }
                                    __field1 =
                                        _serde::__private::Some(_serde::de::MapAccess::next_value::<String>(&mut __map)?);
                                }
                                _ => {
                                    let _ =
                                        _serde::de::MapAccess::next_value::<_serde::de::IgnoredAny>(&mut __map)?;
                                }

That's pretty unfortunate. I don't think I can properly fix it without some fix on Serde's own side, so I'm just going to revert that optimisation for now :(

RReverser avatar Feb 22 '24 15:02 RReverser

Fixed in 0.6.4. Thanks for the report!

RReverser avatar Feb 22 '24 15:02 RReverser

Thank you.

VegarRingdalAibel avatar Feb 23 '24 06:02 VegarRingdalAibel