Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 52 additions & 19 deletions agent-client-protocol-schema/src/v1/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4021,13 +4021,42 @@ pub enum SessionConfigOptionValue {
/// This is the default when `type` is absent on the wire. Unknown `type`
/// values with string payloads also gracefully deserialize into this
/// variant.
#[serde(untagged)]
#[serde(untagged, deserialize_with = "deserialize_config_value_id_fallback")]
// Without `required` inside `not`, the schema would also reject legacy IDs with no `type`.
#[cfg_attr(feature = "schemars", schemars(extend("not" = {
"properties": {"type": {"const": "boolean"}},
"required": ["type"]
})))]
ValueId {
/// The value ID.
value: SessionConfigValueId,
},
}

fn deserialize_config_value_id_fallback<'de, D>(
deserializer: D,
) -> Result<SessionConfigValueId, D::Error>
where
D: serde::Deserializer<'de>,
{
// An untagged struct variant accepts objects, not positional arrays.
#[derive(Deserialize)]
#[serde(untagged)]
enum Fallback {
Id {
#[serde(default, rename = "type")]
value_type: serde_json::Value,
value: SessionConfigValueId,
},
}

let Fallback::Id { value_type, value } = Fallback::deserialize(deserializer)?;
if value_type == "boolean" {
return Err(serde::de::Error::custom("expected a boolean config value"));
}
Ok(value)
}

impl SessionConfigOptionValue {
/// Create a value-id option value (used by `select` and other id-based option types).
#[must_use]
Expand Down Expand Up @@ -7918,14 +7947,20 @@ mod test_serialization {
serde_json::from_value::<NewSessionRequest>(value).unwrap(),
request
);
assert!(
serde_json::from_value::<NewSessionRequest>(json!({
"cwd": "/home/user/project",
"mcpServers": [],
"configOptions": [{"type": "boolean", "value": true}]
}))
.is_err()
);
for invalid in [
json!({"type": "boolean", "value": true}),
json!({"configId": "fast", "type": "boolean", "value": "false"}),
] {
assert!(
serde_json::from_value::<NewSessionRequest>(json!({
"cwd": "/home/user/project",
"mcpServers": [],
"configOptions": [invalid]
}))
.is_err(),
"{invalid}"
);
}
assert_eq!(
serde_json::to_value(SessionOptionsResponse::new()).unwrap(),
json!({})
Expand Down Expand Up @@ -8526,16 +8561,14 @@ mod test_serialization {
}

#[test]
fn test_session_config_option_value_type_mismatch_boolean_with_string() {
// type says "boolean" but value is a string — falls to untagged ValueId
let json = json!({ "type": "boolean", "value": "not a bool" });
let result = serde_json::from_value::<SessionConfigOptionValue>(json);
// serde tries Boolean first (fails), then falls to untagged ValueId (succeeds)
assert!(result.is_ok());
assert_eq!(
result.unwrap().as_value_id().unwrap().to_string(),
"not a bool"
);
fn test_set_session_config_option_request_rejects_boolean_with_string() {
let params = json!({
"sessionId": "s",
"configId": "fast",
"type": "boolean",
"value": "false"
});
assert!(serde_json::from_value::<SetSessionConfigOptionRequest>(params).is_err());
}

#[test]
Expand Down
26 changes: 26 additions & 0 deletions schema-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,32 @@ mod schema_annotation_tests {
}
}

#[cfg(not(feature = "unstable_protocol_v2"))]
#[test]
fn generated_v1_config_value_id_fallback_excludes_boolean() {
let schema = root_schema_value();
for name in [
"SetSessionConfigOptionRequest",
#[cfg(feature = "unstable")]
"InitialSessionConfigOption",
] {
let fallback = def_schema(&schema, name)["anyOf"]
.as_array()
.unwrap()
.iter()
.find(|variant| variant["title"] == "value_id")
.unwrap();
assert_eq!(
fallback["not"],
serde_json::json!({
"properties": {"type": {"const": "boolean"}},
"required": ["type"]
}),
"{name}: exclude the boolean tag, not an omitted type"
);
}
}

#[cfg(feature = "unstable_protocol_v2")]
#[test]
fn generated_v2_fallback_schema_rejects_compaction_discriminators() {
Expand Down
10 changes: 9 additions & 1 deletion schema/v1/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6451,7 +6451,15 @@
]
}
},
"required": ["value"]
"required": ["value"],
"not": {
"properties": {
"type": {
"const": "boolean"
}
},
"required": ["type"]
}
}
],
"x-side": "agent",
Expand Down
20 changes: 18 additions & 2 deletions schema/v1/schema.unstable.json
Original file line number Diff line number Diff line change
Expand Up @@ -7943,7 +7943,15 @@
]
}
},
"required": ["value"]
"required": ["value"],
"not": {
"properties": {
"type": {
"const": "boolean"
}
},
"required": ["type"]
}
}
]
},
Expand Down Expand Up @@ -8471,7 +8479,15 @@
]
}
},
"required": ["value"]
"required": ["value"],
"not": {
"properties": {
"type": {
"const": "boolean"
}
},
"required": ["type"]
}
}
],
"x-side": "agent",
Expand Down