{"record":{"id":"9293412bc5fbd565","repo":"flipped-aurora/gin-vue-admin","slug":"s-929341","errorCode":null,"errorMessage":"序列化自动代码注入信息 %s 失败","messagePattern":"序列化自动代码注入信息 (.+?) 失败","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_template.go","lineNumber":105,"sourceCode":"\tlayout, err := newAutoCodeTaskLayout(\n\t\tglobal.GVA_CONFIG.AutoCode.Root,\n\t\tglobal.GVA_CONFIG.AutoCode.Server,\n\t\tglobal.GVA_CONFIG.AutoCode.WebRoot(),\n\t)\n\tif err != nil {\n\t\treturn err\n\t}\n\tgenerated, templates, injections, err := s.generate(createCtx, info, autoPkg)\n\tif err != nil {\n\t\treturn err\n\t}\n\thistory := info.History()\n\thistory.Templates = templates\n\thistory.Injections = make(map[string]string, len(injections))\n\tfor key, value := range injections {\n\t\tbytes, marshalErr := json.Marshal(value)\n\t\tif marshalErr != nil {\n\t\t\treturn errors.Wrapf(marshalErr, \"序列化自动代码注入信息 %s 失败\", key)\n\t\t}\n\t\thistory.Injections[key] = string(bytes)\n\t}\n\tfiles := make(map[string][]byte, len(generated))\n\tfor target, builder := range generated {\n\t\tfiles[target] = []byte(builder.String())\n\t}\n\tfileTask, err := prepareAutoCodeFileTask(layout, files)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn commitAutoCodeFileTask(fileTask, publishPreparedAutoCodeFile, func() error {\n\t\treturn persistAutoCodeMetadata(createCtx, global.GVA_DB, info, autoPkg.Template, history)\n\t})\n}\n\n// Preview 预览自动化代码\nfunc (s *autoCodeTemplate) Preview(ctx context.Context, info request.AutoCode) (map[string]string, error) {","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_template.go#L87-L123","documentation":"During Create, injection configs (per-template AST injection metadata) are marshaled to JSON for the generation history record: json.Marshal(value) per key. A marshal failure is wrapped with '序列化自动代码注入信息 %s 失败', naming the injection key. It indicates the injection value contains a type json cannot serialize (e.g. func, channel, cyclic reference).","triggerScenarios":"A registered injection builder/template returns a value json.Marshal cannot encode — a custom or plugin-provided injection carrying unsupported types (chan, func, circular pointer structures) under that key.","commonSituations":"Custom/plugin-provided injection code passing structs with unexported cycles or func fields; generator version mismatch where the injection payload shape changed; third-party plugin injecting non-serializable config.","solutions":["Fix the injection value source so it returns JSON-serializable data (plain maps/structs, no func/chan/cycles)","Identify the offending key from the %s in the message and inspect its builder","Add json tags / export fields if unexported fields cause broken structures","If a plugin supplies it, update or patch that plugin's injection code","As a workaround, pre-serialize to a string or map[string]interface{} before registering the injection"],"exampleFix":"// before: struct with func field breaks json.Marshal\ninjections[\"api\"] = someStructWithFuncField\n// after\ntype apiInjection struct{ Path string `json:\"path\"` }\ninjections[\"api\"] = apiInjection{Path: \"/user/list\"}","handlingStrategy":"type-guard","validationCode":"// guard injections before Create\nfor key, v := range injections {\n    if _, err := json.Marshal(v); err != nil {\n        return fmt.Errorf(\"injection %q not serializable: %w\", key, err)\n    }\n}","typeGuard":"func isJSONSerializable(v interface{}) bool {\n    switch v.(type) {\n    case func(), chan interface{}:\n        return false\n    }\n    _, err := json.Marshal(v)\n    return err == nil\n}","tryCatchPattern":"err := autoCodeSvc.Create(ctx, info)\nif err != nil && strings.Contains(err.Error(), \"序列化自动代码注入信息\") {\n    // extract the key from the message, then fix that injection's payload shape\n}","preventionTips":["Keep injection payloads as maps or tagged structs without func/chan/cycles","Version-check plugins that register custom injections","Pre-marshal complex injections to string values yourself","Add a unit test marshaling all default injection keys"],"tags":["json","serialization","auto-code","code-generation"],"backgroundTag":"json-marshal-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}