Skip to content

Conversation

Teebor-Choka
Copy link
Contributor

@Teebor-Choka Teebor-Choka commented Dec 11, 2024

Minor improvements:

  • Fix missing schemas in the spec
  • Conform to the DELETE operation to not accept a body.
  • Add basic integration test for the openapi spec validation

Note

Fixes #6727
Needs #6720 (vacuum-go) package

@Teebor-Choka Teebor-Choka added this to the 2.2.0-rc.1 milestone Dec 11, 2024
@Teebor-Choka Teebor-Choka requested a review from a team December 11, 2024 18:33
@Teebor-Choka Teebor-Choka self-assigned this Dec 11, 2024
Copy link
Contributor

coderabbitai bot commented Dec 11, 2024

Warning

Rate limit exceeded

@Teebor-Choka has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 14 minutes and 49 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 077d209 and bb6e604.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • hoprd/rest-api/src/lib.rs (2 hunks)
  • tests/hopr.py (1 hunks)
  • transport/api/src/lib.rs (1 hunks)
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

This pull request introduces enhancements to the Hopr REST API, focusing on improving OpenAPI documentation, session management, and routing capabilities. The changes primarily involve updating the hoprd/rest-api crate, including modifications to type definitions, API routes, and schema generation. New types like SessionCapability and RoutingOptions have been added to extend the API's interface, and the OpenAPI documentation has been refined to include these new components.

Changes

File Change Summary
hoprd/rest-api/src/lib.rs Updated OpenAPI documentation, added new types SessionCapability and RoutingOptions, modified route for closing session client
hoprd/rest-api/src/session.rs Added new enums SessionCapability and RoutingOptions, updated structs with new fields and schema annotations
tests/hopr.py Simplified session_close_client method, removed default parameter for bound_ip
hoprd/rest-api/Cargo.toml Updated package version to 3.9.0, upgraded dependencies like utoipa, added new dev dependencies
hoprd/rest-api/src/types.rs Added schema annotations to PeerOrAddress enum variants
hoprd/rest-api/tests/test_openapi_spec_validation.rs Added new test function to validate OpenAPI specification

Assessment against linked issues

