{"record":{"id":"0f1f81e131ebfdf5","repo":"SonarSource/sonarqube","slug":"new-code-definition-type-is-required-when-new-code","errorCode":null,"errorMessage":"New code definition type is required when new code definition value is provided","messagePattern":"New code definition type is required when new code definition value is provided","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":400,"severity":"error","filePath":"server/sonar-webserver-common/src/main/java/org/sonar/server/common/newcodeperiod/NewCodeDefinitionResolver.java","lineNumber":113,"sourceCode":"    dto.setProjectUuid(projectUuid);\n\n    if (isCommunityEdition) {\n      dto.setBranchUuid(mainBranchUuid);\n    }\n\n    getNewCodeDefinitionValueProjectCreation(newCodePeriodType, newCodeDefinitionValue, defaultBranchName).ifPresent(dto::setValue);\n\n    if (!CaycUtils.isNewCodePeriodCompliant(dto.getType(), dto.getValue())) {\n      throw new IllegalArgumentException(\"Failed to set the New Code Definition. The given value is not compatible with the Clean as You Code methodology. \"\n        + \"Please refer to the documentation for compliant options.\");\n    }\n\n    return dto;\n  }\n\n  public static void checkNewCodeDefinitionParam(@Nullable String newCodeDefinitionType, @Nullable String newCodeDefinitionValue) {\n    if (newCodeDefinitionType == null && newCodeDefinitionValue != null) {\n      throw new IllegalArgumentException(\"New code definition type is required when new code definition value is provided\");\n    }\n  }\n\n  private static Optional<String> getNewCodeDefinitionValueProjectCreation(NewCodePeriodType type, @Nullable String value, String defaultBranchName) {\n    return switch (type) {\n      case PREVIOUS_VERSION -> {\n        Preconditions.checkArgument(value == null, UNEXPECTED_VALUE_ERROR_MESSAGE, type);\n        yield Optional.empty();\n      }\n      case NUMBER_OF_DAYS -> {\n        requireValue(type, value);\n        yield Optional.of(parseDays(value));\n      }\n      case REFERENCE_BRANCH -> {\n        Preconditions.checkArgument(value == null, UNEXPECTED_VALUE_ERROR_MESSAGE, type);\n        yield Optional.of(defaultBranchName);\n      }\n      default -> throw new IllegalStateException(\"Unexpected type: \" + type);","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-common/src/main/java/org/sonar/server/common/newcodeperiod/NewCodeDefinitionResolver.java#L95-L131","documentation":"The New Code Definition requires both a type and, for most types, a value. NewCodeDefinitionResolver.checkNewCodeDefinitionParam enforces that a value is never submitted without a type, throwing this IllegalArgumentException when newCodeDefinitionType is null but newCodeDefinitionValue is present.","triggerScenarios":"Calling the resolver/API with newCodeDefinitionValue set (e.g. '30' or a branch name) while omitting newCodeDefinitionType.","commonSituations":"Scripts that update only the value assuming the type is remembered; partially filled web forms; API clients that serialize empty type strings as null.","solutions":["Provide newCodeDefinitionType (e.g. NUMBER_OF_DAYS, PREVIOUS_VERSION, REFERENCE, SPECIFIC_ANALYSIS) together with the value.","Remove the value entirely if no type change is intended."],"exampleFix":"// before\ncheckNewCodeDefinitionParam(null, \"30\")\n\n// after\ncheckNewCodeDefinitionParam(\"NUMBER_OF_DAYS\", \"30\")","handlingStrategy":"validation","validationCode":"// before submit\nif (value != null && (type == null || type.isBlank())) {\n  throw new IllegalArgumentException(\"Type required when value provided\");\n}","typeGuard":null,"tryCatchPattern":"try { NewCodeDefinitionResolver.checkNewCodeDefinitionParam(type, value); } catch (IllegalArgumentException e) { promptForType(); }","preventionTips":["Send type and value together in API calls.","Make the type field mandatory in forms whenever a value is filled."],"tags":["new-code-definition","validation","missing-parameter"],"backgroundTag":"missing-required-argument","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}