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

Nested internally tagged enums #311

Open
bradzacher opened this issue Aug 6, 2024 · 0 comments
Open

Nested internally tagged enums #311

bradzacher opened this issue Aug 6, 2024 · 0 comments

Comments

@bradzacher
Copy link

I have the following enums:

#[derive(JsonSchema, Serialize)]
#[serde(tag = "foo")]
enum Foo {
  Bar(Bar),
}
#[derive(JsonSchema, Serialize)]
#[serde(tag = "bar")]
enum Bar {
  Baz(Baz),
}

#[derive(JsonSchema, Serialize)]
#[schemars(deny_unknown_fields)]
struct Baz {
  prop: String
}

And it generates the following JSON schema

{
  "oneOf": [
    {
      "type": "object",
      "oneOf": [
        {
          "type": "object",
          "required": [
            "bar",
            "prop"
          ],
          "properties": {
            "bar": {
              "type": "string",
              "enum": [
                "Baz"
              ]
            },
            "prop": {
              "type": "string"
            }
          },
          "additionalProperties": false
        }
      ],
      "required": [
        "foo"
      ],
      "properties": {
        "foo": {
          "type": "string",
          "enum": [
            "Bar"
          ]
        }
      }
    }
  ]
}

For example attempting to validate this object:

{
  "foo": "Bar",
  "bar": "Baz",
  "prop": ""
}

Leads to an error like this in tools like jsonschemavalidator

Property 'foo' has not been defined and the schema does not allow additional properties.

or this in vscode:

Property foo is not allowed.

If you turn off #[schemars(deny_unknown_fields)] for the inner struct, Baz, then the schema passes - however this also means that ofc you can add extra properties -- which is sub-optimal.

If you also add #[schemars(deny_unknown_fields)] to the enums then you end up with even worse errors because the bar and prop fields aren't declared on the outer object - so they also error.

The issue is that from what I can tell validators do not "correctly" consider additionalProperties within nested oneOfs.

I don't know the best path forward here. I can see that if we were to flatten the nested definitions down into a single-level of oneOf then it will work as expected -- but I suspect this would be quite a complicated thing to do.

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

1 participant