Fix flattening of serde_json::Value

It should behave similarly to flattening a `Map<String,Value>` in that it allows any properties
This commit is contained in:
Graham Esau 2024-08-12 21:39:41 +01:00
parent faf15e7859
commit 705aba1cef
3 changed files with 76 additions and 46 deletions

View file

@ -0,0 +1,14 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "FlattenValue",
"type": "object",
"properties": {
"flag": {
"type": "boolean"
}
},
"required": [
"flag"
],
"additionalProperties": true
}

View file

@ -1,5 +1,7 @@
mod util;
use schemars::JsonSchema;
use serde_json::Value;
use std::collections::BTreeMap;
use util::*;
#[allow(dead_code)]
@ -53,5 +55,34 @@ fn test_flat_schema() -> TestResult {
#[test]
fn test_flattened_schema() -> TestResult {
// intentionally using the same file as test_flat_schema, as the schema should be identical
test_default_generated_schema::<Deep1>("flatten")
}
#[allow(dead_code)]
#[derive(JsonSchema)]
struct FlattenValue {
flag: bool,
#[serde(flatten)]
value: Value,
}
#[allow(dead_code)]
#[derive(JsonSchema)]
#[schemars(rename = "FlattenValue")]
struct FlattenMap {
flag: bool,
#[serde(flatten)]
value: BTreeMap<String, Value>,
}
#[test]
fn test_flattened_value() -> TestResult {
test_default_generated_schema::<FlattenValue>("flattened_value")
}
#[test]
fn test_flattened_map() -> TestResult {
// intentionally using the same file as test_flattened_value, as the schema should be identical
test_default_generated_schema::<FlattenMap>("flattened_value")
}