Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MatteoJoliveau opened this issue Feb 15, 2022 · 2 comments

Comments

@MatteoJoliveau
Copy link
Contributor

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"
      ]
    }
  ]
}
@Rexagon
Copy link
Owner

Rexagon commented Feb 15, 2022

It's made on purpose. There may be parameters or description for the entire entity. And the generated code is valid according to the specification, and different code generators from swagger also accept this.

@MatteoJoliveau
Copy link
Contributor Author

I know it's valid, but the documentation becomes lacking and the examples are all messed up by additionalProp1..N.
For instance, it doesn't show the accepted tag values (in this instance, first and second)

image
image

Is there a way to have them mapped correctly? Maybe a macro tag or something along those lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants