You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从分隔文件聚合组时出现Build Rule需返回Map错误求助

问题排查与代码修正

报错原因分析

报错明确提示「Build Rule must return a Map」,核心问题是你的代码存在分支遗漏,当处理的对象类型既不是Connector.TYPE_ACCOUNT也不是Connector.TYPE_GROUP时,没有返回任何Map对象;另外代码还有几个编译/运行时隐患,一并解决:

具体问题与修复点

  1. 缺少默认返回分支
    必须保证所有代码路径都返回一个Map,否则Sailpoint会抛出规则异常。

  2. 变量重复定义
    循环内的String permission和外层的同名变量冲突,编译会失败,需要重命名内层变量。

  3. 未处理格式异常的权限字符串
    如果权限字符串中没有:,indexOf(":")会返回-1,调用substring(-1)会抛出IndexOutOfBoundsException,需要先判断是否存在:。

  4. 集合类未指定泛型
    未指定泛型的集合可能引发类型转换问题,建议补充泛型声明。

修正后的代码

import sailpoint.object.Schema;
import sailpoint.connector.Connector;
import sailpoint.connector.DelimitedFileConnector;
import sailpoint.object.Permission;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

// 确保所有分支都返回Map
if (schema.getObjectType().equals(Connector.TYPE_ACCOUNT)) {
    HashMap<String, Object> map = DelimitedFileConnector.defaultBuildMap(cols, record);
    return map;
} else if (schema.getObjectType().equals(Connector.TYPE_GROUP)) {
    HashMap<String, Object> map = DelimitedFileConnector.defaultBuildMap(cols, record);
    String permissionStr = (String) map.get("Permission Rights");
    
    if (permissionStr != null && permissionStr.contains("-")) {
        List<Permission> perms = new ArrayList<>();
        String[] permissions = permissionStr.split("-");
        for (String permEntry : permissions) {
            // 先检查是否包含":",避免索引越界
            if (!permEntry.contains(":")) {
                continue; // 跳过格式不正确的条目
            }
            String target = permEntry.substring(0, permEntry.indexOf(":"));
            String permCode = permEntry.substring(permEntry.indexOf(":"));      
            
            if (permCode.equals(":YY")) {
                Permission updatePerm = new Permission();
                updatePerm.setRights("update");
                updatePerm.setTarget(target);
                perms.add(updatePerm);
                
                Permission createPerm = new Permission();
                createPerm.setRights("create");
                createPerm.setTarget(target);
                perms.add(createPerm);
            } else { 
                Permission execPerm = new Permission();
                execPerm.setRights("execute");
                execPerm.setTarget(target);
                perms.add(execPerm); 
            }
        }
        map.put("directPermissions", perms);
    }
    return map;
} else {
    // 默认返回空Map,避免无返回值的情况
    return new HashMap<>();
}

关键修改说明

  • 把外层的permission变量重命名为permissionStr,避免和循环内的变量冲突;
  • 增加了对权限条目是否包含:的判断,跳过格式错误的条目;
  • 为集合类补充了泛型声明(HashMap<String, Object>、List<Permission>);
  • 新增了默认分支,返回空HashMap,确保所有代码路径都返回Map;
  • 把循环改成增强for循环,代码更简洁;
  • 把compareTo改成equals,字符串相等判断更直接。

内容的提问来源于stack exchange,提问作者Shiva shankar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:07:43