Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate quota limits to be non-negative in throttling policies #13059

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RusJaI
Copy link
Contributor

@RusJaI RusJaI commented Mar 27, 2025

Backend fix for wso2/api-manager#3764

Copy link

coderabbitai bot commented Mar 27, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a new error code for handling invalid quota limits and enhances the validation process for throttle policies. A new error code, INVALID_QUOTA_LIMIT, has been added to indicate that quota limits must be non-negative. Additionally, the throttle policy API endpoints now include a validation step to check the default limit property. This is supported by a new public method and several private helper methods that validate various throttle policy properties, ensuring all values are non-negative. The overall control flow remains unchanged.

Changes

File(s) Change Summary
components/.../ExceptionCodes.java Added error code INVALID_QUOTA_LIMIT(901201, "Invalid Quota Limit", 400, "Quota limit should be non negative.") to the ExceptionCodes enum.
components/.../ThrottlingApiServiceImpl.java Added a call to RestApiAdminUtils.validateThrottlePolicyDefaultLimitProperty(body) in methods for throttle policy creation.
components/.../RestApiAdminUtils.java Introduced a new public method validateThrottlePolicyDefaultLimitProperty and private methods for validating request count, bandwidth, event count, and AI quota limits, ensuring all values are non-negative.

Suggested reviewers

  • tgtshanika
  • chamilaadhi
  • dushaniw
  • Arshardh
  • senthuran16
  • AnuGayan
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (2)
components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java (2)

46-46: Replace wildcard import for clarity.

Consider importing only the specific classes you need instead of using the wildcard import import java.util.*; for better maintainability and clarity.


206-228: Abstract duplicate negative checks for AI quota limit.

While validateAiQuotaLimit() thoroughly checks each sub-field of aiApiQuota, you might unify these negative checks in a helper method or loop to reduce repetition. Also, ensure you handle a null aiApiQuota object.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b08daad and aa05a15.

