fix(upstreams): upsert base_raw 改 Option,空串保留原值 (1.0.25)
`upsert` 其他字段(models/auth/note)语义都是 None / Some("") = 保留原值,
Some(有效) = 校验覆盖。`base_raw` 是 `&str`,"" 被 `validate_base`
直接拦截报「缺少 base 参数」。
现场踩坑:用户改 minimax 槽位 resets 时点了「停用」按钮,前端只
发 `{slot: minimax, enabled: 0}`(其他字段没填),被 base 校验拦下。
修:`base_raw` 改 `Option<&String>`,与现有约定对齐。新建必填校验
移进闭包(existing.base 兜底),编辑「停用」/「改备注」/「改账号」
等不需要回填 base 的场景不再被拦。
回归 228/0 全绿。
This commit is contained in:
1 parent
2c37a36d9f
commit
bbe54f109b
2 files changed
+25
-4
No files matched your search
@@ -112,7 +112,7 @@ fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||||
|
||||
match upstreams::upsert(
|
||||
slot_raw,
|
||||
p.get("base").map(String::as_str).unwrap_or(""),
|
||||
p.get("base"),
|
||||
models,
|
||||
p.get("auth"),
|
||||
accounts,
|
||||
|
||||
@@ -686,7 +686,7 @@ pub fn models_flat(allowed: &[String]) -> Vec<(String, String)> {
|
||||
/// 新增或覆盖一个上游。
|
||||
///
|
||||
/// 参数语义(兼容旧调用方,同时支持多账号):
|
||||
/// * `models = None` / `auth_raw = None` / `note = None` 都表示**保持原值**;
|
||||
/// * `models = None` / `auth_raw = None` / `note = None` / `base_raw = None` 都表示**保持原值**;
|
||||
/// `Some("")` 表示显式清空。
|
||||
/// * `accounts = Some(..)` 表示用多账号列表**整体替换**(此时顶层 `auth` 不再生效,
|
||||
/// 写入时被清为 `""`,避免 `auth` 与 `accounts[0]` 重复);`None` 表示沿用旧逻辑
|
||||
@@ -696,7 +696,7 @@ pub fn models_flat(allowed: &[String]) -> Vec<(String, String)> {
|
||||
/// `enabled = false` 会立刻从共享内存摘除。
|
||||
pub fn upsert(
|
||||
slot_raw: &str,
|
||||
base_raw: &str,
|
||||
base_raw: Option<&String>,
|
||||
models: Option<Vec<String>>,
|
||||
auth_raw: Option<&String>,
|
||||
accounts: Option<Vec<UpstreamAccount>>,
|
||||
@@ -706,7 +706,6 @@ pub fn upsert(
|
||||
note: Option<&String>,
|
||||
) -> Result<Upstream, String> {
|
||||
let slot = validate_slot(slot_raw)?;
|
||||
let base = validate_base(base_raw)?;
|
||||
let models = match models {
|
||||
Some(list) => Some(check_models(&list)?),
|
||||
None => None,
|
||||
@@ -717,6 +716,28 @@ pub fn upsert(
|
||||
// 先克隆出来:下面 insert 需要可变借用,不能与这里的不可变借用并存
|
||||
let existing = table.get(&key).cloned();
|
||||
|
||||
// base:None / `Some("")` = 保留原值;`Some(有效)` = 校验并覆盖。
|
||||
// 1.0.24 修:之前 base_raw 是 `&str`,管理台「停用」按钮(前端不传 base)
|
||||
// 会被 `validate_base("")` 拦截。改成 Option + 保留原值后,
|
||||
// 1) 新建时 base_raw 必传 → 否则报「缺少 base」;
|
||||
// 2) 编辑时只传 enabled=false 也能停用(base 沿用原值)。
|
||||
let base = match base_raw {
|
||||
Some(b) => {
|
||||
let b = util::trim(b);
|
||||
if b.is_empty() {
|
||||
existing.as_ref().map(|e| e.base.clone()).unwrap_or_default()
|
||||
} else {
|
||||
validate_base(&b)?
|
||||
}
|
||||
}
|
||||
None => existing.as_ref().map(|e| e.base.clone()).unwrap_or_default(),
|
||||
};
|
||||
if base.is_empty() {
|
||||
// 新建时(existing 为 None)必须 base 非空——上面已经保留了 existing.base,
|
||||
// 这里只在「None + 无 existing」时报错(`base_raw = None` 是异常调用)
|
||||
return Err("缺少 base 参数".to_owned());
|
||||
}
|
||||
|
||||
// 多账号:整体替换(清掉顶层 auth,避免重复);否则走 legacy 单 auth
|
||||
let (accounts, legacy_auth) = match accounts {
|
||||
Some(list) => {
|
||||
|
||||
Reference in new issue
Block a user