flipped-aurora/gin-vue-admin · error

apis 数组元素缺少 path 字段

Error message

apis 数组元素缺少 path 字段

What it means

parseAPIItems iterates the 'apis' array; each map[string]any element must carry a non-empty 'path' string. The error is thrown when an element is an object but its 'path' is missing, not a string, or blank after trimming.

Source

Thrown at server/mcp/role_api_batch_assigner.go:144

// 批内按 归一化path+method 去重
func parseAPIItems(v any) ([]systemReq.CasbinInfo, error) {
	if v == nil {
		return nil, nil
	}
	var items []systemReq.CasbinInfo

	appendItem := func(path, method string) {
		items = append(items, systemReq.CasbinInfo{Path: path, Method: method})
	}

	switch value := v.(type) {
	case []any:
		for _, entry := range value {
			switch typed := entry.(type) {
			case map[string]any:
				path, _ := typed["path"].(string)
				if strings.TrimSpace(path) == "" {
					return nil, errors.New("apis 数组元素缺少 path 字段")
				}
				method, _ := typed["method"].(string)
				appendItem(path, method)
			case string:
				path, method, err := parseAPIItemString(typed)
				if err != nil {
					return nil, err
				}
				appendItem(path, method)
			default:
				return nil, errors.New("apis 数组元素需为 {path,method} 对象或 \"METHOD /path\" 字符串")
			}
		}
	case string:
		trimmed := strings.TrimSpace(value)
		if trimmed == "" {
			return nil, nil
		}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Add a non-empty string 'path' field to each object element
  2. If the item is free-form text, format it as "METHOD /path" string instead of an object
  3. Validate each item client-side for a non-empty string path before calling

Example fix

// before
args["apis"] = []any{map[string]any{"method": "GET"}}
// after
args["apis"] = []any{map[string]any{"path": "/api/user/list", "method": "GET"}}
Defensive patterns

Strategy: validation

Validate before calling

for (const item of apis) {
  if (typeof item === "object" && item !== null && !Array.isArray(item)) {
    if (typeof item.path !== "string" || item.path.trim() === "") {
      throw new Error("每个对象元素必须包含非空 path 字段")
    }
  }
}

Type guard

function isApiItem(v: unknown): v is { path: string; method?: string } {
  return typeof v === "object" && v !== null && !Array.isArray(v)
    && typeof (v as any).path === "string" && (v as any).path.trim() !== ""
}

Try / catch

try {
  await callTool("role_api_batch_assigner", { apis })
} catch (e) {
  if (String(e.message).includes("缺少 path 字段")) {
    // 修正该批次的违规项
  }
  throw e
}

Prevention

When it happens

Trigger: An apis element like {"method":"GET"} (no path), {"path":123}, or {"path":" "}.

Common situations: Hand-written JSON payload with a renamed path field; client sending method only; pasting numeric IDs instead of route paths; mixed batches where some items conform and others do not.

Related errors


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