use rovo::aide::axum::IntoApiResponse;
use rovo::schemars::JsonSchema;
use rovo::{
extract::{Path, State},
http::StatusCode,
response::{IntoResponse, Json},
};
use rovo::{routing::get, rovo, Router};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Clone)]
#[allow(dead_code)]
struct AppState {
value: String,
}
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug, PartialEq)]
struct Item {
id: Uuid,
name: String,
}
impl Default for Item {
fn default() -> Self {
Self {
id: Uuid::nil(),
name: "Test Item".to_string(),
}
}
}
#[derive(Deserialize, JsonSchema)]
struct ItemId {
id: Uuid,
}
#[rovo]
async fn get_item(
State(_state): State<AppState>,
Path(ItemId { id }): Path<ItemId>,
) -> impl IntoApiResponse {
if id == Uuid::nil() {
Json(Item::default()).into_response()
} else {
StatusCode::NOT_FOUND.into_response()
}
}
#[rovo]
async fn simple_handler(State(_state): State<AppState>) -> impl IntoApiResponse {
Json("ok".to_string())
}
#[rovo]
async fn multi_response(State(_state): State<AppState>) -> impl IntoApiResponse {
Json(Item::default())
}
#[test]
fn test_macro_generates_docs_function() {
let state = AppState {
value: "test".to_string(),
};
let _router: ::axum::Router = Router::<AppState>::new()
.route("/items/{id}", get(get_item))
.route("/simple", get(simple_handler))
.route("/multi", get(multi_response))
.with_state(state)
.finish();
}
#[test]
fn test_docs_function_callable() {
use rovo::aide::openapi::Operation;
use rovo::aide::transform::TransformOperation;
let mut operation = Operation::default();
let transform = TransformOperation::new(&mut operation);
let _result = get_item::__docs(transform);
}
#[test]
fn test_multiple_handlers_compile() {
let state = AppState {
value: "test".to_string(),
};
let _router: ::axum::Router = Router::<AppState>::new()
.route("/a", get(simple_handler))
.route("/b", get(multi_response))
.route("/c/{id}", get(get_item))
.with_state(state)
.finish();
}
#[test]
fn test_handler_with_path_params() {
let state = AppState {
value: "test".to_string(),
};
let _router: ::axum::Router = Router::<AppState>::new()
.route("/items/{id}", get(get_item))
.with_state(state)
.finish();
}