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

feat: openapi query namespace support not fill item #5249

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

Conversation

youngzil
Copy link
Contributor

@youngzil youngzil commented Oct 8, 2024

What's the purpose of this PR

XXXXX

Which issue(s) this PR fixes:

Fixes #5243

Brief changelog

XXXXX

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Read the Contributing Guide before making this pull request.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit tests to verify the code.
  • Run mvn clean test to make sure this pull request doesn't break anything.
  • Update the CHANGES log.

Summary by CodeRabbit

  • New Features
    • Enhanced namespace retrieval with detailed item options.
  • Bug Fixes
    • Addressed issues related to namespace loading and export functionality.
  • Documentation
    • Updated method signatures to reflect new parameters for improved clarity.
  • Chores
    • Upgraded dependency version for improved compatibility and performance.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 8, 2024
Copy link
Contributor

coderabbitai bot commented Oct 8, 2024

Walkthrough

The changes in this pull request involve modifications to several classes within the Apollo project, primarily focusing on enhancing the functionality of methods related to namespace management. Key updates include the addition of a boolean parameter, fillItemDetail, to multiple method signatures, which allows for more control over the retrieval of detailed item information within namespaces. This affects the behavior of both the API endpoints and the internal service methods that interact with namespaces, as well as adjustments to corresponding test cases.

Changes

File Change Summary
.../ServerNamespaceOpenApiService.java Updated getNamespace and getNamespaces methods to include boolean fillItemDetail parameter.
.../NamespaceController.java Modified findNamespaces and loadNamespace methods to include fillItemDetail parameter with default value.
.../ConfigsExportController.java Updated exportItems method to include new boolean parameter in loadNamespaceBO method call.
.../ConfigsExportService.java Modified exportNamespaces method to include new boolean parameter in findNamespaceBOs method call.
.../NamespaceService.java Updated findNamespaceBOs, loadNamespaceBO, and transformNamespace2BO methods to include fillItemDetail parameter.
.../ConfigsExportServiceTest.java Adjusted testNamespaceExportImport method to include new parameter in findNamespaceBOs method calls.
.../NamespaceServiceTest.java Updated loadNamespaceBO method to include new boolean parameter.
pom.xml Updated apollo-java version from 2.2.0 to 2.4.0-SNAPSHOT.

Assessment against linked issues

Objective Addressed Explanation
Add parameter to control item detail retrieval in namespace API (5243)

Possibly related PRs

Suggested labels

size:L, lgtm

🐰 "In the code we hop and play,
New parameters lead the way.
With details filled or left behind,
Our namespaces are now refined.
A version bump, we cheer and sing,
In Apollo's realm, we bring the spring!" 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
Contributor

@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: 1

🧹 Outside diff range and nitpick comments (11)
apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java (1)

61-63: LGTM! Consider adding method documentation.

The changes to the getNamespace method align well with the PR objectives. The new fillItemDetail parameter is correctly integrated into the method signature and passed to the loadNamespaceBO call.

Consider adding Javadoc to document the method's purpose and the new fillItemDetail parameter. For example:

/**
 * Retrieves namespace information.
 *
 * @param appId The application ID
 * @param env The environment
 * @param clusterName The cluster name
 * @param namespaceName The namespace name
 * @param fillItemDetail Whether to include detailed item information
 * @return The namespace data transfer object
 */
apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/NamespaceController.java (3)

85-87: LGTM: Method updated to support optional item detail retrieval.

The changes to findNamespaces method align well with the PR objectives. The new fillItemDetail parameter allows clients to control whether item details are populated, potentially improving performance.

Consider adding a brief Javadoc comment to explain the purpose of the fillItemDetail parameter:

/**
 * @param fillItemDetail If true, populates item details for each namespace. Defaults to true.
 */

92-94: LGTM: Method updated consistently with findNamespaces.

The changes to loadNamespace method are consistent with the modifications made to findNamespaces and align well with the PR objectives.

Similar to the suggestion for findNamespaces, consider adding a brief Javadoc comment to explain the purpose of the fillItemDetail parameter:

/**
 * @param fillItemDetail If true, populates item details for the namespace. Defaults to true.
 */

33-33: Summary: Successful implementation of optional item detail retrieval.

The changes in this file successfully implement the feature to control item detail population for namespaces. The modifications are minimal, focused, and maintain backward compatibility by defaulting fillItemDetail to true. This enhancement aligns well with the PR objectives and should improve API flexibility and potential performance.

To fully leverage this new feature:

  1. Ensure that the NamespaceOpenApiService implementation efficiently handles the fillItemDetail flag to avoid unnecessary data retrieval.
  2. Update the API documentation to clearly explain the new parameter and its performance implications.
  3. Consider adding metrics to track usage of this feature, which could inform future optimizations.

Also applies to: 85-87, 92-94

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java (1)

96-96: Approve change, but suggest improvements for flexibility and clarity.

The addition of the new boolean parameter aligns with the PR objectives to control item population. However, consider the following improvements:

  1. Instead of hardcoding true, introduce a configurable parameter to allow dynamic control over item population:
- namespaceService.loadNamespaceBO(appId, Env.valueOf(env), clusterName, namespaceName, true, false);
+ namespaceService.loadNamespaceBO(appId, Env.valueOf(env), clusterName, namespaceName, fillItemDetail, false);
  1. Add documentation explaining the purpose of the new fillItemDetail parameter.

  2. Update the NamespaceService import statement to reflect the new method signature, ensuring clarity for developers.

Would you like me to propose these changes in a more detailed manner?

apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/ConfigsExportServiceTest.java (1)

Line range hint 1-280: Overall assessment of changes in ConfigsExportServiceTest

The changes in this file address the addition of a new parameter (presumably fillItemDetail) to the namespaceService.findNamespaceBOs method. While this is a step in the right direction, there are opportunities to enhance the test coverage and clarity:

  1. The test method testNamespaceExportImport could be split into multiple test cases to cover different scenarios with the new parameter.
  2. Consider adding more assertions to verify the impact of the fillItemDetail parameter on the exported and imported data.
  3. Update the test class documentation to mention the new functionality being tested.

To improve the overall test coverage, consider adding the following test methods:

@Test
public void testNamespaceExportImportWithFillItemDetailTrue() {
    // Test scenario with fillItemDetail = true
}

@Test
public void testNamespaceExportImportWithFillItemDetailFalse() {
    // Test scenario with fillItemDetail = false
}

@Test
public void testNamespaceExportImportWithMixedFillItemDetail() {
    // Test scenario with a mix of true and false for fillItemDetail
}

These additional test methods would provide more comprehensive coverage of the new functionality and help ensure its correctness under various scenarios.

apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java (1)

Line range hint 268-272: LGTM! Consider adding a comment to explain the new test case.

The new test case effectively validates the behavior of loadNamespaceBO with different boolean parameters, aligning with the PR objectives. It correctly checks for a reduced number of items and modified count when not including deleted items.

Consider adding a brief comment above this new test case to explain what specific scenario it's testing. For example:

// Test loadNamespaceBO when includeDeleted is true and fillItemDetail is false
NamespaceBO namespaceBO2 = namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName, true, false);

This would improve the readability and maintainability of the test suite.

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java (4)

232-234: Confirm Default Parameter Values in Overloaded Method

In the overloaded method:

public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName) {
    return findNamespaceBOs(appId, env, clusterName, true, true);
}

The default values true, true are used for fillItemDetail and includeDeletedItems. Ensure that these defaults align with the expected behavior and do not introduce unintended side effects.


263-265: Verify Default Behavior in Overloaded loadNamespaceBO Method

In the overloaded method:

public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName,
    String namespaceName) {
    return loadNamespaceBO(appId, env, clusterName, namespaceName, true, true);
}

Confirm that using true, true as default values for fillItemDetail and includeDeletedItems is intended and verify that it doesn't adversely affect performance or functionality when detailed item information is unnecessary.


Line range hint 296-324: Initialize items List Even When fillItemDetail Is False

When fillItemDetail is false, the method returns early without populating items:

if (!fillItemDetail) {
    return namespaceBO;
}

Ensure that the items list is initialized to prevent potential NullPointerException issues in downstream code that may iterate over this list.

Apply this diff to initialize the items list:

 private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boolean fillItemDetail, boolean includeDeletedItems) {
     NamespaceBO namespaceBO = new NamespaceBO();
     namespaceBO.setBaseInfo(namespace);

     String appId = namespace.getAppId();
     String clusterName = namespace.getClusterName();
     String namespaceName = namespace.getNamespaceName();

     fillAppNamespaceProperties(namespaceBO);

+    // Initialize items list to prevent NullPointerException
+    namespaceBO.setItems(new LinkedList<>());

     if (!fillItemDetail) {
         return namespaceBO;
     }

354-356: Consistent Use of Default Parameters in transformNamespace2BO

The method overload:

private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace) {
    return transformNamespace2BO(env, namespace, true, true);
}

