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

Fix incorrect gateway URL port (-1) in Dev Portal when custom tenant URL is used with HTTP-only APIs. #13066

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

Conversation

Dakshithas
Copy link

Scenario

This issue occurs when:

  • A tenant has a custom gateway domain configured (e.g., via registry path /customurl/api-cloud/...)
  • An API is created under that tenant with only HTTP transport enabled

As a result:

  • The Dev Portal shows a incorrect gateway URL like http://example.com:-1/context/version
  • The Try Out page fails to load because the Swagger API call returns incomplete server details (missing gateway URLs)

Fix

  • Fixes the -1 port issue by correctly setting the port.
  • Fixes Swagger server URL generation by determining the correct transport protocol (HTTP or HTTPS) based on the API’s transport level security.

Related Issue: wso2/api-manager#3819

…n custom tenant URL is used with HTTP-only APIs.
Copy link

coderabbitai bot commented Apr 2, 2025

📝 Walkthrough

Walkthrough

This pull request updates the API management functionality in two components. In the API consumer implementation, the logic for processing custom URLs now checks for both HTTP and HTTPS protocols, defaulting to assign the URL to both when no valid prefix is present. In the API mapping utility, error handling is added to manage malformed custom gateway URLs, with enhancements for extracting the HTTP port and context from the provided URL.

Changes

File(s) Change Summary
components/apimgt/.../APIConsumerImpl.java Updated the getHostWithSchemeMappingForEnvironment method to first check if customUrl is non-null and then verify if it starts with "http://" or "https://". If neither condition is met, the URL is assigned to both HTTP and HTTPS protocols. The method signature remains the same.
components/apimgt/.../APIMappingUtil.java Introduced error handling in the fromAPIRevisionToEndpoints method by creating a URL object from customGatewayUrl and throwing an APIManagementException upon a malformed URL. Enhanced logic for extracting the HTTP port—defaulting to VHost.DEFAULT_HTTP_PORT if necessary—and setting the HTTP context from the URL path.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant APIConsumerImpl
    Client->>APIConsumerImpl: getHostWithSchemeMappingForEnvironment(api, org, env)
    APIConsumerImpl->>APIConsumerImpl: Check if customUrl is not null
    APIConsumerImpl->>APIConsumerImpl: Verify if customUrl starts with "http://" or "https://"
    alt Valid prefix found
        APIConsumerImpl->>APIConsumerImpl: Map customUrl to corresponding protocol
    else No valid prefix
        APIConsumerImpl->>APIConsumerImpl: Map customUrl to both HTTP and HTTPS
    end
    APIConsumerImpl-->>Client: Return protocol mapping
Loading
sequenceDiagram
    participant Client
    participant APIMappingUtil
    Client->>APIMappingUtil: fromAPIRevisionToEndpoints(apiRevision, customGatewayUrl)
    APIMappingUtil->>APIMappingUtil: Attempt to create URL from customGatewayUrl
    alt URL creation error
        APIMappingUtil-->>Client: Throw APIManagementException
    else URL creation successful
        APIMappingUtil->>APIMappingUtil: Extract HTTP port and context from URL
        APIMappingUtil-->>Client: Return endpoints configuration
    end
Loading

Suggested reviewers

  • tgtshanika
  • chamilaadhi
  • tharindu1st
  • dushaniw
  • Arshardh
  • HeshanSudarshana
  • AnuGayan

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.31.1)
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3ac8150 and 851fa71.

📒 Files selected for processing (2)
  • components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java (1 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/APIMappingUtil.java (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java (2)
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java (1)
  • APIConstants (32-3337)
components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/APIConstants.java (1)
  • APIConstants (21-416)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build-product (2, group2)
  • GitHub Check: build-product (1, group1)
  • GitHub Check: build-product (3, group3)
  • GitHub Check: run-benchmark-test
  • GitHub Check: build-carbon
  • GitHub Check: build-product (4, group4)
🔇 Additional comments (4)
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java (1)

3602-3616: Improved custom URL handling for both HTTP and HTTPS protocols.

This change enhances the custom URL handling logic by accommodating URLs that don't explicitly start with protocol prefixes. When a custom URL is provided without a protocol prefix, it will now be correctly mapped to both HTTP and HTTPS protocols, which addresses the issue of incorrect gateway URLs with -1 ports being displayed in the Dev Portal.

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/APIMappingUtil.java (3)

76-77: Added necessary imports for URL handling.

These imports are required for proper URL parsing and validation in the updated code, which will handle the custom gateway URLs better.


565-570: Good addition of robust error handling for custom gateway URLs.

The code now properly parses the custom gateway URL using the URL class and includes appropriate exception handling. This is a significant improvement that should prevent issues when invalid URLs are provided.


572-584: Fixed the incorrect port display issue when using HTTP-only APIs.

This is the core fix for the issue described in PR #13066. The code now correctly:

  1. Checks if HTTP transport is enabled
  2. Extracts the protocol and port from the custom URL
  3. Properly handles the -1 port value (returned when no port is specified in URL)
  4. Sets the HTTP context from the URL path

This implementation resolves the issue where the gateway URL was incorrectly displayed as http://example.com:-1/context/version in the Dev Portal.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.

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