flipped-aurora/gin-vue-admin · error

序列化自动代码注入信息 %s 失败

Error message

序列化自动代码注入信息 %s 失败

What it means

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).

Source

Thrown at server/service/system/auto_code_template.go:105

	layout, err := newAutoCodeTaskLayout(
		global.GVA_CONFIG.AutoCode.Root,
		global.GVA_CONFIG.AutoCode.Server,
		global.GVA_CONFIG.AutoCode.WebRoot(),
	)
	if err != nil {
		return err
	}
	generated, templates, injections, err := s.generate(createCtx, info, autoPkg)
	if err != nil {
		return err
	}
	history := info.History()
	history.Templates = templates
	history.Injections = make(map[string]string, len(injections))
	for key, value := range injections {
		bytes, marshalErr := json.Marshal(value)
		if marshalErr != nil {
			return errors.Wrapf(marshalErr, "序列化自动代码注入信息 %s 失败", key)
		}
		history.Injections[key] = string(bytes)
	}
	files := make(map[string][]byte, len(generated))
	for target, builder := range generated {
		files[target] = []byte(builder.String())
	}
	fileTask, err := prepareAutoCodeFileTask(layout, files)
	if err != nil {
		return err
	}
	return commitAutoCodeFileTask(fileTask, publishPreparedAutoCodeFile, func() error {
		return persistAutoCodeMetadata(createCtx, global.GVA_DB, info, autoPkg.Template, history)
	})
}

// Preview 预览自动化代码
func (s *autoCodeTemplate) Preview(ctx context.Context, info request.AutoCode) (map[string]string, error) {

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Fix the injection value source so it returns JSON-serializable data (plain maps/structs, no func/chan/cycles)
  2. Identify the offending key from the %s in the message and inspect its builder
  3. Add json tags / export fields if unexported fields cause broken structures
  4. If a plugin supplies it, update or patch that plugin's injection code
  5. As a workaround, pre-serialize to a string or map[string]interface{} before registering the injection

Example fix

// before: struct with func field breaks json.Marshal
injections["api"] = someStructWithFuncField
// after
type apiInjection struct{ Path string `json:"path"` }
injections["api"] = apiInjection{Path: "/user/list"}
Defensive patterns

Strategy: type-guard

Validate before calling

// guard injections before Create
for key, v := range injections {
    if _, err := json.Marshal(v); err != nil {
        return fmt.Errorf("injection %q not serializable: %w", key, err)
    }
}

Type guard

func isJSONSerializable(v interface{}) bool {
    switch v.(type) {
    case func(), chan interface{}:
        return false
    }
    _, err := json.Marshal(v)
    return err == nil
}

Try / catch

err := autoCodeSvc.Create(ctx, info)
if err != nil && strings.Contains(err.Error(), "序列化自动代码注入信息") {
    // extract the key from the message, then fix that injection's payload shape
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/9293412bc5fbd565. Report an issue: GitHub.