Deserialization of enum variant which recursively refers to itself failed with stackoverflow

Aperta
#819 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #982 di @scadastrangelove — chiusa senza merge
  • #995 di @dralley — chiusa senza merge

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
backend

Direzione di ricerca

The report includes a Rust reproduction using from_str::<MathNode> and recursive enum variants. Start by reading the XML deserialization path for enum newtype variants and compare it with the struct variant behavior shown in the issue. Done means the newtype form deserializes the sample without overflowing the stack; the issue names no test file or implementation entry point.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug help wanted serde

Found in https://stackoverflow.com/questions/67932584/deserializing-recursive-xml-using-serde-in-rust

Currently recursively defined enums using newtype variants for recursion lead to stack overflow error:

#[test]
fn recursive() {
    #[derive(Debug, Deserialize, PartialEq)]
    #[serde(rename_all = "camelCase")]
    enum MathNode {
        Apply(Vec<MathNode>),
        Ci(Vec<MathNode>),
        #[serde(rename = "$text")]
        Text(String),
        #[serde(rename = "math")]
        Root(Vec<MathNode>),
    }

    let test = r#"
    <math>
        <apply>
            <ci type="integer">5</ci>
        </apply>
    </math>"#;
    assert_eq!(
        from_str::<MathNode>(test).unwrap(),
        MathNode::Root(vec![MathNode::Apply(vec![MathNode::Ci(vec![
            MathNode::Text("5".into())
        ])])])
    );
}

However, if change newtype variant to the struct variant with one $value field, everything works:

#[test]
fn recursive() {
    #[derive(Debug, Deserialize, PartialEq)]
    #[serde(rename_all = "camelCase")]
    enum MathNode {
        Apply {
            #[serde(rename = "$value")]
            value: Vec<MathNode>,
        },
        Ci {
            #[serde(rename = "$value")]
            value: Vec<MathNode>,
        },
        #[serde(rename = "$text")]
        Text(String),
        #[serde(rename = "math")]
        Root {
            #[serde(rename = "$value")]
            value: Vec<MathNode>,
        },
    }

    let test = r#"
    <math>
        <apply>
            <ci type="integer">5</ci>
        </apply>
    </math>"#;
    assert_eq!(
        from_str::<MathNode>(test).unwrap(),
        MathNode::Root {
            value: vec![MathNode::Apply {
                value: vec![MathNode::Ci {
                    value: vec![MathNode::Text("5".into())]
                }],
            }],
        }
    );
}

It seems worth to implement newtype variant deserialization like such structs

Lingua principale
Rust
Stelle
1.6k
Fork
299
Merge medio
2g 4h
PR unite (30g)
9

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tafia/quick-xml

Tutte le issue di tafia/quick-xml

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.