Skip to content
This repository was archived by the owner on Jun 17, 2026. It is now read-only.
This repository was archived by the owner on Jun 17, 2026. It is now read-only.

facet-json: Incorrect de/serialization of flattened adjacently-tagged enums #4

Description

@kylewlacy

Easiest to show with an example:

#[derive(Facet, Clone, PartialEq, Debug)]
pub struct Wrapper {
    value: String,
    #[facet(flatten)]
    inner: AdjacentlyTagged,
}

#[derive(Facet, Clone, PartialEq, Debug)]
#[repr(u8)]
#[facet(tag = "tag", content = "content")]
pub enum AdjacentlyTagged {
    Foo { foo: String },
    Bar { bar: String },
}

fn main() {
  let value = Wrapper {
    value: "".to_string(),
    inner: AdjacentlyTagged::Foo { foo: "".to_string() },
  };
  let json = facet_json::to_string(&value).unwrap();
  println!("Serialized:\n{json}");
  println!("Deserialized:\n{:?}", facet_json::from_str::<Wrapper>(&json));
}

(Tested with facet = "=0.44.1" and facet-json locked to 451b761)

facet_json serializes this type as:

{
  "value": "",
  "Foo": {
    "tag": "Foo",
    "content": {"foo": ""}
  }
}

...which is to say, the inner field gets wrapped in an extra field named after its variant, as if it were externally-tagged (I think). Instead, I'd expect this (i.e. what serde_json does):

{
  "value": "",
  "tag": "Foo",
  "content": {"foo": ""}
}

Deserialization fails with either version of the JSON.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions