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
@@ -36,7 +36,7 @@ func CreateSkillDefinition(c *gin.Context) {
ObjectKind: req.ObjectKind,
Source: req.Source,
ObjectEntryRoute: req.ObjectEntryRoute,
ExposedToUser: req.ExposedToUser,
ExposedToUser: exposedOrTrue(req.ExposedToUser),
StarterPromptsJSON: req.StarterPromptsJSON,
PromptTemplate: req.PromptTemplate,
InputSchemaJSON: req.InputSchemaJSON,
@@ -83,7 +83,11 @@ func UpdateSkillDefinition(c *gin.Context) {
item.ObjectKind = req.ObjectKind
item.Source = req.Source
item.ObjectEntryRoute = req.ObjectEntryRoute
item.ExposedToUser = req.ExposedToUser
// 没传就保持原值 —— 全量覆盖对其余字段成立,唯独这个不行:前端表单根本不发
// exposed_to_user,若无脑覆盖成零值 false,管理员改个标题就会把技能从用户目录里撤下来。
if req.ExposedToUser != nil {
item.ExposedToUser = *req.ExposedToUser
}
item.StarterPromptsJSON = req.StarterPromptsJSON
item.PromptTemplate = req.PromptTemplate
item.InputSchemaJSON = req.InputSchemaJSON
@@ -1,13 +1,17 @@
package skillapi
type definitionReq struct {
Key string `json:"key"`
Label string `json:"label"`
Description string `json:"description"`
ObjectKind string `json:"object_kind"`
Source string `json:"source"`
ObjectEntryRoute string `json:"object_entry_route"`
ExposedToUser bool `json:"exposed_to_user"`
Key string `json:"key"`
Label string `json:"label"`
Description string `json:"description"`
ObjectKind string `json:"object_kind"`
Source string `json:"source"`
ObjectEntryRoute string `json:"object_entry_route"`
// 三态:nil = 客户端没传这个字段,非 nil = 客户端明确说了 true/false。
// 用 *bool 而不是 bool,是因为「没传」和「传 false」是两件事:
// 新建时没传按默认 true,更新时没传保持原值(前端表单不发这个字段)。
// 压成 bool 的话,false 身兼二职,二者都会被当成「显式要求隐藏」。
ExposedToUser *bool `json:"exposed_to_user"`
StarterPromptsJSON string `json:"starter_prompts_json"`
PromptTemplate string `json:"prompt_template"`
InputSchemaJSON string `json:"input_schema_json"`
@@ -70,3 +70,16 @@ func validateDefinitionReq(req *definitionReq) *web.AppError {
}
return nil
}
// exposedOrTrue 解析 exposed_to_user 的三态,供**新建**用:没传按 true。
//
// 这个默认值原先由库列默认值(default:true)兜着,但那条路会把显式传的 false
// 也一并吃掉,所以挪到这里 —— 默认值是接口契约,归这一层。
// 口径与种子数据、前端 `item.exposed_to_user !== false` 三处一致:不表态就是可见,
// 只有明确传 false 才隐藏。
func exposedOrTrue(v *bool) bool {
if v == nil {
return true
}
return *v
}
@@ -4,16 +4,20 @@ import "time"
// SkillDefinition 对外暴露的任务级能力定义。
type SkillDefinition 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"`
Description string `gorm:"type:text" json:"description"`
ObjectKind string `gorm:"column:object_kind;size:32;not null;default:skill;index" json:"object_kind"`
Source string `gorm:"size:32;not null;default:eai;index" json:"source"`
ObjectEntryRoute string `gorm:"column:object_entry_route;size:128;default:''" json:"object_entry_route"`
ExposedToUser bool `gorm:"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"`
Description string `gorm:"type:text" json:"description"`
ObjectKind string `gorm:"column:object_kind;size:32;not null;default:skill;index" json:"object_kind"`
Source string `gorm:"size:32;not null;default:eai;index" json:"source"`
ObjectEntryRoute string `gorm:"column:object_entry_route;size:128;default:''" json:"object_entry_route"`
// 这里**有意不写** `default:true`。带上它,GORM 在 Create 时会跳过布尔零值字段
// (false 被当成「没填」),改由库默认值 true 生效 —— 于是对象显式设的 false
// 被持久层改写成 true,创建接口永远设不出「不暴露给用户」的技能。
// 「没传默认 true」是接口契约,归 handler 管(见 admin_handlers.go),不归库管。
ExposedToUser bool `gorm:"not null;index" json:"exposed_to_user"`
StarterPromptsJSON string `gorm:"type:text" json:"starter_prompts_json"`
PromptTemplate string `gorm:"type:text" json:"prompt_template"`
InputSchemaJSON string `gorm:"type:text" json:"input_schema_json"`