use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use v_utils_macros::{Settings, SettingsNested};
#[derive(Clone, Debug, Default, Deserialize, JsonSchema, Serialize, SettingsNested)]
#[allow(unused)]
struct Logging {
#[serde(default)]
level: String,
#[serde(default)]
file: Option<String>,
}
#[derive(Clone, Debug, Default, JsonSchema, v_utils_macros::MyConfigPrimitives, Settings)]
#[allow(unused)]
struct SchemaConfig {
#[serde(default)]
host: String,
#[serde(default)]
port: u16,
#[settings(flatten)]
#[serde(default)]
logging: Logging,
}
#[test]
fn writes_valid_schema_when_jsonschema_derived() {
let tmp = tempfile::tempdir().unwrap();
unsafe {
std::env::set_var("XDG_CONFIG_HOME", tmp.path());
}
let path = SchemaConfig::write_schema().expect("JsonSchema is derived, so this must succeed");
assert!(path.exists(), "schema file should have been written to {}", path.display());
assert_eq!(path.extension().and_then(|e| e.to_str()), Some("json"));
let contents = std::fs::read_to_string(&path).unwrap();
let schema: Value = serde_json::from_str(&contents).expect("output must be valid JSON");
let props = schema.get("properties").and_then(Value::as_object).expect("schema should have a properties object");
assert!(props.contains_key("host"), "schema should describe the `host` field");
assert!(props.contains_key("port"), "schema should describe the `port` field");
assert!(props.contains_key("logging"), "schema should describe the flattened `logging` field");
}