-
Notifications
You must be signed in to change notification settings - Fork 1
Add UI-only route tests and remove unused mut from mount_route_ui #42
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request adds support for UI/SSR routes that are mounted to the router but excluded from the OpenAPI specification. It introduces route_ui and mount_route_ui methods as counterparts to existing route and mount_route methods, along with comprehensive test coverage.
Changes:
- Added
route_uiandmount_route_uimethods to allow mounting UI/SSR endpoints without OpenAPI registration - Added helper method
route_with_method_uithat mirrorsroute_with_methodbut skips OpenAPI registration - Added unit tests
test_route_ui_excludes_openapiandtest_mount_route_ui_excludes_openapito verify the new functionality - Updated documentation in
docs/README.mdwith examples showing the distinction between API and UI routes
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| docs/README.md | Added example demonstrating UI route usage with route_ui and clarified OpenAPI inclusion behavior |
| crates/rustapi-core/src/app.rs | Added route_ui, mount_route_ui, and route_with_method_ui methods with corresponding unit tests to support UI-only routes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /// Helper to mount a single method handler without OpenAPI registration | ||
| fn route_with_method_ui( | ||
| self, | ||
| path: &str, | ||
| method: http::Method, | ||
| handler: crate::handler::BoxedHandler, | ||
| ) -> Self { | ||
| use crate::router::MethodRouter; | ||
|
|
||
| let path = if !path.starts_with('/') { | ||
| format!("/{}", path) | ||
| } else { | ||
| path.to_string() | ||
| }; |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This helper function duplicates the path normalization logic from route_with_method. Consider extracting the path normalization into a shared helper function to avoid code duplication and ensure consistency.
| let method_enum = match route.method { | ||
| "GET" => http::Method::GET, | ||
| "POST" => http::Method::POST, | ||
| "PUT" => http::Method::PUT, | ||
| "DELETE" => http::Method::DELETE, | ||
| "PATCH" => http::Method::PATCH, | ||
| _ => http::Method::GET, | ||
| }; |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method-to-enum conversion logic is duplicated across multiple functions (mount_auto_routes_grouped, mount_route, and now mount_route_ui). Consider extracting this into a helper function to reduce duplication and ensure consistency in how method strings are converted to HTTP method enums.
|
|
||
| /// Add a UI route without registering OpenAPI operations. | ||
| /// | ||
| /// Use this for SSR or UI-only endpoints that should not appear in OpenAPI. |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation for route_ui could be improved by adding an example section similar to the route method. Consider adding a code example showing how to use this method for UI/SSR endpoints.
| /// Use this for SSR or UI-only endpoints that should not appear in OpenAPI. | |
| /// Use this for SSR or UI-only endpoints that should not appear in OpenAPI. | |
| /// | |
| /// # Example | |
| /// | |
| /// ```rust,ignore | |
| /// use rustapi_rs::prelude::*; | |
| /// | |
| /// async fn ui_index() -> impl IntoResponse { | |
| /// Html("<html><body><h1>Welcome</h1></body></html>") | |
| /// } | |
| /// | |
| /// #[tokio::main] | |
| /// async fn main() -> Result<()> { | |
| /// RustApi::new() | |
| /// // API route registered in OpenAPI | |
| /// .route("/api/health", get(health_check)) | |
| /// // UI route not included in OpenAPI | |
| /// .route_ui("/", get(ui_index)) | |
| /// .run("127.0.0.1:8080") | |
| /// .await | |
| /// } | |
| /// ``` |
|
|
||
| /// Mount a UI route created with `#[rustapi::get]`, `#[rustapi::post]`, etc. | ||
| /// | ||
| /// This skips OpenAPI registration while still mounting the handler. |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation for mount_route_ui could be improved by adding an example section similar to the mount_route method above. Consider adding a code example showing how to use this method with macro-generated routes.
| /// This skips OpenAPI registration while still mounting the handler. | |
| /// This skips OpenAPI registration while still mounting the handler. | |
| /// | |
| /// # Example | |
| /// | |
| /// ```rust,ignore | |
| /// use rustapi_rs::prelude::*; | |
| /// | |
| /// struct AppState; | |
| /// | |
| /// #[rustapi::get("/ui")] | |
| /// async fn ui_home(state: State<AppState>) -> impl IntoResponse { | |
| /// // Render your UI here | |
| /// "Hello from UI" | |
| /// } | |
| /// | |
| /// #[tokio::main] | |
| /// async fn main() -> Result<()> { | |
| /// RustApi::new() | |
| /// .state(AppState) | |
| /// // Mount a UI route without registering it in the OpenAPI spec | |
| /// .mount_route_ui(route!(ui_home)) | |
| /// .run("127.0.0.1:8080") | |
| /// .await | |
| /// } | |
| /// ``` |
Motivation
route_ui/mount_route_uiare registered with the router but are excluded from the OpenAPI spec.mutfrom themount_route_uisignature to remove a compiler warning and tidy the API.Description
test_route_ui_excludes_openapiandtest_mount_route_ui_excludes_openapitocrates/rustapi-core/src/app.rswhich assert UI routes are mounted whileopenapi_spec().pathsremains empty.crate::handler::Routeinto the test module and adjusted test assertions to checkapp.openapi_spec()before callinginto_router().mutfrom thepub fn mount_route_ui(self, ...)signature to address anunused_mutwarning.Testing
cargo test -p rustapi-core --liband all tests completed successfully with205 passed; 0 failed.Codex Task