{"record":{"id":"2e346cd582459683","repo":"apolloconfig/apollo","slug":"token-namespace-scope-can-not-be-null","errorCode":null,"errorMessage":"Token namespace scope can not be null","messagePattern":"Token namespace scope can not be null","errorType":"validation","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/UserTokenService.java","lineNumber":369,"sourceCode":"  }\n\n  private Set<String> emptyToNull(Set<String> values) {\n    return values == null || values.isEmpty() ? null : values;\n  }\n\n  private void validateCreateRequest(UserTokenCreateRequest request) {\n    if (request == null || StringUtils.isBlank(request.getName())) {\n      throw new BadRequestException(\"Token name can not be blank\");\n    }\n    if (request.getRateLimit() != null && request.getRateLimit() < 0) {\n      throw BadRequestException.rateLimitIsInvalid();\n    }\n    if (request.getNamespaces() == null) {\n      return;\n    }\n    for (UserTokenNamespaceScope namespaceScope : request.getNamespaces()) {\n      if (namespaceScope == null) {\n        throw new BadRequestException(\"Token namespace scope can not be null\");\n      }\n    }\n  }\n\n  private void validateUserEnabled(String userId) {\n    UserInfo userInfo = userService.findByUserId(userId);\n    if (userInfo == null) {\n      throw BadRequestException.userNotExists(userId);\n    }\n    if (userInfo.getEnabled() != USER_ENABLED) {\n      throw new BadRequestException(\"User is disabled\");\n    }\n  }\n\n  private Date resolveExpires(Date requestedExpires, Date now) {\n    Date expires = requestedExpires;\n    if (expires == null) {\n      Calendar calendar = Calendar.getInstance();","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/apolloconfig/apollo/blob/d95fc18d112589efc09ddcbe1507047584d55251/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/UserTokenService.java#L351-L387","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Filter out null entries from the namespaces list before setting it on the request.","Ensure the client never sends null objects in the namespaces JSON array.","Validate each namespace scope is non-null before adding to the list."],"exampleFix":"// before\nrequest.setNamespaces(Arrays.asList(scope1, null, scope3));\n// after\nrequest.setNamespaces(Arrays.asList(scope1, scope3));","handlingStrategy":"validation","validationCode":"if (request.getNamespaces() != null) {\n    request.getNamespaces().removeIf(Objects::isNull);\n}\nuserTokenService.createToken(request, operator);","typeGuard":"static boolean hasNoNullNamespaceScopes(UserTokenCreateRequest request) {\n    if (request.getNamespaces() == null) return true;\n    return request.getNamespaces().stream().noneMatch(Objects::isNull);\n}","tryCatchPattern":"try {\n    userTokenService.createToken(request, operator);\n} catch (BadRequestException e) {\n    if (e.getMessage().contains(\"namespace scope can not be null\")) {\n        return Response.status(400).entity(\"Remove null entries from the namespaces array\").build();\n    }\n    throw e;\n}","preventionTips":["Filter null entries from the namespaces list before setting it on the request.","Validate JSON array contents on the client side before submission."],"tags":["apollo-portal","user-token","validation","bad-request"],"backgroundTag":null,"analyzedSha":"d95fc18d112589efc09ddcbe1507047584d55251","analyzedAt":"2026-08-14T04:00:05.477Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}