flipped-aurora/gin-vue-admin · error

无法解析API项 %q,期望 "/path" 或 "METHOD /path"

Error message

无法解析API项 %q,期望 "/path" 或 "METHOD /path"

What it means

The role-API batch assigner parses each API item string expecting either a bare path `/path` or a `METHOD /path` pair. A string that splits into more than two whitespace-separated fields cannot be interpreted, so parsing aborts with this error naming the offending item.

Source

Thrown at server/mcp/role_api_batch_assigner.go:209

		if _, ok := seen[key]; ok {
			continue
		}
		seen[key] = struct{}{}
		deduped = append(deduped, item)
	}
	return deduped, nil
}

// parseAPIItemString 解析"METHOD /path"或"/path"形式的单条API,方法缺省为POST
func parseAPIItemString(s string) (path, method string, err error) {
	fields := strings.Fields(strings.TrimSpace(s))
	switch len(fields) {
	case 1:
		return fields[0], "", nil
	case 2:
		return fields[1], fields[0], nil
	default:
		return "", "", fmt.Errorf("无法解析API项 %q,期望 \"/path\" 或 \"METHOD /path\"", s)
	}
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Remove extra tokens so each item is exactly `/path` or `METHOD /path` (e.g. `POST /api/user`)
  2. Check for full-width or invisible whitespace characters and replace them with nothing or a single ASCII space
  3. Strip any comments or annotations from each item string
  4. If method and path are supplied separately, use only the `/path` form and set the method elsewhere

Example fix

// before
items := []string{"POST /api/user comment", "/api/user"}
// after
items := []string{"POST /api/user", "/api/user"}
Defensive patterns

Strategy: validation

Validate before calling

func validAPIItem(s string) bool {
    fields := strings.Fields(s)
    if len(fields) == 1 {
        return strings.HasPrefix(fields[0], "/")
    }
    if len(fields) == 2 {
        return isHTTPMethod(fields[0]) && strings.HasPrefix(fields[1], "/")
    }
    return false
}

Type guard

func normalizeAPIItem(s string) (string, error) {
    s = strings.Join(strings.Fields(s), " ")
    if !validAPIItem(s) {
        return "", fmt.Errorf("invalid API item %q", s)
    }
    return s, nil
}

Prevention

When it happens

Trigger: Passing an item like `POST /api/user extra` or an item containing accidental spaces/tabs/newlines to the APIs list of the batch assign tool.

Common situations: Copy-pasting a curl command line into the API item field; a stray comma converted to space; trailing comment after the path; Chinese full-width space mistaken as separator.

Related errors


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