Skip to content

Commit a095082

Browse files
committed
controllers/krate/search: Use basic utoipa annotation
1 parent f9176c8 commit a095082

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

src/controllers/krate/search.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,35 @@ use crate::models::krate::ALL_COLUMNS;
2424
use crate::sql::{array_agg, canon_crate_name, lower};
2525
use crate::util::RequestUtils;
2626

27-
/// Handles the `GET /crates` route.
28-
/// Returns a list of crates. Called in a variety of scenarios in the
29-
/// front end, including:
27+
/// Returns a list of crates.
28+
///
29+
/// Called in a variety of scenarios in the front end, including:
3030
/// - Alphabetical listing of crates
3131
/// - List of crates under a specific owner
3232
/// - Listing a user's followed crates
33-
///
34-
/// Notes:
35-
/// The different use cases this function covers is handled through passing
36-
/// in parameters in the GET request.
37-
///
38-
/// We would like to stop adding functionality in here. It was built like
39-
/// this to keep the number of database queries low, though given Rust's
40-
/// low performance overhead, this is a soft goal to have, and can afford
41-
/// more database transactions if it aids understandability.
42-
///
43-
/// All of the edge cases for this function are not currently covered
44-
/// in testing, and if they fail, it is difficult to determine what
45-
/// caused the break. In the future, we should look at splitting this
46-
/// function out to cover the different use cases, and create unit tests
47-
/// for them.
33+
#[utoipa::path(
34+
get,
35+
path = "/api/v1/crates",
36+
operation_id = "crates_list",
37+
tag = "crates",
38+
responses((status = 200, description = "Successful Response")),
39+
)]
4840
pub async fn search(app: AppState, req: Parts) -> AppResult<ErasedJson> {
41+
// Notes:
42+
// The different use cases this function covers is handled through passing
43+
// in parameters in the GET request.
44+
//
45+
// We would like to stop adding functionality in here. It was built like
46+
// this to keep the number of database queries low, though given Rust's
47+
// low performance overhead, this is a soft goal to have, and can afford
48+
// more database transactions if it aids understandability.
49+
//
50+
// All of the edge cases for this function are not currently covered
51+
// in testing, and if they fail, it is difficult to determine what
52+
// caused the break. In the future, we should look at splitting this
53+
// function out to cover the different use cases, and create unit tests
54+
// for them.
55+
4956
let mut conn = app.db_read().await?;
5057

5158
use diesel::sql_types::Float;

src/router.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use axum::response::IntoResponse;
33
use axum::routing::{delete, get, post, put};
44
use axum::{Json, Router};
55
use http::{Method, StatusCode};
6+
use utoipa_axum::routes;
67

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

1617
pub fn build_axum_router(state: AppState) -> Router<()> {
17-
let (router, openapi) = BaseOpenApi::router().split_for_parts();
18+
let (router, openapi) = BaseOpenApi::router()
19+
.routes(routes!(
20+
// Route used by both `cargo search` and the frontend
21+
krate::search::search
22+
))
23+
.split_for_parts();
1824

1925
let mut router = router
20-
// Route used by both `cargo search` and the frontend
21-
.route("/api/v1/crates", get(krate::search::search))
2226
// Routes used by `cargo`
2327
.route(
2428
"/api/v1/crates/new",

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

+17-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,23 @@ snapshot_kind: text
1919
"version": "0.0.0"
2020
},
2121
"openapi": "3.1.0",
22-
"paths": {},
22+
"paths": {
23+
"/api/v1/crates": {
24+
"get": {
25+
"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",
26+
"operationId": "crates_list",
27+
"responses": {
28+
"200": {
29+
"description": "Successful Response"
30+
}
31+
},
32+
"summary": "Returns a list of crates.",
33+
"tags": [
34+
"crates"
35+
]
36+
}
37+
}
38+
},
2339
"servers": [
2440
{
2541
"url": "https://crates.io"

0 commit comments

Comments
 (0)