{"record":{"id":"4e68428661e99370","repo":"apolloconfig/apollo","slug":"params-namespacename-can-not-be-empty-4e6842","errorCode":null,"errorMessage":"Params(NamespaceName) can not be empty.","messagePattern":"Params\\(NamespaceName\\) can not be empty\\.","errorType":"validation","errorClass":"BadRequestException","httpStatus":400,"severity":"warning","filePath":"apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConsumerController.java","lineNumber":149,"sourceCode":"  @PreAuthorize(value = \"@unifiedPermissionValidator.isSuperAdmin()\")\n  @PostMapping(value = \"/consumers/{token}/assign-role\")\n  public List<ConsumerRole> assignNamespaceRoleToConsumer(@PathVariable String token,\n      @RequestParam String type, @RequestParam(required = false) String envs,\n      @RequestBody NamespaceDTO namespace) {\n    List<ConsumerRole> consumerRoleList = new ArrayList<>(8);\n\n    String appId = namespace.getAppId();\n    String namespaceName = namespace.getNamespaceName();\n\n    if (StringUtils.isEmpty(appId)) {\n      throw new BadRequestException(\"Params(AppId) can not be empty.\");\n    }\n    if (Objects.equals(\"AppRole\", type)) {\n      return Collections.singletonList(consumerService.assignAppRoleToConsumer(token, appId,\n          userInfoHolder.getUser().getUserId()));\n    }\n    if (StringUtils.isEmpty(namespaceName)) {\n      throw new BadRequestException(\"Params(NamespaceName) can not be empty.\");\n    }\n    if (null != envs) {\n      String[] envArray = envs.split(\",\");\n      List<String> envList = Lists.newArrayList();\n      // validate env parameter\n      for (String env : envArray) {\n        if (Strings.isNullOrEmpty(env)) {\n          continue;\n        }\n        if (Env.UNKNOWN.equals(Env.transformEnv(env))) {\n          throw BadRequestException.invalidEnvFormat(env);\n        }\n        envList.add(env);\n      }\n\n      List<ConsumerRole> consumeRoles = new ArrayList<>();\n      for (String env : envList) {\n        consumeRoles.addAll(consumerService.assignNamespaceRoleToConsumer(token, appId,","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/apolloconfig/apollo/blob/d95fc18d112589efc09ddcbe1507047584d55251/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConsumerController.java#L131-L167","documentation":"BadRequestException (HTTP 400) from the same assign-role handler, reached only on the namespace-role branch (type != 'AppRole'). After appId is confirmed non-empty and the AppRole branch is skipped, it requires namespaceName on the NamespaceDTO body to be non-empty via StringUtils.isEmpty.","triggerScenarios":"POST /consumers/{token}/assign-role?type=<not AppRole> with a body whose namespaceName is null/empty, or when envs selects namespace-scoped roles but the namespace is unnamed.","commonSituations":"Assigning env-specific namespace roles but forgetting the namespace name; client defaulting type to 'Namespace' without the name; misusing the AppRole shortcut without setting type='AppRole'.","solutions":["Set namespace.setNamespaceName(<namespace>) in the body.","If you actually want the app-level role, send type='AppRole' (the appId-only path).","Validate that namespaceName is non-empty for the namespace branch (see validationCode).","When envs are provided, ensure a namespaceName is also present per env."],"exampleFix":"// before\nPOST /consumers/{token}/assign-role?type=NAMESPACE\nbody: { \"appId\": \"sample-app\" }   // namespaceName missing -> 400\n\n// after\nbody: { \"appId\": \"sample-app\", \"namespaceName\": \"application\" }","handlingStrategy":"validation","validationCode":"// For the namespace-role branch (type != AppRole), namespaceName is required.\nif (!\"AppRole\".equals(type)) {\n  String ns = namespaceDto.getNamespaceName();\n  if (ns == null || ns.trim().isEmpty()) {\n    throw new IllegalArgumentException(\"namespaceName is required for namespace roles\");\n  }\n}","typeGuard":"static boolean hasNamespaceName(NamespaceDTO dto) {\n  return dto != null && dto.getNamespaceName() != null\n      && !dto.getNamespaceName().trim().isEmpty();\n}","tryCatchPattern":null,"preventionTips":["For app-level roles, send type='AppRole' to skip the namespaceName requirement.","For namespace/env roles, always set namespaceName.","Validate with the type guard before submit.","Make the UI require namespaceName unless AppRole is selected."],"tags":["consumer","validation","role","namespace","bad-request"],"backgroundTag":null,"analyzedSha":"d95fc18d112589efc09ddcbe1507047584d55251","analyzedAt":"2026-08-14T04:00:05.477Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}