Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OneOrMany serialization not implemented? #728

Closed
adwhit opened this issue Apr 16, 2024 · 4 comments
Closed

OneOrMany serialization not implemented? #728

adwhit opened this issue Apr 16, 2024 · 4 comments

Comments

@adwhit
Copy link
Contributor

adwhit commented Apr 16, 2024

Hi, thanks for the great crate, I use it all the time.

The docs seems to suggest that OneOrMany will serialize a Vec as a singleton or list depending upon config. There is even a helpful example. However the example does not actually demo serialization and in face serialization does not actually work. Inspecting the generated code with cargo expand, it seems to do nothing at all.

Repro:

    #[test]
    fn test_serde_one_many() {
        use serde_with::{serde_as, OneOrMany};

        #[serde_as]
        #[derive(serde::Deserialize, serde::Serialize)]
        struct Data {
            #[serde_as(deserialize_as = "OneOrMany<_>")]
            countries: Vec<AssetId>,
        }
        let data = Data {
            countries: vec!["Spain".to_string().into()],
        };
        let expect = serde_json::json!({
            "countries": "Spain",
        });
        assert_eq!(serde_json::to_value(data).unwrap(), expect);
    }
@adwhit
Copy link
Contributor Author

adwhit commented Apr 16, 2024

OK, I realized this is just a docs issue. If we change the annotation to

#[serde_as(deserialize_as = "OneOrMany<_>", serialize_as = "OneOrMany<_>")]

it works correctly.

@jonasbb
Copy link
Owner

jonasbb commented Apr 16, 2024

In your first post you only specify deserialize_as. This means you are requesting the logic to only run during deserialization. If you want to run during serialization too, just use as or combine serialize_as and deserialize_as like your second post. You can even specify how you want single elements to be serialized with PreferOne or PreferMany.

@jonasbb jonasbb closed this as completed Apr 16, 2024
@adwhit
Copy link
Contributor Author

adwhit commented Apr 16, 2024

I appreciate that I was using it wrong. As I mentioned in my second comment, its because the docs are wrong or at least misleading - see https://docs.rs/serde_with/latest/serde_with/struct.OneOrMany.html. They don't specify to use serialize_as, and the example seems to suggest that it demonstrates serialization but in fact only shows deserialization. So probably the example should be updated.

jonasbb added a commit that referenced this issue Apr 17, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
dtzWill Will Dietz
@jonasbb
Copy link
Owner

jonasbb commented Apr 17, 2024

Yes indeed. I update the doctest to ensure serialization is tested and align it more with the other examples.

jonasbb added a commit that referenced this issue Apr 17, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
dtzWill Will Dietz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants