glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
12.12k stars 1.06k forks source link

generated rust code breaks when object literal name is `Option` #2385

Closed darklyspaced closed 8 months ago

darklyspaced commented 1 year ago

there is a name conflict between Option<T> and the Option generated by quicktype, resulting in rust complaining that Option doesn't have any generic arguments as it's definition has been overridden from the prelude.

MRE

{
    "option": {
        "name": "test",
        "age": 5
    }
}
use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Welcome {
    pub option: Option<Option>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Option {
    pub name: Option<String>,
    pub age: Option<i64>,
}

the rust code produces by quicktype fails to compile.

this should be pretty simple to fix: just add a case when a struct is called Option. call it something else and just #[serde(rename = "option")].

dvdsgl commented 1 year ago

Adding Option to the keywords list will fix this.