📒 Files selected for processing (3)
  • components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java (1 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ThrottlingApiServiceImpl.java (3 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ThrottlingApiServiceImpl.java (1)
components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java (1)
  • RestApiAdminUtils (52-471)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build-product (2, group2)
  • GitHub Check: build-product (4, group4)
  • GitHub Check: build-product (3, group3)
  • GitHub Check: build-product (1, group1)
  • GitHub Check: run-benchmark-test
  • GitHub Check: build-carbon
🔇 Additional comments (5)
components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java (1)

429-429: Looks good - New error code for handling negative quota limits.

This addition of INVALID_QUOTA_LIMIT error code is clean and follows the established pattern for error codes in the system. It will be used for validating that quota limits in throttling policies are non-negative values.

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ThrottlingApiServiceImpl.java (3)

114-114: Good addition of validation for Advanced throttling policy limit properties.

This validation ensures that the quota limits in the Advanced throttling policy are non-negative before proceeding with policy creation, improving input validation.


308-308: Good addition of validation for Application throttling policy limit properties.

This validation ensures that the quota limits in the Application throttling policy are non-negative before proceeding with policy creation, improving input validation.


509-509: Good addition of validation for Subscription throttling policy limit properties.

This validation ensures that the quota limits in the Subscription throttling policy are non-negative before proceeding with policy creation, improving input validation.

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java (1)

35-38: Imports referencing new throttle policy DTOs look appropriate.

These newly added imports correctly align with the new policy validation methods below.

Comment on lines +176 to +188
private static void validateRequestCountLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.REQUESTCOUNTLIMIT)) {
if (throttleLimitDTO.getRequestCount().getRequestCount() < 0) {
throw new APIManagementException("Request count should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Check for null before using requestCount.

In validateRequestCountLimit(), calling throttleLimitDTO.getRequestCount().getRequestCount() can trigger an NPE if throttleLimitDTO.getRequestCount() is null. Consider adding a null check to prevent runtime exceptions.

Comment on lines +186 to +198
private static void validateBandwidthLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.BANDWIDTHLIMIT)) {
if (throttleLimitDTO.getBandwidth().getDataAmount() < 0) {
throw new APIManagementException("Bandwidth should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.valueOf(throttleLimitDTO.getBandwidth().getDataAmount())));
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Check for null before using bandwidth.

Similar to the request count limit, ensure throttleLimitDTO.getBandwidth() is non-null before accessing getDataAmount(), preventing NPE in validateBandwidthLimit().

Comment on lines +196 to +208
private static void validateEventCountLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.EVENTCOUNTLIMIT)) {
if (throttleLimitDTO.getEventCount().getEventCount() < 0) {
throw new APIManagementException("Event count should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.valueOf(throttleLimitDTO.getEventCount().getEventCount())));
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Check for null before using eventCount.

In validateEventCountLimit(), invoking throttleLimitDTO.getEventCount().getEventCount() can fail if eventCount is not provided. A null check is necessary to avoid potential crashes.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

♻️ Duplicate comments (1)
components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java (1)

148-178: ⚠️ Potential issue

Add null checks for defaultLimit to prevent NullPointerException

The method implements validation of throttle policy default limits, but it doesn't check if the retrieved throttleLimitDTO is null before using it, which could lead to NullPointerException.

 public static void validateThrottlePolicyDefaultLimitProperty(ThrottlePolicyDTO throttlePolicyDTO)
         throws APIManagementException {
     ThrottleLimitDTO throttleLimitDTO;
     if (throttlePolicyDTO instanceof AdvancedThrottlePolicyDTO) {
+        if (((AdvancedThrottlePolicyDTO) throttlePolicyDTO).getDefaultLimit() == null) {
+            throw new APIManagementException("Default limit cannot be null",
+                    ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, "NULL_LIMIT"));
+        }
         throttleLimitDTO = ((AdvancedThrottlePolicyDTO) throttlePolicyDTO).getDefaultLimit();
         validateRequestCountLimit(throttleLimitDTO);
         validateBandwidthLimit(throttleLimitDTO);
     } else if (throttlePolicyDTO instanceof ApplicationThrottlePolicyDTO) {
+        if (((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getDefaultLimit() == null) {
+            throw new APIManagementException("Default limit cannot be null",
+                    ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, "NULL_LIMIT"));
+        }
         throttleLimitDTO = ((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getDefaultLimit();
         validateRequestCountLimit(throttleLimitDTO);
         validateBandwidthLimit(throttleLimitDTO);
         if (((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getBurstLimit() != null) {
             if (((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getBurstLimit().getRateLimitCount() < 0) {
                 throw new APIManagementException("Burst Control rate limit should be a non-negative value",
                         ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
                                 String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
             }
         }
     } else if (throttlePolicyDTO instanceof SubscriptionThrottlePolicyDTO) {
+        if (((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getDefaultLimit() == null) {
+            throw new APIManagementException("Default limit cannot be null",
+                    ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, "NULL_LIMIT"));
+        }
         throttleLimitDTO = ((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getDefaultLimit();
         validateRequestCountLimit(throttleLimitDTO);
         validateBandwidthLimit(throttleLimitDTO);
         validateEventCountLimit(throttleLimitDTO);
         validateAiQuotaLimit(throttleLimitDTO);
         if (((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getRateLimitCount() < 0) {
             throw new APIManagementException("Rate limit count should be a non-negative value",
                     ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
                             String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
         }
     }
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aa05a15 and 14235a3.

📒 Files selected for processing (3)
  • components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java (1 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ThrottlingApiServiceImpl.java (3 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ThrottlingApiServiceImpl.java
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build-product (2, group2)
  • GitHub Check: build-product (3, group3)
  • GitHub Check: build-product (4, group4)
  • GitHub Check: build-product (1, group1)
  • GitHub Check: run-benchmark-test
  • GitHub Check: build-carbon
🔇 Additional comments (3)
components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java (3)

180-188: Check for null before using requestCount

In validateRequestCountLimit(), calling throttleLimitDTO.getRequestCount().getRequestCount() can trigger an NPE if throttleLimitDTO.getRequestCount() is null. Consider adding a null check to prevent runtime exceptions.

 private static void validateRequestCountLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
     if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.REQUESTCOUNTLIMIT)) {
+        if (throttleLimitDTO.getRequestCount() == null) {
+            throw new APIManagementException("Request count cannot be null",
+                    ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, "NULL_REQUEST_COUNT"));
+        }
         if (throttleLimitDTO.getRequestCount().getRequestCount() < 0) {
             throw new APIManagementException("Request count should be a non-negative value",
                     ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
                             String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
         }
     }
 }

190-198: Check for null before using bandwidth

Similar to the request count limit, ensure throttleLimitDTO.getBandwidth() is non-null before accessing getDataAmount(), preventing NPE in validateBandwidthLimit().

 private static void validateBandwidthLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
     if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.BANDWIDTHLIMIT)) {
+        if (throttleLimitDTO.getBandwidth() == null) {
+            throw new APIManagementException("Bandwidth cannot be null",
+                    ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, "NULL_BANDWIDTH"));
+        }
         if (throttleLimitDTO.getBandwidth().getDataAmount() < 0) {
             throw new APIManagementException("Bandwidth should be a non-negative value",
                     ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
                             String.valueOf(throttleLimitDTO.getBandwidth().getDataAmount())));
         }
     }
 }

200-208: Check for null before using eventCount

In validateEventCountLimit(), invoking throttleLimitDTO.getEventCount().getEventCount() can fail if eventCount is not provided. A null check is necessary to avoid potential crashes.

 private static void validateEventCountLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
     if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.EVENTCOUNTLIMIT)) {
+        if (throttleLimitDTO.getEventCount() == null) {
+            throw new APIManagementException("Event count cannot be null",
+                    ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, "NULL_EVENT_COUNT"));
+        }
         if (throttleLimitDTO.getEventCount().getEventCount() < 0) {
             throw new APIManagementException("Event count should be a non-negative value",
                     ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
                             String.valueOf(throttleLimitDTO.getEventCount().getEventCount())));
         }
     }
 }

Comment on lines +210 to +232
private static void validateAiQuotaLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.AIAPIQUOTALIMIT)) {
List<String> paramNames = new ArrayList<>();
if (throttleLimitDTO.getAiApiQuota().getRequestCount() < 0) {
paramNames.add("Request Count");
}
if (throttleLimitDTO.getAiApiQuota().getTotalTokenCount() < 0) {
paramNames.add("Total Token Count");
}
if (throttleLimitDTO.getAiApiQuota().getPromptTokenCount() < 0) {
paramNames.add("Prompt Token Count");
}
if (throttleLimitDTO.getAiApiQuota().getCompletionTokenCount() < 0) {
paramNames.add("Complete Token Count");
}

if (!paramNames.isEmpty()) {
throw new APIManagementException("AI quota limit should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.join(",", paramNames)));
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add null check for AI quota

The validateAiQuotaLimit() method doesn't check if throttleLimitDTO.getAiApiQuota() is null before accessing its methods, which could lead to a NullPointerException.

 private static void validateAiQuotaLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
     if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.AIAPIQUOTALIMIT)) {
+        if (throttleLimitDTO.getAiApiQuota() == null) {
+            throw new APIManagementException("AI API quota cannot be null",
+                    ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, "NULL_AI_QUOTA"));
+        }
         List<String> paramNames = new ArrayList<>();
         if (throttleLimitDTO.getAiApiQuota().getRequestCount() < 0) {
             paramNames.add("Request Count");
         }
         if (throttleLimitDTO.getAiApiQuota().getTotalTokenCount() < 0) {
             paramNames.add("Total Token Count");
         }
         if (throttleLimitDTO.getAiApiQuota().getPromptTokenCount() < 0) {
             paramNames.add("Prompt Token Count");
         }
         if (throttleLimitDTO.getAiApiQuota().getCompletionTokenCount() < 0) {
             paramNames.add("Complete Token Count");
         }

         if (!paramNames.isEmpty()) {
             throw new APIManagementException("AI quota limit should be a non-negative value",
                     ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
                             String.join(",", paramNames)));
         }
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private static void validateAiQuotaLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.AIAPIQUOTALIMIT)) {
List<String> paramNames = new ArrayList<>();
if (throttleLimitDTO.getAiApiQuota().getRequestCount() < 0) {
paramNames.add("Request Count");
}
if (throttleLimitDTO.getAiApiQuota().getTotalTokenCount() < 0) {
paramNames.add("Total Token Count");
}
if (throttleLimitDTO.getAiApiQuota().getPromptTokenCount() < 0) {
paramNames.add("Prompt Token Count");
}
if (throttleLimitDTO.getAiApiQuota().getCompletionTokenCount() < 0) {
paramNames.add("Complete Token Count");
}
if (!paramNames.isEmpty()) {
throw new APIManagementException("AI quota limit should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.join(",", paramNames)));
}
}
}
private static void validateAiQuotaLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException {
if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.AIAPIQUOTALIMIT)) {
if (throttleLimitDTO.getAiApiQuota() == null) {
throw new APIManagementException("AI API quota cannot be null",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, "NULL_AI_QUOTA"));
}
List<String> paramNames = new ArrayList<>();
if (throttleLimitDTO.getAiApiQuota().getRequestCount() < 0) {
paramNames.add("Request Count");
}
if (throttleLimitDTO.getAiApiQuota().getTotalTokenCount() < 0) {
paramNames.add("Total Token Count");
}
if (throttleLimitDTO.getAiApiQuota().getPromptTokenCount() < 0) {
paramNames.add("Prompt Token Count");
}
if (throttleLimitDTO.getAiApiQuota().getCompletionTokenCount() < 0) {
paramNames.add("Complete Token Count");
}
if (!paramNames.isEmpty()) {
throw new APIManagementException("AI quota limit should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.join(",", paramNames)));
}
}
}

Comment on lines +172 to +176
if (((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getRateLimitCount() < 0) {
throw new APIManagementException("Rate limit count should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix potential NullPointerException in rate limit validation

When validating the rate limit count, the code references throttleLimitDTO.getRequestCount().getRequestCount() in the error code, but if the current throttle limit type is not REQUESTCOUNTLIMIT, this could cause a NullPointerException.

 if (((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getRateLimitCount() < 0) {
     throw new APIManagementException("Rate limit count should be a non-negative value",
             ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
-                    String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
+                    String.valueOf(((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getRateLimitCount())));
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getRateLimitCount() < 0) {
throw new APIManagementException("Rate limit count should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
}
if (((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getRateLimitCount() < 0) {
throw new APIManagementException("Rate limit count should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.valueOf(((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getRateLimitCount())));
}

if (((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getBurstLimit().getRateLimitCount() < 0) {
throw new APIManagementException("Burst Control rate limit should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect error message parameter

In the exception thrown for negative burst limit, the error message references throttleLimitDTO.getRequestCount().getRequestCount() which may not be related to the burst limit being validated and could cause NullPointerException if request count is null.

 throw new APIManagementException("Burst Control rate limit should be a non-negative value",
         ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
-                String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
+                String.valueOf(((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getBurstLimit().getRateLimitCount())));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount())));
throw new APIManagementException("Burst Control rate limit should be a non-negative value",
ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT,
String.valueOf(((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getBurstLimit().getRateLimitCount())));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant