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

Incorrect schema for flattened optional untagged enum #367

Open
Masken8 opened this issue Jan 20, 2025 · 0 comments
Open

Incorrect schema for flattened optional untagged enum #367

Masken8 opened this issue Jan 20, 2025 · 0 comments

Comments

@Masken8
Copy link

Masken8 commented Jan 20, 2025

use schemars::{schema_for, JsonSchema};
use serde::{Deserialize, Serialize};

fn main() {
    let schema = schema_for!(Root);
    let schema = serde_json::to_value(&schema).unwrap();
    let validator = jsonschema::validator_for(&schema).unwrap();
    let json = serde_json::json!({
        "blah": "Foo"
    });
    let result = validator.validate(&json).unwrap(); // called `Result::unwrap()` on an `Err` value: ValidationError { instance: Object {"blah": String("Foo")}, kind: AnyOf, instance_path: Location(""), schema_path: Location("/anyOf") }
}

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct BarAndBaz {
    pub bar: String,
    pub baz: String,
}

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct Bar {
    pub bar: String,
}

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum Foo {
    BarAndBaz(BarAndBaz),
    Bar(Bar),
}

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct Root {
    #[serde(flatten)]
    pub foo: Option<Foo>,

    pub blah: String,
}
[package]
name = "workspace"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.135"
schemars = { version = "=1.0.0-alpha.17" }
jsonschema = "0.28.1"
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