fix: 布尔字段的「没传」与「传 false」被库默认值合并
三处同一形状的缺陷:模型的 bool 字段带 `gorm:"default:true"`,而 GORM 在
Create 时会跳过「带 default 标签的零值字段」,于是 Go 里的 false 被当成「没填」,
改由库默认值 true 生效。对象显式设的 false 被持久层改写成 true。
- skill_definition.exposed_to_user
- xapp_definition.exposed_to_user
- position_knowledge.is_mandatory
前两处有两个方向的病症:
1. 创建接口设不出 false —— 管理员建一个「不暴露给用户」的技能/应用,
接口返回 200、看着成功,库里存的是 true。
2. **更新接口会静默撤下** —— 前端 admin 表单根本不发 exposed_to_user
(只读不写,见 skillCatalog.js / xappCatalog.js),而 PUT 对每个字段
无条件覆盖,于是管理员改个标题就把技能/应用从用户目录里摘了。
这条是线上正在发生的,比第一条更狠。
position_knowledge 同样:前端的必学/选学 el-switch 怎么拨都存成必学。
修法是拿掉那层「持久层替对象拿主意」:
- 模型去掉 `default:true`,对象说了算,仓库原样落库;
- 请求 DTO 改 *bool,把「没传」和「传 false」分开 —— 三态只存在于线上,
`not null` 的列没有「未设置」态,所以不进模型;
- 默认值归接口契约,放 handler:新建时没传按 true(与种子数据、前端
`exposed_to_user !== false` 口径一致),更新时没传保持原值
(前端不发这个字段,不能因为一次无关编辑就改变可见性)。
没选的两个方案:给 Create 加 Select("*") 是把 GORM 的零值规则泄漏进 HTTP
适配层,只盖住症状还得每处创建路径都记得;模型改 *bool 则是给一个
不存在的领域状态造了个位置,还让 JSON 多出 null。
验证(tmp_vfy_fix,临时程序,验完已删):真实路由 + 真实 HTTP,跑在数据库
副本上,39 条断言全绿。同一份断言在修复前跑出 6 条红(S1/X1/P2:显式 false
存成 true;S9/X9:更新不带该字段把 true 静默改成 false),确认断言确实在测
这个修复而不是装饰。
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -48,7 +48,9 @@ type definitionReq struct {
|
||||
PromptsJSON string `json:"prompts_json"`
|
||||
TagsJSON string `json:"tags_json"`
|
||||
InstallState string `json:"install_state"`
|
||||
ExposedToUser bool `json:"exposed_to_user"`
|
||||
// 三态:nil = 客户端没传,非 nil = 明确说了 true/false。理由同技能侧
|
||||
// skillapi.definitionReq.ExposedToUser:没传与传 false 是两件事。
|
||||
ExposedToUser *bool `json:"exposed_to_user"`
|
||||
State string `json:"state"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
@@ -89,6 +91,17 @@ func normalizeXAppMode(req *definitionReq) {
|
||||
}
|
||||
}
|
||||
|
||||
// exposedOrTrue 解析 exposed_to_user 的三态,供**新建**用:没传按 true。
|
||||
// 这个默认值原先由库列默认值兜着,但那条路会把显式传的 false 一并吃掉(GORM 的
|
||||
// default 标签会跳过布尔零值字段),所以挪到这一层 —— 默认值是接口契约。
|
||||
// 口径与种子数据、前端 `item.exposed_to_user !== false` 一致。
|
||||
func exposedOrTrue(v *bool) bool {
|
||||
if v == nil {
|
||||
return true
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func validateDefinitionReq(req *definitionReq) *web.AppError {
|
||||
normalizeDefinitionReq(req)
|
||||
normalizeXAppMode(req)
|
||||
@@ -183,7 +196,7 @@ func CreateXAppDefinition(c *gin.Context) {
|
||||
PromptsJSON: req.PromptsJSON,
|
||||
TagsJSON: req.TagsJSON,
|
||||
InstallState: req.InstallState,
|
||||
ExposedToUser: req.ExposedToUser,
|
||||
ExposedToUser: exposedOrTrue(req.ExposedToUser),
|
||||
State: req.State,
|
||||
SortOrder: req.SortOrder,
|
||||
}
|
||||
@@ -233,7 +246,11 @@ func UpdateXAppDefinition(c *gin.Context) {
|
||||
item.PromptsJSON = req.PromptsJSON
|
||||
item.TagsJSON = req.TagsJSON
|
||||
item.InstallState = req.InstallState
|
||||
item.ExposedToUser = req.ExposedToUser
|
||||
// 没传就保持原值,理由同技能侧 admin_handlers.go:前端不发这个字段,
|
||||
// 全覆盖会把管理员的一次普通编辑变成「从用户目录撤下」。
|
||||
if req.ExposedToUser != nil {
|
||||
item.ExposedToUser = *req.ExposedToUser
|
||||
}
|
||||
item.State = req.State
|
||||
item.SortOrder = req.SortOrder
|
||||
if !xAppDefinitionRepo.Update(&item) {
|
||||
|
||||
Reference in New Issue
Block a user