oxidecomputer / oxidecomputer/typify

`anyOf` generated code cannot be serialized

Open
#710 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

anyOf
Dominant language
Rust
Stars
898
Forks
114
Avg merge
4h 18m
Merged PRs (30d)
14

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the attached my_types.json and the Rust generated from its anyOf definition; reproduce serialization of the string variant in an RPC request. Trace how the generated type handles the untagged enum and flattening, then verify that the reported serialization failure is resolved or that a documented workaround is established.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.