Ensure that defaulting fillItemDetail and includeDeletedItems to true is appropriate for all cases where this overload is used. If there are scenarios where less detail is sufficient, consider providing additional overloads or adjusting defaults as needed.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b32fcc6 and 71f9fa0.

📒 Files selected for processing (8)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java (1 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/NamespaceController.java (2 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ConfigsExportController.java (1 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ConfigsExportService.java (1 hunks)
  • apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java (7 hunks)
  • apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/ConfigsExportServiceTest.java (1 hunks)
  • apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/service/NamespaceServiceTest.java (1 hunks)
  • pom.xml (2 hunks)
🧰 Additional context used
🔇 Additional comments (8)
apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/server/service/ServerNamespaceOpenApiService.java (2)

Line range hint 1-99: Overall assessment: Changes are well-implemented and align with PR objectives.

The modifications to ServerNamespaceOpenApiService successfully introduce the fillItemDetail parameter to control item information retrieval for namespaces. This implementation aligns well with the PR objectives and the linked issue #5243.

Key points:

  1. Both getNamespace and getNamespaces methods have been consistently updated.
  2. The changes maintain backward compatibility.
  3. The new parameter is correctly integrated into the underlying service calls.

To further improve the code:

  1. Add Javadoc for the modified methods as suggested in previous comments.
  2. Ensure that all callers of these methods are updated to provide the fillItemDetail parameter.
  3. Consider updating the NamespaceOpenApiService interface (if it exists) to reflect these changes.

Great job on implementing this feature!


71-74: LGTM! Consider adding method documentation and verify usage.

The changes to the getNamespaces method are consistent with the PR objectives and the modifications made to getNamespace. The new fillItemDetail parameter is correctly integrated into the method signature and passed to the findNamespaceBOs call.

Consider adding Javadoc to document the method's purpose and the new fillItemDetail parameter. For example:

/**
 * Retrieves a list of namespaces.
 *
 * @param appId The application ID
 * @param env The environment
 * @param clusterName The cluster name
 * @param fillItemDetail Whether to include detailed item information for each namespace
 * @return A list of namespace data transfer objects
 */

Let's verify the impact of these changes on other parts of the codebase:

This script will help identify any locations where these methods are called, allowing us to ensure that all calls are updated to include the new fillItemDetail parameter.

✅ Verification successful

LGTM! The getNamespaces method is correctly updated with the fillItemDetail parameter, and its usages across the codebase have been verified.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usages of getNamespace and getNamespaces methods
# and ensure they are updated with the new fillItemDetail parameter.

# Search for getNamespace method calls
echo "Checking getNamespace method calls:"
rg --type java "getNamespace\s*\([^)]*\)" --glob "!**/ServerNamespaceOpenApiService.java"

echo "\nChecking getNamespaces method calls:"
rg --type java "getNamespaces\s*\([^)]*\)" --glob "!**/ServerNamespaceOpenApiService.java"

Length of output: 2937

apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/NamespaceController.java (1)

33-33: LGTM: Import statement added for new functionality.

The import of @RequestParam is necessary and consistent with the changes made to the method signatures.

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ConfigsExportService.java (1)

238-238: Verify the alignment of the new parameter with PR objectives.

The addition of the new boolean parameter to namespaceService.findNamespaceBOs is in line with the updated interface. However, there are a few points to consider:

  1. The use of true for the new parameter (presumably fillItemDetail) seems to contradict the PR objective of optimizing performance by not filling item data unnecessarily. Can you clarify why true is used here?

  2. It's important to ensure that this change is consistently applied across the codebase wherever namespaceService.findNamespaceBOs is called.

To verify the consistency of this change, please run the following script:

This script will help identify any inconsistencies in the usage of namespaceService.findNamespaceBOs across the codebase.

✅ Verification successful

All usages of namespaceService.findNamespaceBOs have been consistently updated with the new parameter. No instances of the old method signature were found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for all occurrences of namespaceService.findNamespaceBOs
# and verify if they have been updated with the new parameter.

# Search for all occurrences of namespaceService.findNamespaceBOs
echo "Occurrences of namespaceService.findNamespaceBOs:"
rg --type java "namespaceService\.findNamespaceBOs\(" -A 2

# Check if there are any occurrences with the old method signature (4 parameters)
echo "\nPotential occurrences with old method signature:"
rg --type java "namespaceService\.findNamespaceBOs\([^,]+,[^,]+,[^,]+,[^,]+\)"

Length of output: 4386

pom.xml (1)

66-66: Verify compatibility and stability with apollo-java 2.4.0-SNAPSHOT

The update of apollo-java.version from 2.2.0 to 2.4.0-SNAPSHOT is a significant change that warrants careful consideration:

  1. Compatibility: Ensure that this new version is compatible with the rest of the project and doesn't introduce breaking changes.
  2. Stability: Using a SNAPSHOT version in a production environment could lead to inconsistent builds. Consider using a stable release version instead.
  3. Features: Verify if this version update is necessary for implementing the new feature described in the PR objectives (openapi query namespace support).

To check for any potential issues, run the following commands:

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java (3)

309-312: Review Early Return Logic Based on fillItemDetail

The early return when fillItemDetail is false skips the population of item details:

if (!fillItemDetail) {
    return namespaceBO;
}

Verify that all consumers of NamespaceBO can handle scenarios where item details are absent. This is crucial to prevent any unintended exceptions or logic errors in parts of the application that expect these details.


Line range hint 189-208: Ensure All Calls to findNamespaceBOs Are Updated

The method findNamespaceBOs now includes the parameters boolean fillItemDetail and boolean includeDeletedItems. Please verify that all invocations of this method throughout the codebase have been updated to reflect the new signature to prevent any MethodNotFoundException at runtime.

Run the following script to locate all method calls to findNamespaceBOs:

#!/bin/bash
# Description: Find all invocations of findNamespaceBOs and check for correct parameters.

rg --type java 'findNamespaceBOs\s*\(' -A 2

253-258: Update Calls to loadNamespaceBO with New Parameters

The method loadNamespaceBO has been updated to include boolean fillItemDetail and boolean includeDeletedItems:

public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName,
    String namespaceName, boolean fillItemDetail, boolean includeDeletedItems)

Please ensure that all method invocations are updated accordingly to prevent runtime issues.

Run the following script to find all calls to loadNamespaceBO:

Comment on lines +149 to +152
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName1, true, false)).thenReturn(app1Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName2, true, false)).thenReturn(app1Cluster2Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName1, true, false)).thenReturn(app2Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName2, true, false)).thenReturn(app2Cluster2Namespace);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance test coverage for the new fillItemDetail parameter

