{"record":{"id":"86ba03e1279fa66b","repo":"apolloconfig/apollo","slug":"params-namespacename-can-not-be-empty","errorCode":null,"errorMessage":"Params(NamespaceName) can not be empty.","messagePattern":"Params\\(NamespaceName\\) can not be empty\\.","errorType":"validation","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/PortalManagementController.java","lineNumber":397,"sourceCode":"  @Override\n  @PreAuthorize(value = \"@unifiedPermissionValidator.isSuperAdmin()\")\n  public ResponseEntity<List<Object>> assignRoleToConsumer(String token, String type, Object body,\n      String envs) {\n    requirePortalUserRequest();\n    NamespaceDTO namespace = convertBody(body, NamespaceDTO.class);\n    List<ConsumerRole> consumerRoles = new ArrayList<>(8);\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 ResponseEntity.ok(asObjects(Collections\n          .singletonList(consumerService.assignAppRoleToConsumer(token, appId, currentUserId()))));\n    }\n    if (StringUtils.isEmpty(namespaceName)) {\n      throw new BadRequestException(\"Params(NamespaceName) can not be empty.\");\n    }\n    if (envs != null) {\n      for (String env : envs.split(\",\")) {\n        if (StringUtils.isEmpty(env)) {\n          continue;\n        }\n        parseEnv(env);\n        consumerRoles.addAll(consumerService.assignNamespaceRoleToConsumer(token, appId,\n            namespaceName, env, currentUserId()));\n      }\n      return ResponseEntity.ok(asObjects(consumerRoles));\n    }\n\n    consumerRoles.addAll(consumerService.assignNamespaceRoleToConsumer(token, appId, namespaceName,\n        currentUserId()));\n    return ResponseEntity.ok(asObjects(consumerRoles));\n  }\n","sourceCodeStart":379,"sourceCodeEnd":415,"githubUrl":"https://github.com/apolloconfig/apollo/blob/d95fc18d112589efc09ddcbe1507047584d55251/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/PortalManagementController.java#L379-L415","documentation":"Thrown in assignRoleToConsumer when the request type is not 'AppRole' (meaning a namespace-level role is requested) but the NamespaceDTO body has a null or empty namespaceName. For namespace-role assignment Apollo needs both appId and namespaceName. This check is reached only after appId is validated and the type is confirmed not to be 'AppRole'.","triggerScenarios":"A POST to assignRoleToConsumer with type set to 'NamespaceRole' (or any value other than 'AppRole') where the body's namespaceName field is missing or empty.","commonSituations":"The caller set type to a namespace role but forgot to include namespaceName in the body; or the namespaceName was stripped during JSON serialization.","solutions":["Include a non-empty namespaceName in the request body, e.g. {\"appId\":\"myApp\",\"namespaceName\":\"application\"}.","If you intended app-level role assignment, set the type parameter to 'AppRole' instead.","Verify the body is valid JSON and maps to NamespaceDTO with both fields populated."],"exampleFix":"// before\ntype=NamespaceRole\n{\"appId\":\"myApp\"}\n\n// after\ntype=NamespaceRole\n{\"appId\":\"myApp\",\"namespaceName\":\"application\"}","handlingStrategy":"validation","validationCode":"// For namespace-role assignment, validate namespaceName before sending\nif (!\"AppRole\".equals(type) && StringUtils.isEmpty(namespaceName)) {\n  throw new IllegalArgumentException(\"namespaceName required for type=\" + type);\n}","typeGuard":"function hasValidNamespaceForType(type: string, body: unknown): boolean {\n  if (type === 'AppRole') return true;\n  return typeof body === 'object' && body !== null\n    && typeof (body as any).namespaceName === 'string'\n    && (body as any).namespaceName.trim().length > 0;\n}","tryCatchPattern":null,"preventionTips":["When type is NamespaceRole, always include namespaceName in the body.","Build request bodies using a factory that enforces required fields per type.","Add client-side validation that mirrors the server's field requirements."],"tags":["validation","openapi","bad-request","consumer","namespace","apollo-portal"],"backgroundTag":null,"analyzedSha":"d95fc18d112589efc09ddcbe1507047584d55251","analyzedAt":"2026-08-14T04:00:05.477Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}