-
-
Notifications
You must be signed in to change notification settings - Fork 235
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
Option<bool> gets required in schema #11
Comments
My understanding was that deserializing an However I just tested this and it's clear I was wrong - when deserializing use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
struct MyStruct {
pub foo: Option<bool>
}
fn main() {
let json = r#"{ "foo": false }"#;
let s: MyStruct = serde_json::from_str(json).unwrap();
assert_eq!(s.foo, Some(false));
let json = r#"{ "foo": null }"#;
let s: MyStruct = serde_json::from_str(json).unwrap();
assert_eq!(s.foo, None);
let json = "{ }";
let s: MyStruct = serde_json::from_str(json).unwrap();
assert_eq!(s.foo, None);
} In other words, this is indeed a bug in schemars! I don't think this should be controlled by an option in |
Unfortunately I don't have a lot of time right now so this probably won't be fixed for a few weeks - but I'll try to get round to fixing this at some point... |
No problem at all. Perhaps I could take a stab at it if you have any suggestion of how it might work? |
Sorry, missed your previous response. I can take a look at it and see if I can fix it. |
Ideally, the fix for this should work for all paths to use std::option::Option as Opt;
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
struct MyStruct {
pub foo: Option<bool>,
pub bar: std::option::Option<bool>,
pub baz: Opt<bool>,
} Similarly, a field of a user-defined #[derive(Debug, PartialEq, Deserialize, Serialize, JsonSchema)]
struct Option<T>(T);
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
struct MyStruct {
// foo is required
pub foo: Option<bool>
} Getting this behaviour from a proc-macro may be awkward though... |
Fixed in v0.7.0-alpha-2 |
Hello, and thank you for a great project!
I suspect I've encountered a bug (or I'm holding it wrong), but for some reason optional bools are set as required in definitions.
The version I'm using is
0.7.0-alpha-1
Generates the following schema:
If I add
#[serde(default)]
tofoo
it removes the requirement.Would it make sense to add an option to
SchemaSettings
that says "Don't make Options required". I realize that the logic for this probably is more complicated since we have to takeoption_nullable
andoption_add_null_type
into account, but having some kind of sensible default for this would make adoption easier.Again, thanks for your great work. Much appreciated!
The text was updated successfully, but these errors were encountered: