apolloconfig/apollo · error · BadRequestException

Params(NamespaceName) can not be empty.

Error message

Params(NamespaceName) can not be empty.

What it means

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'.

Source

Thrown at apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/PortalManagementController.java:397

  @Override
  @PreAuthorize(value = "@unifiedPermissionValidator.isSuperAdmin()")
  public ResponseEntity<List<Object>> assignRoleToConsumer(String token, String type, Object body,
      String envs) {
    requirePortalUserRequest();
    NamespaceDTO namespace = convertBody(body, NamespaceDTO.class);
    List<ConsumerRole> consumerRoles = new ArrayList<>(8);
    String appId = namespace.getAppId();
    String namespaceName = namespace.getNamespaceName();

    if (StringUtils.isEmpty(appId)) {
      throw new BadRequestException("Params(AppId) can not be empty.");
    }
    if (Objects.equals("AppRole", type)) {
      return ResponseEntity.ok(asObjects(Collections
          .singletonList(consumerService.assignAppRoleToConsumer(token, appId, currentUserId()))));
    }
    if (StringUtils.isEmpty(namespaceName)) {
      throw new BadRequestException("Params(NamespaceName) can not be empty.");
    }
    if (envs != null) {
      for (String env : envs.split(",")) {
        if (StringUtils.isEmpty(env)) {
          continue;
        }
        parseEnv(env);
        consumerRoles.addAll(consumerService.assignNamespaceRoleToConsumer(token, appId,
            namespaceName, env, currentUserId()));
      }
      return ResponseEntity.ok(asObjects(consumerRoles));
    }

    consumerRoles.addAll(consumerService.assignNamespaceRoleToConsumer(token, appId, namespaceName,
        currentUserId()));
    return ResponseEntity.ok(asObjects(consumerRoles));
  }

View on GitHub (pinned to d95fc18d11)

Solutions

  1. Include a non-empty namespaceName in the request body, e.g. {"appId":"myApp","namespaceName":"application"}.
  2. If you intended app-level role assignment, set the type parameter to 'AppRole' instead.
  3. Verify the body is valid JSON and maps to NamespaceDTO with both fields populated.

Example fix

// before
type=NamespaceRole
{"appId":"myApp"}

// after
type=NamespaceRole
{"appId":"myApp","namespaceName":"application"}
Defensive patterns

Strategy: validation

Validate before calling

// For namespace-role assignment, validate namespaceName before sending
if (!"AppRole".equals(type) && StringUtils.isEmpty(namespaceName)) {
  throw new IllegalArgumentException("namespaceName required for type=" + type);
}

Type guard

function hasValidNamespaceForType(type: string, body: unknown): boolean {
  if (type === 'AppRole') return true;
  return typeof body === 'object' && body !== null
    && typeof (body as any).namespaceName === 'string'
    && (body as any).namespaceName.trim().length > 0;
}

Prevention

When it happens

Trigger: 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.

Common situations: The caller set type to a namespace role but forgot to include namespaceName in the body; or the namespaceName was stripped during JSON serialization.

Related errors


AI-assisted analysis of apolloconfig/apollo@d95fc18d11 (2026-08-14). Data as JSON: /api/errors/86ba03e1279fa66b. Report an issue: GitHub.