fix(gateway): allow clearing API key IP whitelists

This commit is contained in:
RWDai
2026-05-20 13:41:14 +08:00
parent 149651f831
commit 1604a6d87d
4 changed files with 74 additions and 7 deletions
@@ -1,5 +1,6 @@
use super::ADMIN_USERS_DATA_UNAVAILABLE_DETAIL;
use crate::handlers::admin::shared::AdminTypedObjectPatch;
use crate::handlers::shared::deserialize_optional_string_list_patch;
use axum::{
body::Body,
http,
@@ -51,7 +52,7 @@ pub(super) struct AdminUpdateUserApiKeyRequest {
pub(super) concurrent_limit: Option<i32>,
#[serde(default)]
pub(super) feature_settings: Option<Option<Value>>,
#[serde(default)]
#[serde(default, deserialize_with = "deserialize_optional_string_list_patch")]
pub(super) allowed_ips: Option<Option<Vec<String>>>,
}
@@ -388,7 +389,8 @@ pub(super) fn format_optional_datetime_iso8601(
#[cfg(test)]
mod tests {
use super::normalize_admin_user_api_formats;
use super::{normalize_admin_user_api_formats, AdminUpdateUserApiKeyRequest};
use serde_json::json;
#[test]
fn admin_user_api_formats_accept_current_canonical_signatures() {
@@ -425,4 +427,31 @@ mod tests {
);
}
}
#[test]
fn admin_update_api_key_distinguishes_missing_null_and_present_allowed_ips() {
let missing = serde_json::from_value::<AdminUpdateUserApiKeyRequest>(json!({
"name": "unchanged-whitelist",
}))
.expect("missing allowed_ips should deserialize");
assert_eq!(missing.allowed_ips, None);
let cleared = serde_json::from_value::<AdminUpdateUserApiKeyRequest>(json!({
"allowed_ips": null,
}))
.expect("null allowed_ips should deserialize");
assert_eq!(cleared.allowed_ips, Some(None));
let updated = serde_json::from_value::<AdminUpdateUserApiKeyRequest>(json!({
"allowed_ips": ["203.0.113.10", "10.0.0.0/24"],
}))
.expect("present allowed_ips should deserialize");
assert_eq!(
updated.allowed_ips,
Some(Some(vec![
"203.0.113.10".to_string(),
"10.0.0.0/24".to_string(),
])),
);
}
}