Skip to content

Support non-string enums #1956

@tlammi

Description

@tlammi

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions