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

a second flattened enum in a structure is omitted from the schema #165

Closed
dimbleby opened this issue Jul 23, 2022 · 4 comments · Fixed by #320
Closed

a second flattened enum in a structure is omitted from the schema #165

dimbleby opened this issue Jul 23, 2022 · 4 comments · Fixed by #320

Comments

@dimbleby
Copy link
Contributor

dimbleby commented Jul 23, 2022

Somewhat related to #164. I considered just adding this as a comment there but decided that it was different enough to get its own issue. If you think they're basically the same thing then of course just close one out as a duplicate.

Start with a structure that contains two flattened enums:

#[derive(JsonSchema)]
pub struct MyStruct {
    #[schemars(flatten)]
    pub enumeration1: MyEnum1,
    #[schemars(flatten)]
    pub enumeration2: MyEnum2,
}

#[derive(JsonSchema)]
#[schemars(rename_all = "lowercase")]
pub enum MyEnum1 {
    Enum1Variant(String),
}

#[derive(JsonSchema)]
#[schemars(rename_all = "lowercase")]
pub enum MyEnum2 {
    Enum2Variant(String),
}

Then the resulting schema makes no mention of MyEnum2 / enum2variant at all:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "oneOf": [
    {
      "type": "object",
      "required": [
        "enum1variant"
      ],
      "properties": {
        "enum1variant": {
          "type": "string"
        }
      },
      "additionalProperties": false
    }
  ]
}

If the enums had more variants then a correct schema would seem to need to be some sort of product of possibilities, which certainly sounds awkward.

@GREsau
Copy link
Owner

GREsau commented Aug 19, 2024

Fixed in v1.0.0-alpha.7 🙂

@dimbleby
Copy link
Contributor Author

not really?

the above example now produces an unsatisfiable schema, somewhat like #164

 {
   "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "MyStruct",
    "type": "object",
    "allOf": [
      {
        "oneOf": [
          {
            "type": "object",
            "properties": {
              "enum1variant": {
                "type": "string"
              }
            },
            "additionalProperties": false,
            "required": [
              "enum1variant"
            ]
          }
        ]
      },
      {
        "oneOf": [
          {
            "type": "object",
            "properties": {
              "enum2variant": {
                "type": "string"
              }
            },
            "additionalProperties": false,
            "required": [
              "enum2variant"
            ]
          }
        ]
      }
    ]
  }

the first entry requires that the only allowed property is enum1variant, the second entry requires that the only allowed property is enum2variant, these cannot both be satisfied at the same time

@GREsau
Copy link
Owner

GREsau commented Aug 19, 2024

Ah you're right, it still needs to remove the additionalProperties

@GREsau
Copy link
Owner

GREsau commented Aug 22, 2024

OK, it should be fixed for real this time in 1.0.0-alpha.10 - the example above now produces:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "MyStruct",
  "type": "object",
  "allOf": [
    {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "enum1variant": {
              "type": "string"
            }
          },
          "required": [
            "enum1variant"
          ]
        }
      ]
    },
    {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "enum2variant": {
              "type": "string"
            }
          },
          "required": [
            "enum2variant"
          ]
        }
      ]
    }
  ]
}

@GREsau GREsau closed this as completed Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants