Skip to content

Commit cd60138

Browse files
committed
controllers/krate/search: Use utoipa annotations
1 parent 58fc8ae commit cd60138

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

src/controllers/krate/search.rs

Lines changed: 25 additions & 18 deletions
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

Lines changed: 7 additions & 3 deletions
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::Router;
55
use http::{Method, StatusCode};
6+
use utoipa_axum::routes;
67
use utoipa_swagger_ui::SwaggerUi;
78

89
use crate::app::AppState;
@@ -15,11 +16,14 @@ use crate::Env;
1516
const MAX_PUBLISH_CONTENT_LENGTH: usize = 128 * 1024 * 1024; // 128 MB
1617

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

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

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 17 additions & 1 deletion
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)