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

Implement nested error message handling for schema validation #13045

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Jithmi004
Copy link
Contributor

Purpose

The schema validator in the apimgt.gateway no longer returns the full error message to the user. Only the first error in the validation report is sent. This needs to be fixed.

Implementation Details

A function getErrorMessage() is implemented where it recursively checks whether the ValidationReport.Message has nested messages and get the concatenated message as the result. This fix has been added to both the handleRequest and handleResponse flows.

Related To

wso2/api-manager#3585

Copy link

coderabbitai bot commented Mar 7, 2025

📝 Walkthrough

Walkthrough

The code has been updated in the SchemaValidator class to improve error message handling during schema validation. A new private method, getErrorMessage, has been introduced to recursively construct detailed error messages from nested validation errors. The updated logic now cleans up the final message output by removing the trailing comma and space, ensuring more structured error information in both request and response handling.

Changes

File(s) Change Summary
components/apimgt/.../SchemaValidator.java Added a private method getErrorMessage to recursively accumulate nested error messages. Updated handleRequest and handleResponse to use the new method and remove the trailing comma and space from error messages.

Sequence Diagram(s)

sequenceDiagram
    participant Handler as handleRequest/handleResponse
    participant Validator as SchemaValidator
    participant Msg as ValidationReport.Message
    Handler->>Validator: Invoke getErrorMessage(topLevelMessage)
    Validator->>Msg: Check for nested errors
    alt Nested errors exist
        Validator->>Validator: Recursively call getErrorMessage(nestedMessage)
    end
    Validator->>Validator: Construct comprehensive error message
    Validator->>Validator: Remove trailing comma and space
    Validator->>Handler: Return formatted error message
Loading

Suggested reviewers

  • tgtshanika
  • chamilaadhi
  • dushaniw
  • Arshardh
  • AnuGayan
  • PasanT9

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5ee9d1e and 241c1a1.

📒 Files selected for processing (1)
  • components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/SchemaValidator.java (3 hunks)

🪧 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 resolve resolve all the CodeRabbit review comments.
  • @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: 0

🧹 Nitpick comments (1)
components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/SchemaValidator.java (1)

122-132: Well-implemented recursive error message concatenation.

The getErrorMessage method elegantly handles nested validation errors through recursion. The implementation correctly:

  1. Returns the base message when no nested messages exist
  2. Recursively processes all nested messages when present
  3. Properly formats the combined message with appropriate separators

This addresses the original issue where only the first error message was being returned to users, providing a much better debugging experience.

Consider adding a safeguard against extremely deep recursion (though unlikely in practice) to prevent potential stack overflow:

 private String getErrorMessage(ValidationReport.Message message){
+    return getErrorMessage(message, 0);
+}
+
+private String getErrorMessage(ValidationReport.Message message, int depth){
+    // Prevent extremely deep recursion (adjust max depth as needed)
+    if (depth > 100) {
+        return message.getMessage() + " [max depth reached]";
+    }
     if(message.getNestedMessages().isEmpty()){
         return message.getMessage();
     }
     StringBuilder combinedMessages = new StringBuilder();
     combinedMessages.append(message.getMessage());
     for (ValidationReport.Message nestedMessage : message.getNestedMessages()) {
-        combinedMessages.append(", ").append(getErrorMessage(nestedMessage));
+        combinedMessages.append(", ").append(getErrorMessage(nestedMessage, depth + 1));
     }
     return combinedMessages.toString().trim();
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5ee9d1e and 15a0833.

📒 Files selected for processing (1)
  • components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/SchemaValidator.java (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build-product (4, group4)
  • GitHub Check: build-product (2, group2)
  • GitHub Check: run-benchmark-test
  • GitHub Check: build-carbon
  • GitHub Check: build-product (1, group1)
  • GitHub Check: build-product (3, group3)
🔇 Additional comments (2)
components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/SchemaValidator.java (2)

81-86: Improved error message handling in request validation.

The change now properly concatenates all error messages including nested ones, providing more comprehensive feedback to API users. The cleanup of trailing commas and spaces makes the error messages more readable.


108-113: Consistent implementation of error message handling in response validation.

Good to see the same error message handling approach applied to both request and response validation paths, ensuring consistency in the error reporting mechanism.

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.

2 participants