Objective Addressed Explanation
Missing schemas in swagger-ui [#6727]

Possibly related PRs

Suggested labels

bug, crate:hopr-lib

Suggested reviewers

  • NumberFour8

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@Teebor-Choka Teebor-Choka force-pushed the kauki/rest/fix-missing-schemas branch from 52998ed to d5d6412 Compare December 11, 2024 18:33
@Teebor-Choka Teebor-Choka marked this pull request as ready for review December 11, 2024 18:33
@github-actions github-actions bot added dependencies Pull requests that update a dependency file testing Related to tests crate:hoprd-api labels Dec 11, 2024
@Teebor-Choka Teebor-Choka force-pushed the kauki/rest/fix-missing-schemas branch 2 times, most recently from d41a93c to 0bf5f48 Compare December 11, 2024 18:35
@github-actions github-actions bot removed the dependencies Pull requests that update a dependency file label Dec 11, 2024
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 (2)
hoprd/rest-api/src/session.rs (1)

100-104: Consider removing #[repr(u8)] from SessionCapability enum

The #[repr(u8)] attribute on the SessionCapability enum may not be necessary since the enum variants are already unit-like and serialization is handled by Serde. Keeping #[repr(u8)] could lead to unintended behaviors during serialization or when adding new variants in the future. Removing it reduces potential issues and aligns with Rust's best practices for enums.

hoprd/rest-api/src/lib.rs (1)

Line range hint 1-424: Consider adding response examples to the OpenAPI documentation.

While the schema definitions have been added, consider enhancing the documentation by adding example responses for the session-related endpoints. This would improve the swagger-ui experience.

Example enhancement for SessionClientResponse:

 #[derive(Debug, Clone, Serialize, utoipa::ToSchema)]
+#[schema(example = json!({
+    "id": "12345",
+    "status": "connected",
+    "target": {
+        "address": "127.0.0.1:8000",
+        "protocol": "tcp"
+    }
+}))]
 pub struct SessionClientResponse {
     // existing fields...
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4708a45 and 0bf5f48.

📒 Files selected for processing (3)
  • hoprd/rest-api/src/lib.rs (1 hunks)
  • hoprd/rest-api/src/session.rs (12 hunks)
  • tests/hopr.py (1 hunks)
🔇 Additional comments (2)
tests/hopr.py (1)

500-502: Ensure proper parameter passing when calling session_close_client

The updated session_close_client method now directly passes ip and port to the __call_api method. Verify that the SessionApi client expects these parameters as path parameters and that they are correctly passed in the API call. This ensures that the method functions as intended with the updated API endpoint.

hoprd/rest-api/src/lib.rs (1)

150-150: LGTM! The schema additions align with PR objectives.

The addition of session-related schemas (SessionClientRequest, SessionCapability, RoutingOptions, SessionTargetSpec, SessionClientResponse) to the OpenAPI documentation addresses the missing schemas issue mentioned in #6727.

Let's verify that the DELETE operation for session endpoints doesn't accept a request body:

✅ Verification successful

DELETE operation conforms to standards - no request body found

After reviewing the implementation in session.rs, I can confirm that the DELETE operation for closing sessions (close_client function) is properly implemented and does not accept a request body. The function only takes path parameters (protocol, ip, and port) to identify the session to close, which aligns with REST API best practices.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify DELETE operation conformance in OpenAPI spec
# Expected: DELETE operations should not have requestBody

# Search for DELETE operation definitions in the OpenAPI spec
ast-grep --pattern 'delete($$$).route("/session/$$$", $$$)'

# Search for any DELETE operations that might have request bodies
rg -A 5 'delete\(' hoprd/rest-api/src/session.rs

Length of output: 251


Script:

#!/bin/bash
# Let's try a different approach to find DELETE operations in the codebase

# Search for DELETE operations in the REST API implementation
rg -i "delete.*session" hoprd/rest-api/src/lib.rs

# Look for route handlers in session.rs
rg "fn.*delete" hoprd/rest-api/src/session.rs

# Check the actual implementation of session-related operations
cat hoprd/rest-api/src/session.rs

Length of output: 39094

@Teebor-Choka Teebor-Choka force-pushed the kauki/rest/fix-missing-schemas branch from 0bf5f48 to ea45aec Compare December 11, 2024 22:52
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: 0

🧹 Outside diff range and nitpick comments (3)
hoprd/rest-api/src/session.rs (3)

314-333: Add documentation for the RoutingOptions enum.

While the implementation is correct, adding documentation would improve API clarity:

  • Purpose of each variant
  • When to use IntermediatePath vs Hops
  • Impact of the explicit-path feature flag
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
+/// Options for configuring the routing behavior of a session.
 pub enum RoutingOptions {
     #[cfg(feature = "explicit-path")]
     #[schema(value_type = Vec<String>)]
+    /// Route through specific intermediate peers in the given order.
     IntermediatePath(#[serde_as(as = "Vec<DisplayFromStr>")] Vec<PeerId>),
+    /// Route through the specified number of random intermediate hops.
     Hops(usize),
 }

693-712: Add documentation for the IpProtocol enum.

The enum implementation is correct, but adding documentation would improve API clarity:

  • Purpose of the enum
  • When to use each protocol variant
  • Impact on session behavior
 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, strum::Display, strum::EnumString, utoipa::ToSchema)]
 #[strum(serialize_all = "lowercase", ascii_case_insensitive)]
 #[serde(rename_all = "lowercase")]
+/// Supported IP protocols for session communication.
 pub enum IpProtocol {
     #[allow(clippy::upper_case_acronyms)]
+    /// Transmission Control Protocol - reliable, ordered delivery
     TCP,
     #[allow(clippy::upper_case_acronyms)]
+    /// User Datagram Protocol - unreliable, unordered delivery
     UDP,
 }

917-917: Enhance test coverage for routing options.

The tests use Default::default() for IntermediatePath, which might not adequately test all routing scenarios. Consider adding test cases for:

  • Different path lengths
  • Various hop counts
  • Invalid path configurations

Also applies to: 939-939, 982-982

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0bf5f48 and ea45aec.

📒 Files selected for processing (3)
  • hoprd/rest-api/src/lib.rs (1 hunks)
  • hoprd/rest-api/src/session.rs (18 hunks)
  • tests/hopr.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/hopr.py
  • hoprd/rest-api/src/lib.rs
🔇 Additional comments (4)
hoprd/rest-api/src/session.rs (4)

100-124: LGTM! Well-structured enum with clear documentation.

The SessionCapability enum and its implementation are well-designed with:

  • Clear documentation for each variant
  • Proper trait derivations including utoipa::ToSchema for OpenAPI spec
  • Clean conversion implementation to hopr_lib::SessionCapability

714-720: LGTM! Proper implementation of query parameters for DELETE operation.

The SessionCloseClientQuery struct properly encapsulates the path parameters for the close operation, aligning with REST standards and fixing the previous implementation issues.


746-748: LGTM! Improved path parameter handling.

The updated implementation correctly uses a single Path extractor with a structured type, addressing the previous concerns about multiple path parameter extraction.


56-56: LGTM! OpenAPI schema improvements are complete.

All necessary types now properly derive utoipa::ToSchema and include appropriate schema annotations:

  • SessionTargetSpec
  • SessionCapability
  • RoutingOptions
  • IpProtocol
  • SessionCloseClientQuery

This addresses the original issue of missing schemas in swagger-ui.

Also applies to: 101-103, 314-315, 694-695, 714-715

Copy link
Contributor

@tolbrino tolbrino left a comment

Choose a reason for hiding this comment

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

crate version bump missing

Copy link
Contributor

@NumberFour8 NumberFour8 left a comment

Choose a reason for hiding this comment

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

See comment - make sure that it builds also without the explicit-path feature enabled.

@Teebor-Choka Teebor-Choka force-pushed the kauki/rest/fix-missing-schemas branch 2 times, most recently from 4b2a16b to bfe9e97 Compare December 13, 2024 20:56
@Teebor-Choka Teebor-Choka force-pushed the kauki/rest/fix-missing-schemas branch 2 times, most recently from 4c04ab3 to e54ba71 Compare December 13, 2024 21:46
@NumberFour8
Copy link
Contributor

There's a failure in E2E tests:

ERROR hopr-api:hopr.py:348 No param peer_id found for peers

@Teebor-Choka
Copy link
Contributor Author

Teebor-Choka commented Dec 14, 2024

There's a failure in E2E tests:

ERROR hopr-api:hopr.py:348 No param peer_id found for peers

It's actually a bug from the previous utoipa, where the serde(to_camel_case) was not considered, but it is now. We will have to fix our code to be backwards compatible.

Now the peerId is expected instead of the peer_id, but peer_id should be there, because that's the API contract. The amount of places this change broke our setup is enormous. I may consider downgrading utoipa in 2.2. just for that single reason. But then, other parts of the behavior, that wasn't there before, will be missed.

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: 0

🧹 Nitpick comments (3)
Makefile (1)

105-105: LGTM! Improved process termination signal.

The change from default kill to kill -s INT is a better practice as it sends an interrupt signal (SIGINT) instead of termination signal (SIGTERM), allowing the process to handle the shutdown more gracefully. This is particularly appropriate for server processes like anvil.

tests/utils.py (1)

55-55: Improve code readability by breaking long lines

The change to use get() with a default value is a good defensive programming practice. However, the lines exceed the maximum length of 120 characters.

Consider breaking the lines for better readability:

-        oc for oc in all_channels.all if oc.get('source_address', None) == src.address and oc.get('destination_address', None) == dest.address
+        oc for oc in all_channels.all
+        if (oc.get('source_address', None) == src.address and
+            oc.get('destination_address', None) == dest.address)

Also applies to: 64-64

🧰 Tools
🪛 Ruff (0.8.2)

55-55: Line too long (142 > 120)

(E501)

tests/hopr.py (1)

347-348: Improve code style in membership test

The membership test can be written more idiomatically.

-                if not param in getattr(response, status)[0]:
+                if param not in getattr(response, status)[0]:
🧰 Tools
🪛 Ruff (0.8.2)

347-347: Test for membership should be not in

Convert to not in

(E713)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4c04ab3 and c27948c.

📒 Files selected for processing (6)
  • Makefile (1 hunks)
  • tests/conftest.py (1 hunks)
  • tests/hopr.py (4 hunks)
  • tests/test_hopli.py (1 hunks)
  • tests/test_integration.py (6 hunks)
  • tests/utils.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/conftest.py
  • tests/test_integration.py
🧰 Additional context used
🪛 Ruff (0.8.2)
tests/utils.py

55-55: Line too long (142 > 120)

(E501)


64-64: Line too long (143 > 120)

(E501)

tests/hopr.py

347-347: Test for membership should be not in

Convert to not in

(E713)

🔇 Additional comments (3)
tests/hopr.py (2)

135-138: LGTM! Consistent with API changes

The update from peer_id to peerId aligns with the API's camelCase naming convention.


517-519: LGTM! Improved API conformance

The removal of the empty body parameter aligns with REST standards for DELETE operations, and the improved formatting enhances readability.

tests/test_hopli.py (1)

469-469: LGTM! Verify test coverage after parameter removal.

The removal of the swarm7 parameter simplifies the test's dependencies while maintaining its core functionality of testing safe module creation.

Let's verify that the test coverage remains comprehensive:

@Teebor-Choka Teebor-Choka force-pushed the kauki/rest/fix-missing-schemas branch from a0a717c to fab421a Compare December 17, 2024 09:15
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: 0

🧹 Nitpick comments (2)
hoprd/rest-api/src/session.rs (2)

119-143: Document TCP-specific assumptions in capabilities.

Based on past learnings, the current implementation assumes TCP-like behavior for sessions. Consider adding documentation to clarify which capabilities are applicable to different session types (TCP vs UDP).

Add documentation like this:

 #[repr(u8)]
 #[derive(
     Debug, Clone, strum::EnumIter, strum::Display, strum::EnumString, Serialize, Deserialize, utoipa::ToSchema,
 )]
+/// Session capabilities that can be enabled for a connection.
+/// Note: Some capabilities are primarily designed for TCP-like sessions
+/// and may not be applicable to all session types.
 pub enum SessionCapability {

402-420: Optimize capabilities conversion and defaults.

The current implementation could be more efficient by avoiding the intermediate Vec allocation.

Consider this optimization:

-            capabilities: self
-                .capabilities
-                .map(|vs| {
-                    vs.into_iter()
-                        .map(|v| {
-                            let cap: hopr_lib::SessionCapability = v.into();
-                            cap
-                        })
-                        .collect::<Vec<_>>()
-                })
-                .unwrap_or_else(|| match target_protocol {
-                    IpProtocol::TCP => {
-                        vec![
-                            hopr_lib::SessionCapability::Retransmission,
-                            hopr_lib::SessionCapability::Segmentation,
-                        ]
-                    }
-                    _ => vec![], // no default capabilities for UDP, etc.
-                }),
+            capabilities: self.capabilities
+                .map(|vs| vs.into_iter().map(Into::into).collect())
+                .unwrap_or_else(|| match target_protocol {
+                    IpProtocol::TCP => vec![
+                        hopr_lib::SessionCapability::Retransmission,
+                        hopr_lib::SessionCapability::Segmentation,
+                    ],
+                    _ => Vec::new(),
+                }),
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a0a717c and fab421a.

📒 Files selected for processing (2)
  • hoprd/rest-api/src/session.rs (18 hunks)
  • tests/hopr.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/hopr.py
🧰 Additional context used
📓 Learnings (1)
hoprd/rest-api/src/session.rs (1)
Learnt from: Teebor-Choka
PR: hoprnet/hoprnet#6393
File: hoprd/rest-api/src/session.rs:91-91
Timestamp: 2024-11-12T10:56:01.095Z
Learning: The current implementation in `hoprd/rest-api/src/session.rs` assumes all sessions are TCP-like due to the missing implementation of UDP behavior. The commented-out code suggests an intention to support different session types based on `capabilities`.
🔇 Additional comments (6)
hoprd/rest-api/src/session.rs (6)

29-35: LGTM: Import changes align with new session capabilities.

The new imports support the added session management functionality and are used appropriately throughout the file.


56-57: LGTM: Enhanced OpenAPI documentation.

The addition of utoipa::ToSchema improves API documentation by generating proper OpenAPI schemas for the SessionTargetSpec enum.


332-353: Add feature-gate checks for IntermediatePath variant.

The feature-gate handling needs to be updated to ensure proper error handling when the explicit-path feature is disabled.


708-727: LGTM: Clean protocol enum implementation.

The IpProtocol enum implementation is well-structured with appropriate derive macros and a clean From trait implementation.


729-735: LGTM: Fixed path parameter extraction.

The implementation correctly handles path parameters using a dedicated struct, addressing the previous issues with parameter extraction.

Also applies to: 763-778


151-152: Verify backward compatibility with peer_id parameter.

Based on the PR comments, there was an E2E test failure related to the peer_id parameter due to camelCase conversion. Ensure that both peer_id and peerId forms are supported for backward compatibility.

Also applies to: 194-198

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: 0

🧹 Nitpick comments (1)
tests/conftest.py (1)

427-428: LGTM! Good addition of timeout protection.

Adding timeout protection to the node connectivity check is a good improvement that prevents tests from hanging indefinitely if nodes fail to connect.

Consider these improvements:

  1. Extract the timeout value (60) to a constant at the top of the file for better maintainability
  2. Add more detailed error logging when timeout occurs, for example:
-    nodes_connectivity = await asyncio.wait_for(asyncio.gather(*tasks), timeout)
+    try:
+        nodes_connectivity = await asyncio.wait_for(asyncio.gather(*tasks), timeout)
+    except asyncio.TimeoutError:
+        logging.error(f"Node connectivity check timed out after {timeout} seconds")
+        raise
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1d5052e and 077d209.

📒 Files selected for processing (3)
  • hoprd/rest-api/src/lib.rs (2 hunks)
  • tests/conftest.py (1 hunks)
  • tests/hopr.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/hopr.py
  • hoprd/rest-api/src/lib.rs
🔇 Additional comments (1)
tests/conftest.py (1)

Line range hint 33-34: Verify parameter naming compatibility with API changes.

Given the reported changes in parameter naming from peer_id to peerId due to the utoipa library update, we should verify if this code needs to be updated for compatibility.

Let's check the API response format:

@Teebor-Choka Teebor-Choka merged commit 65abeda into master Dec 17, 2024
28 checks passed
@Teebor-Choka Teebor-Choka deleted the kauki/rest/fix-missing-schemas branch December 17, 2024 12:15
@coderabbitai coderabbitai bot mentioned this pull request Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:api crate:core-transport crate:hoprd-api dependencies Pull requests that update a dependency file testing Related to tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some schema are missing in swagger-ui
3 participants