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:
eaiadmin
2026-09-19 02:17:19 +08:00
co-authored by Claude Code
parent c5af5084b1
commit 9157df40f3
8 changed files with 121 additions and 58 deletions
@@ -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) {
@@ -5,30 +5,33 @@ import "time"
// XAppDefinition 面向用户的一键应用目录定义。
// 它是“成品入口”这一层:决定应用怎么展示、默认挂哪个专员/技能、打开后走哪条路。
type XAppDefinition struct {
ID uint `gorm:"primaryKey" json:"id"`
Key string `gorm:"size:64;uniqueIndex;not null" json:"key"`
Label string `gorm:"size:128;not null" json:"label"`
DisplayCode string `gorm:"column:display_code;size:16;default:'';index" json:"display_code"`
EAILogicCode string `gorm:"column:eailogic_code;size:32;default:'';index" json:"eailogic_code"`
Badge string `gorm:"size:32;default:''" json:"badge"`
Kind string `gorm:"size:64;default:''" json:"kind"`
MarketTag string `gorm:"column:market_tag;size:32;default:'';index" json:"market_tag"`
XAppMode string `gorm:"size:16;default:'';index" json:"xapp_mode"`
Tier string `gorm:"size:16;default:'';index" json:"tier"`
Source string `gorm:"size:32;not null;default:eai;index" json:"source"` // eai / custom / imported
Color string `gorm:"size:32;default:''" json:"color"`
IconText string `gorm:"column:icon_text;size:16;default:''" json:"icon_text"`
CoverTone string `gorm:"column:cover_tone;type:text" json:"cover_tone"`
Summary string `gorm:"type:text" json:"summary"`
Description string `gorm:"type:text" json:"description"`
OpenRoute string `gorm:"column:open_route;size:256;default:''" json:"open_route"`
SpecialistKey string `gorm:"column:specialist_key;size:64;default:'';index" json:"specialist_key"`
SkillKey string `gorm:"column:skill_key;size:64;default:'';index" json:"skill_key"`
DefaultPrompt string `gorm:"column:default_prompt;type:text" json:"default_prompt"`
PromptsJSON string `gorm:"column:prompts_json;type:text" json:"prompts_json"`
TagsJSON string `gorm:"column:tags_json;type:text" json:"tags_json"`
InstallState string `gorm:"column:install_state;size:16;not null;default:installed;index" json:"install_state"`
ExposedToUser bool `gorm:"column:exposed_to_user;not null;default:true;index" json:"exposed_to_user"`
ID uint `gorm:"primaryKey" json:"id"`
Key string `gorm:"size:64;uniqueIndex;not null" json:"key"`
Label string `gorm:"size:128;not null" json:"label"`
DisplayCode string `gorm:"column:display_code;size:16;default:'';index" json:"display_code"`
EAILogicCode string `gorm:"column:eailogic_code;size:32;default:'';index" json:"eailogic_code"`
Badge string `gorm:"size:32;default:''" json:"badge"`
Kind string `gorm:"size:64;default:''" json:"kind"`
MarketTag string `gorm:"column:market_tag;size:32;default:'';index" json:"market_tag"`
XAppMode string `gorm:"size:16;default:'';index" json:"xapp_mode"`
Tier string `gorm:"size:16;default:'';index" json:"tier"`
Source string `gorm:"size:32;not null;default:eai;index" json:"source"` // eai / custom / imported
Color string `gorm:"size:32;default:''" json:"color"`
IconText string `gorm:"column:icon_text;size:16;default:''" json:"icon_text"`
CoverTone string `gorm:"column:cover_tone;type:text" json:"cover_tone"`
Summary string `gorm:"type:text" json:"summary"`
Description string `gorm:"type:text" json:"description"`
OpenRoute string `gorm:"column:open_route;size:256;default:''" json:"open_route"`
SpecialistKey string `gorm:"column:specialist_key;size:64;default:'';index" json:"specialist_key"`
SkillKey string `gorm:"column:skill_key;size:64;default:'';index" json:"skill_key"`
DefaultPrompt string `gorm:"column:default_prompt;type:text" json:"default_prompt"`
PromptsJSON string `gorm:"column:prompts_json;type:text" json:"prompts_json"`
TagsJSON string `gorm:"column:tags_json;type:text" json:"tags_json"`
InstallState string `gorm:"column:install_state;size:16;not null;default:installed;index" json:"install_state"`
// 有意不写 `default:true`,理由同 skill_definition.ExposedToUser:
// 带 default 标签会让 GORM 在 Create 时跳过零值 false,由库默认值接管,
// 对象说了不算。「没传默认 true」由 handler 兜(见 xapps/api/handlers.go)。
ExposedToUser bool `gorm:"column:exposed_to_user;not null;index" json:"exposed_to_user"`
State string `gorm:"size:16;not null;default:active;index" json:"state"` // active / inactive
SortOrder int `gorm:"not null;default:0;index" json:"sort_order"`
CreatedAt time.Time `json:"created_at"`