Skip to content

postcard-schema: handling unsupported serde attributes #276

@Calsign

Description

@Calsign

There are a bunch of serde attributes that are unsupported by postcard, and supporting some attributes is impossible: #125

There are some attributes that are supported by postcard, but break postcard-schema/postcard-dyn, such as #[serde(skip)] on struct fields. Consider this example:

use postcard_schema::{Schema, schema::owned::OwnedNamedType};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Schema)]
struct Foobar {
    x: u32,
    #[serde(skip)]
    y: u32,
}

fn main() {
    let foobar = Foobar { x: 1, y: 2 };

    let encoded = postcard::to_stdvec(&foobar).unwrap();
    let schema = OwnedNamedType::from(Foobar::SCHEMA);

    // this works
    let _decoded: Foobar = postcard::from_bytes(&encoded).unwrap();

    // this fails
    let _json_dyn = postcard_dyn::from_slice_dyn(&schema, &encoded).unwrap();
}

Output:

thread 'main' panicked at src/bin/ex3.rs:21:69:
called `Result::unwrap()` on an `Err` value: UnexpectedEndOfData

I don't expect postcard-schema to handle all of these attributes correctly. But it occurs to me that unlike postcard, postcard-schema has the ability to have the derive macro fail on unsupported schemas.

Currently the only attribute that postcard-schema supports is #[serde(rename)]. Is it reasonable for #[derive(Schema)] to fail for all other serde attributes? (Possibly with opt-in to ignore?) If more attributes get supported in the future then those could be added without breaking backwards-compatibility.

I desire this because I would like to have confidence that if I derive Schema, the derived schema will actually work correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions