Closed bdarcus closed 1 year ago
I still haven't diagnosed why this is happening, but this minimal example works as expected, for schema and rust.
use schemars::{schema_for, JsonSchema}; #[derive(JsonSchema, Default)] pub struct Config { pub group: Option<Group>, pub substitute: Option<Substitute>, pub sort: Option<Sort>, pub localize: Option<Localize>, pub contributors: Option<Contributors>, pub date: Option<Date>, } #[test] fn sets_group_option() { let config = Config::default(); assert!(config.group.is_none(), "{}", true); } #[test] fn sets_default_date() { let config = Config::default(); assert_eq!(config.date.unwrap_or_default().month, "long".to_string()); } #[test] fn sets_default_contributors() { let config = Config::default(); assert_eq!(config.contributors.unwrap_or_default().foo, "".to_string()); } impl Default for Contributors { fn default() -> Self { Self { foo: "".to_string(), } } } #[derive(JsonSchema)] pub struct Date { pub month: String, } impl Default for Date { fn default() -> Self { Self { month: "long".to_string(), } } } #[derive(JsonSchema)] pub struct Contributors { pub foo: String, } #[derive(JsonSchema)] pub struct Localize { pub scope: Scope, } #[derive(JsonSchema, PartialEq, Debug)] #[serde(rename_all = "kebab-case")] pub enum Scope { Global, PerItem, } impl Default for Localize { fn default() -> Self { Self { scope: Scope::Global, } } } #[test] fn sets_default_localize_scope() { let config = Config::default(); assert_eq!(config.localize.unwrap_or_default().scope, Scope::Global); } #[derive(JsonSchema)] pub struct Group { pub template: Vec<SortKey>, } #[derive(JsonSchema)] pub struct Substitute { pub template: Vec<SubstituteKey>, } impl Default for Substitute { fn default() -> Self { Self { template: vec![ SubstituteKey::Editor, SubstituteKey::Title, SubstituteKey::Translator, ], } } } #[test] fn sets_default_substitute_template() { let config = Config::default(); assert_eq!(config.substitute.unwrap_or_default().template.len(), 3); } #[test] fn sets_empty_default_sort_config() { let config = Config::default(); // the result for config.sort should be None assert!(config.sort.is_none(), "{}", true); } // These two do orrectly translate. fn default_foo() -> String { "foo-default".to_string() } fn default_bar() -> String { "bar-default".to_string() } #[derive(JsonSchema)] pub struct Sort { pub options: Option<SortOptions>, pub template: SortSpec, } #[derive(JsonSchema)] pub struct SortOptions { #[serde(default = "default_foo")] pub foo: String, #[serde(default = "default_bar")] pub bar: String, } #[derive(JsonSchema)] pub struct SortSpec { pub key: SortKey, #[serde(default = "default_ascending")] pub ascending: bool, } fn default_ascending() -> bool { true } #[derive(Default, JsonSchema)] #[serde(rename_all = "lowercase")] pub enum SortKey { #[default] Author, Year, Title, } #[derive(Default, JsonSchema)] #[serde(rename_all = "lowercase")] pub enum SubstituteKey { #[default] Editor, Title, Translator, } fn main() { let schema = schema_for!(Config); println!("{}", serde_json::to_string_pretty(&schema).unwrap()); }
Fixed via #83
I still haven't diagnosed why this is happening, but this minimal example works as expected, for schema and rust.