schemars
schemars copied to clipboard
Schemars omit serde(flatten) BTreeMap
use schemars::{schema_for, JsonSchema};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, JsonSchema)]
pub struct MyStruct {
pub values: HashMap<String, String>,
#[serde(flatten)]
pub flatten: HashMap<String, String>,
}
fn main() {
let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyStruct",
"type": "object",
"required": [
"values"
],
"properties": {
"values": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
That schema doesn't seem to correspond to those type definitions at all.
Sorry. I pasted the wrong rust type. I've updated the example
Fwiw, it looks like a workaround for now is to add #[schemars(deny_unknown_fields)] to MyStruct.
This is fixed in schemars 1.0.0-alpha.6 (and I think earlier alphas) - the output from the repro is now:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "MyStruct",
"type": "object",
"properties": {
"values": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"additionalProperties": {
"type": "string"
},
"required": [
"values"
]
}