Skip to content

Commit

Permalink
controllers/krate/search: Use utoipa annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Dec 12, 2024
1 parent f9176c8 commit b1bf412
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
43 changes: 25 additions & 18 deletions src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,35 @@ use crate::models::krate::ALL_COLUMNS;
use crate::sql::{array_agg, canon_crate_name, lower};
use crate::util::RequestUtils;

/// Handles the `GET /crates` route.
/// Returns a list of crates. Called in a variety of scenarios in the
/// front end, including:
/// Returns a list of crates.
///
/// Called in a variety of scenarios in the front end, including:
/// - Alphabetical listing of crates
/// - List of crates under a specific owner
/// - Listing a user's followed crates
///
/// Notes:
/// The different use cases this function covers is handled through passing
/// in parameters in the GET request.
///
/// We would like to stop adding functionality in here. It was built like
/// this to keep the number of database queries low, though given Rust's
/// low performance overhead, this is a soft goal to have, and can afford
/// more database transactions if it aids understandability.
///
/// All of the edge cases for this function are not currently covered
/// in testing, and if they fail, it is difficult to determine what
/// caused the break. In the future, we should look at splitting this
/// function out to cover the different use cases, and create unit tests
/// for them.
#[utoipa::path(
get,
path = "/api/v1/crates",
operation_id = "crates_list",
tag = "crates",
responses((status = 200, description = "Successful Response")),
)]
pub async fn search(app: AppState, req: Parts) -> AppResult<ErasedJson> {
// Notes:
// The different use cases this function covers is handled through passing
// in parameters in the GET request.
//
// We would like to stop adding functionality in here. It was built like
// this to keep the number of database queries low, though given Rust's
// low performance overhead, this is a soft goal to have, and can afford
// more database transactions if it aids understandability.
//
// All of the edge cases for this function are not currently covered
// in testing, and if they fail, it is difficult to determine what
// caused the break. In the future, we should look at splitting this
// function out to cover the different use cases, and create unit tests
// for them.

let mut conn = app.db_read().await?;

use diesel::sql_types::Float;
Expand Down
10 changes: 7 additions & 3 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use axum::response::IntoResponse;
use axum::routing::{delete, get, post, put};
use axum::{Json, Router};
use http::{Method, StatusCode};
use utoipa_axum::routes;

use crate::app::AppState;
use crate::controllers::user::update_user;
Expand All @@ -14,11 +15,14 @@ use crate::Env;
const MAX_PUBLISH_CONTENT_LENGTH: usize = 128 * 1024 * 1024; // 128 MB

pub fn build_axum_router(state: AppState) -> Router<()> {
let (router, openapi) = BaseOpenApi::router().split_for_parts();
let (router, openapi) = BaseOpenApi::router()
.routes(routes!(
// Route used by both `cargo search` and the frontend
krate::search::search
))
.split_for_parts();

let mut router = router
// Route used by both `cargo search` and the frontend
.route("/api/v1/crates", get(krate::search::search))
// Routes used by `cargo`
.route(
"/api/v1/crates/new",
Expand Down
18 changes: 17 additions & 1 deletion src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@ snapshot_kind: text
"version": "0.0.0"
},
"openapi": "3.1.0",
"paths": {},
"paths": {
"/api/v1/crates": {
"get": {
"description": "Called in a variety of scenarios in the front end, including:\n- Alphabetical listing of crates\n- List of crates under a specific owner\n- Listing a user's followed crates",
"operationId": "crates_list",
"responses": {
"200": {
"description": "Successful Response"
}
},
"summary": "Returns a list of crates.",
"tags": [
"crates"
]
}
}
},
"servers": [
{
"url": "https://crates.io"
Expand Down

0 comments on commit b1bf412

Please sign in to comment.