Skip to content

Externally tagged enums don't work correctly when using struct variants #18

@MatteoJoliveau

Description

@MatteoJoliveau

When generating schemas for externally tagged enums containing struct variants, the oneOf block gets embedded inside additionalProperties instead of being top-level in the schema.

I tried to debug the proc macro, but my knowledge of Rust macros is fairly lacking and I wasn't able to track down the point where the schema gets generated. If you could point me in the right direction I could open a PR to fix this.

Example

Rust

#[derive(Clone, Debug, Deserialize, Serialize, OpgModel)]
#[serde(rename_all = "camelCase")]
pub enum ExampleEnum {
    First { hello: String },
    Second { world: String },
}

Example payload

{
  "first": {
    "hello": "hi!",
  }
}

{
  "second": {
    "world": "mondo",
  }
}

Currently generated schema

"ExampleEnum": {
  "type": "object",
  "additionalProperties": {
    "oneOf": [
      {
        "type": "object",
        "properties": {
          "hello": {
            "type": "string"
          }
        },
        "required": [
          "hello"
        ]
      },
      {
        "type": "object",
        "properties": {
          "world": {
            "type": "string"
          }
        },
        "required": [
          "world"
        ]
      }
    ]
  }
},

Expected Schema

"ExampleEnum": {
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "first": {
          "type": "object",
          "properties": {
            "hello": {
              "type": "string"
            }
          },
          "required": [
            "hello"
          ]
        }
      },
      "required": [
        "first"
      ]
    },
    {
      "type": "object",
      "properties": {
        "second": {
          "type": "object",
          "properties": {
            "world": {
              "type": "string"
            }
          },
          "required": [
            "world"
          ]
        }
      },
      "required": [
        "second"
      ]
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions