Preserve string and boolean literal constraints in strict schemas

This commit is contained in:
Afloat 2026-10-20 21:22:43 +08:00
parent 935817c66f
commit d44bf1eb62

View File

@ -78,6 +78,8 @@ pub(crate) enum JsonSchemaType {
format: Option<StringFormat>, format: Option<StringFormat>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
r#enum: Option<Vec<String>>, r#enum: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
r#const: Option<String>,
}, },
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
Number { Number {
@ -121,6 +123,8 @@ pub(crate) enum JsonSchemaType {
Boolean { Boolean {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>, description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
r#const: Option<bool>,
}, },
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
Array { Array {
@ -364,3 +368,28 @@ mod tests {
); );
} }
} }
#[cfg(test)]
mod contribution_regression {
use super::*;
#[test]
fn primitive_constants_survive_normalization() {
for property in [
serde_json::json!({"type":"string","const":"fixed"}),
serde_json::json!({"type":"boolean","const":true}),
] {
let result = normalize_strict_schema(
serde_json::json!({"type":"object","properties":{"value":property.clone()}}),
true,
|e| e.to_string(),
"root",
)
.unwrap();
assert_eq!(result["properties"]["value"], property);
let validator = jsonschema::options().offline().build(&result).unwrap();
assert!(validator.is_valid(&serde_json::json!({"value":property["const"]})));
assert!(!validator.is_valid(&serde_json::json!({"value":if property["type"] == "string" {serde_json::json!("other")} else {serde_json::json!(false)}})));
}
}
}