`anyOf` generated code cannot be serialized

Aperta
#710 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Inizia con il file my_types.json allegato e con il Rust generato dalla relativa definizione anyOf; riproduci la serializzazione della variante string in una richiesta RPC. Traccia il modo in cui il tipo generato gestisce l’untagged enum e il flattening, quindi verifica che l’errore di serializzazione segnalato sia stato risolto o che sia stata stabilita una soluzione alternativa documentata.

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

Descrizione

anyOf

In our codebase, we are using the following structs, which are part of an ETH RPC API built on top of our Filecoin node implementation:

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Predefined {
    Earliest,
    Pending,
    #[default]
    Latest,
    Safe,
    Finalized,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct BlockNumber {
    block_number: EthInt64,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct BlockHash {
    block_hash: EthHash,
    require_canonical: bool,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum BlockNumberOrHash {
    #[schemars(with = "String")]
    PredefinedBlock(Predefined),
    BlockNumber(EthInt64),
    BlockHash(EthHash),
    BlockNumberObject(BlockNumber),
    BlockHashObject(BlockHash),
}

We're using typify to create a JSON OpenRPC specification to establish a Common Node API.

Our issue is that the untagged BlockNumberOrHash enum is translated into a definition that uses anyOf (see attached json file). The Rust code generated from this JSON specification is used in some tooling, and when trying to use the string variant to create an RPC request, we encounter the following error:

        couldn't serialize params
        can only flatten structs and maps (got a string)

Do you have any ideas on how to work around this kind of issue? For example, could we generate a oneOf (XOR) constraint instead?

Many thanks.

my_types.json

Lingua principale
Rust
Stelle
898
Fork
114
Merge medio
4h 18m
PR unite (30g)
14

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

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 oxidecomputer/typify

Tutte le issue di oxidecomputer/typify

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.