apolloconfig/apollo · error · BadRequestException

Token namespace scope can not be null

Error message

Token namespace scope can not be null

What it means

Thrown by UserTokenService.validateCreateRequest() when the request's namespaces list is non-null but contains a null element. The check iterates every UserTokenNamespaceScope in request.getNamespaces(); if any single entry is null, this fires. A null namespaces list itself is allowed (returns early). BadRequestException → HTTP 400.

Source

Thrown at apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/UserTokenService.java:369

  }

  private Set<String> emptyToNull(Set<String> values) {
    return values == null || values.isEmpty() ? null : values;
  }

  private void validateCreateRequest(UserTokenCreateRequest request) {
    if (request == null || StringUtils.isBlank(request.getName())) {
      throw new BadRequestException("Token name can not be blank");
    }
    if (request.getRateLimit() != null && request.getRateLimit() < 0) {
      throw BadRequestException.rateLimitIsInvalid();
    }
    if (request.getNamespaces() == null) {
      return;
    }
    for (UserTokenNamespaceScope namespaceScope : request.getNamespaces()) {
      if (namespaceScope == null) {
        throw new BadRequestException("Token namespace scope can not be null");
      }
    }
  }

  private void validateUserEnabled(String userId) {
    UserInfo userInfo = userService.findByUserId(userId);
    if (userInfo == null) {
      throw BadRequestException.userNotExists(userId);
    }
    if (userInfo.getEnabled() != USER_ENABLED) {
      throw new BadRequestException("User is disabled");
    }
  }

  private Date resolveExpires(Date requestedExpires, Date now) {
    Date expires = requestedExpires;
    if (expires == null) {
      Calendar calendar = Calendar.getInstance();

View on GitHub (pinned to d95fc18d11)

Solutions

  1. Filter out null entries from the namespaces list before setting it on the request.
  2. Ensure the client never sends null objects in the namespaces JSON array.
  3. Validate each namespace scope is non-null before adding to the list.

Example fix

// before
request.setNamespaces(Arrays.asList(scope1, null, scope3));
// after
request.setNamespaces(Arrays.asList(scope1, scope3));
Defensive patterns

Strategy: validation

Validate before calling

if (request.getNamespaces() != null) {
    request.getNamespaces().removeIf(Objects::isNull);
}
userTokenService.createToken(request, operator);

Type guard

static boolean hasNoNullNamespaceScopes(UserTokenCreateRequest request) {
    if (request.getNamespaces() == null) return true;
    return request.getNamespaces().stream().noneMatch(Objects::isNull);
}

Try / catch

try {
    userTokenService.createToken(request, operator);
} catch (BadRequestException e) {
    if (e.getMessage().contains("namespace scope can not be null")) {
        return Response.status(400).entity("Remove null entries from the namespaces array").build();
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling createToken() with a request where namespaces is a List containing at least one null entry — e.g., Arrays.asList(scope1, null, scope3). This typically happens when JSON deserialization of a partially-empty array or programmatic list construction leaves a null slot.

Common situations: JSON payload has a null object in the namespaces array (e.g., [{"appId":"x"}, null]). List built dynamically where a condition produces null instead of skipping. Deserialization of malformed or sparse arrays.

Related errors


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