The addition of the new boolean parameter (presumably fillItemDetail) to the namespaceService.findNamespaceBOs method calls is a good start, but the test coverage could be improved:

  1. Consider adding test cases where this parameter is set to false to ensure both scenarios are covered.
  2. Update the test method name (e.g., testNamespaceExportImportWithFillItemDetail) to reflect the new functionality being tested.
  3. Add assertions to verify the behavior differences when the new parameter is set to true vs false.

Here's a suggested refactor to improve test coverage:

@Test
public void testNamespaceExportImportWithFillItemDetail() throws FileNotFoundException {
    // ... (existing setup code)

    // Test with fillItemDetail = true
    testExportImportScenario(true);

    // Test with fillItemDetail = false
    testExportImportScenario(false);
}

private void testExportImportScenario(boolean fillItemDetail) throws FileNotFoundException {
    // ... (existing test logic)

    when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName1, fillItemDetail, false)).thenReturn(app1Cluster1Namespace);
    when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName2, fillItemDetail, false)).thenReturn(app1Cluster2Namespace);
    when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName1, fillItemDetail, false)).thenReturn(app2Cluster1Namespace);
    when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName2, fillItemDetail, false)).thenReturn(app2Cluster2Namespace);

    // ... (rest of the test logic)

    // Add assertions to verify behavior based on fillItemDetail value
    verify(namespaceService, times(4)).findNamespaceBOs(any(), any(), any(), eq(fillItemDetail), eq(false));
    // Add more specific assertions based on expected behavior when fillItemDetail is true vs false
}

This refactoring allows testing both scenarios and verifies that the fillItemDetail parameter is correctly passed to the findNamespaceBOs method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
1 participant