{"record":{"id":"4f23c26675da6085","repo":"SonarSource/sonarqube","slug":"invalid-type-s-s-can-only-be-set-with-types","errorCode":null,"errorMessage":"Invalid type '%s'. %s can only be set with types: %s","messagePattern":"Invalid type '(.+?)'\\. (.+?) can only be set with types: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":400,"severity":"error","filePath":"server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java","lineNumber":285,"sourceCode":"  private SnapshotDto getAnalysis(DbSession dbSession, String analysisUuid, ProjectDto project, BranchDto branch) {\n    SnapshotDto snapshotDto = dbClient.snapshotDao().selectByUuid(dbSession, analysisUuid)\n      .orElseThrow(() -> new NotFoundException(format(\"Analysis '%s' is not found\", analysisUuid)));\n    checkAnalysis(dbSession, project, branch, snapshotDto);\n    return snapshotDto;\n  }\n\n  private void checkAnalysis(DbSession dbSession, ProjectDto project, BranchDto branch, SnapshotDto analysis) {\n    BranchDto analysisBranch = dbClient.branchDao().selectByUuid(dbSession, analysis.getRootComponentUuid()).orElse(null);\n    boolean analysisMatchesProjectBranch = analysisBranch != null && analysisBranch.getUuid().equals(branch.getUuid());\n\n    checkArgument(analysisMatchesProjectBranch,\n      \"Analysis '%s' does not belong to branch '%s' of project '%s'\",\n      analysis.getUuid(), branch.getKey(), project.getKey());\n  }\n\n  private static void checkType(String name, Set<NewCodePeriodType> validTypes, NewCodePeriodType type) {\n    if (!validTypes.contains(type)) {\n      throw new IllegalArgumentException(String.format(\"Invalid type '%s'. %s can only be set with types: %s\", type, name, validTypes));\n    }\n  }\n}\n","sourceCodeStart":267,"sourceCodeEnd":289,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java#L267-L289","documentation":"checkType enforces scope-specific whitelists: the overall setting accepts only OVERALL_TYPES and branch settings only BRANCH_TYPES. When the parsed type is valid as an enum but outside the whitelist for the request's scope, it throws with the scope name and the allowed types. This prevents e.g. setting a reference branch as the global New Code Definition.","triggerScenarios":"Calling api/new_code_periods/set without a project (overall scope) with type=REFERENCE_BRANCH, or with a branch parameter using a type not in BRANCH_TYPES.","commonSituations":"Copy-pasting a project-level set command to set the overall default; scripts configuring REFERENCE_BRANCH globally; misunderstanding which types apply to overall vs branch scope.","solutions":["Use only the types listed in the error message for the target scope (e.g. PREVIOUS_VERSION/NUMBER_OF_DAYS for overall).","If you need REFERENCE_BRANCH, set it per-project or per-branch, not as the overall definition.","Read the error's allowed-types list and adjust type accordingly."],"exampleFix":"// before (overall scope)\nPOST api/new_code_periods/set?type=REFERENCE_BRANCH&value=master\n// after\nPOST api/new_code_periods/set?type=PREVIOUS_VERSION","handlingStrategy":"validation","validationCode":"const OVERALL = ['PREVIOUS_VERSION', 'SPECIFIC_ANALYSIS', 'NUMBER_OF_DAYS'];\nconst scope = params.project ? (params.branch ? 'branch' : 'project') : 'overall';\nif (scope === 'overall' && !OVERALL.includes(params.type)) throw new Error('Type not allowed for overall scope: ' + params.type);","typeGuard":null,"tryCatchPattern":"try { ... } catch (e) { const m = String(e.message).match(/can only be set with types: (.*)/); if (m) { params.type = m[1].split(', ')[0]; return retry(params); } throw e; }","preventionTips":["Map scope -> allowed types in your tooling instead of sharing one type constant everywhere.","Never set REFERENCE_BRANCH as the overall definition."],"tags":["webapi","new-code-period","enum","scope","sonarqube"],"backgroundTag":"invalid-enum-value","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}