json icon indicating copy to clipboard operation
json copied to clipboard

Duplicated field in output

Open DevSlashRichie opened this issue 1 year ago • 0 comments

When I use tag on a (flatten) enum, and add a field with the same name, on a struct serde will output a json with a duplicated field.

// struct
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateEvent {
    #[serde(rename = "type")]
    ty: EventTypeDiscriminants,
    from: NaiveDateTime,
    to: NaiveDateTime,
    #[serde(flatten)]
    data: CreateEventType,
}

// CreateEventType
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum CreateEventType {
    #[serde(rename = "field")]
    Field,
    #[serde(rename = "class")]
    Class(CreateClass),
}

output

{
  "type": "class",
  "from": "1970-01-01T00:00:00",
  "to": "1970-01-01T00:00:00",
  "type": "class",
  "data": {
    "teacher_id": "usr_b7a7zAh6XlTeN4azMcCFL"
  }
}

DevSlashRichie avatar Oct 05 '23 20:10 DevSlashRichie