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

#[serde(flatten)] crashes when trying to serialize #52

Open
maxomatic458 opened this issue Feb 10, 2024 · 6 comments
Open

#[serde(flatten)] crashes when trying to serialize #52

maxomatic458 opened this issue Feb 10, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@maxomatic458
Copy link

#[serde(rename = "AppState")]
struct AppManifestFile {
    #[serde(rename = "appid")]
    pub app_id: String,
    pub name: String,
    #[serde(rename = "AutoUpdateBehavior")]
    pub auto_update_bahavior: String,
    #[serde(flatten)]
    other: HashMap<String, Value>,
}

deserializing works.

but when converting this object back to a string, it panics with this message: Expected key, found: Some(ObjBegin)

@CosmicHorrorDev CosmicHorrorDev added the bug Something isn't working label Feb 10, 2024
@CosmicHorrorDev
Copy link
Owner

I believe on the latest version this should emit an error instead of panicking at least (unless you're the one panicking of course)

Can you update your repro with the definition for Value? keyvalues_parser::Value doesn't impl (De)Serialize (although it really should)

@CosmicHorrorDev
Copy link
Owner

Ope, spoke too soon. I haven't released those changes yet, so that's only on main atm

@maxomatic458
Copy link
Author

just realized i used serde_json::Value, i think this might be what caused the panic.
In my case i need to have a "any" type there so i will just wait until Serialize is implemented for Value

then i will retest it

@maxomatic458
Copy link
Author

hmm
i think that might not be the cause of the problem here

this also fails:

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "AppState")]
struct AppManifestFile {
    #[serde(rename = "appid")]
    pub app_id: String,
    pub name: String,
    #[serde(rename = "AutoUpdateBehavior")]
    pub auto_update_behavior: String,
    #[serde(flatten)]
    other: HashMap<String, String>,
}

this is the data:

"AppState"
{
	"AllowOtherDownloadsWhileRunning"	"0"
	"AutoUpdateBehavior"	"1"
	"BytesDownloaded"	"13772112"
	"BytesStaged"	"48688316"
	"BytesToDownload"	"13772112"
	"BytesToStage"	"48688316"
	"LastOwner"	"76561199134537034"
	"LastUpdated"	"1707501698"
	"LauncherPath"	"C:\\Program Files (x86)\\Steam\\steam.exe"
	"ScheduledAutoUpdate"	"0"
	"SizeOnDisk"	"36124234611"
	"StagingSize"	"0"
	"StateFlags"	"4"
	"TargetBuildID"	"13410341"
	"Universe"	"1"
	"UpdateResult"	"0"
	"appid"	"730"
	"buildid"	"13410341"
	"installdir"	"Counter-Strike Global Offensive"
	"name"	"Counter-Strike 2"
}

and here is a debug print of the AppManifest object:

AppManifestFile {
    app_id: "730",
    name: "Counter-Strike 2",
    auto_update_behavior: "1",
    other: {
        "LastOwner": "76561199134537034",
        "LastUpdated": "1707501698",
        "UpdateResult": "0",
        "BytesDownloaded": "13772112",
        "LauncherPath": "C:\\Program Files (x86)\\Steam\\steam.exe",
        "SizeOnDisk": "36124234611",
        "TargetBuildID": "13410341",
        "Universe": "1",
        "BytesToStage": "48688316",
        "BytesStaged": "48688316",
        "StagingSize": "0",
        "buildid": "13410341",
        "installdir": "Counter-Strike Global Offensive",
        "BytesToDownload": "13772112",
        "ScheduledAutoUpdate": "0",
        "StateFlags": "4",
        "AllowOtherDownloadsWhileRunning": "0",
    },
}

@CosmicHorrorDev
Copy link
Owner

Perfect, thanks! I'll try to figure out a fix sometime next week

@maxomatic458
Copy link
Author

maxomatic458 commented Feb 12, 2024

here is a full example:

use std::collections::HashMap;
use keyvalues_serde;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Foo {
    #[serde(rename = "KnownField")]
    known_field: String,
    #[serde(rename = "KnownFieldTwo")]
    known_field_two: String,
    #[serde(flatten)]
    other: HashMap<String, String>
}

fn main() {
    let input = r##"
        "Foo"
        {
            "KnownField" "0"
            "KnownFieldTwo" "1"
            "UnknownField" "2"
        }
    "##;

    let serialized: Foo = keyvalues_serde::from_str(input).unwrap();
    let back_to_string = keyvalues_serde::to_string(&serialized).unwrap(); // panics here

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants