quicktype / quicktype

Generate types and converters from JSON, Schema, and GraphQL

Home Page:https://app.quicktype.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support non-string enums

tlammi opened this issue · comments

Enumeration fields with non-string values seem to be ignored.

I tested this with C++ and Rust

Sample schema

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Tmp",
    "definitions": {
        "Tmp": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "version": {
                    "enum": [0, 1, null, true, false, "string1", "string2"]
                }
            },
            "required": [
                "version"
            ],
            "title": "Tmp"
        }
    }
}

Generated Rust code (selected since this is short):

extern crate serde_derive;

#[derive(Serialize, Deserialize)]
pub struct Tmp {
    #[serde(rename = "version")]
    version: Option<VersionUnion>,
}

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum VersionUnion {
    Bool(bool),

    Double(f64),

    Enum(VersionEnum),
}

#[derive(Serialize, Deserialize)]
pub enum VersionEnum {
    #[serde(rename = "string1")]
    String1,

    #[serde(rename = "string2")]
    String2,
}

If type is explicitly set to e.g. "integer" and enumeration values are ints, no enumeration is generated.