Skip to content

Multiple case variants and aliases are not accounted for in JSON schema #4

Description

@sergiimk

When using case-enums-any feature the generated JSON Schema only allows a single style of variant.

This is a known issue in schemars library:
GREsau/schemars#338

Minimal test:

#[test]
fn x() {
    #[derive(serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
    enum EnumStr {
        #[serde(alias = "Bar")]
        Foo,
    }

    let schema = schemars::schema_for!(EnumStr);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());

    #[derive(serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
    #[serde(tag = "kind")]
    enum EnumUnnamed {
        #[serde(alias = "Bar")]
        Foo(Foo),
    }

    #[derive(serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
    struct Foo {
        a: u32,
    }

    let schema = schemars::schema_for!(EnumUnnamed);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}

Outputs:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "EnumStr",
  "type": "string",
  "enum": [
    "Foo"
  ]
}
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "EnumUnnamed",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "Foo"
        }
      },
      "$ref": "#/$defs/Foo",
      "required": [
        "kind"
      ]
    }
  ],
  "$defs": {
    "Foo": {
      "type": "object",
      "properties": {
        "a": {
          "type": "integer",
          "format": "uint32",
          "minimum": 0
        }
      },
      "required": [
        "a"
      ]
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    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