Skip to content

Commit daa58ad

Browse files
committed
Improve OpenAPI documentation for GET /api/v1/crates/{name}/reverse_dependencies endpoints
1 parent 64b9338 commit daa58ad

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

src/controllers/krate/rev_deps.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,43 @@ use crate::controllers::krate::CratePath;
44
use crate::models::{CrateName, User, Version, VersionOwnerAction};
55
use crate::util::errors::AppResult;
66
use crate::views::{EncodableDependency, EncodableVersion};
7-
use axum_extra::json;
8-
use axum_extra::response::ErasedJson;
7+
use axum::Json;
98
use crates_io_database::schema::{crates, users, versions};
109
use diesel::prelude::*;
1110
use diesel_async::RunQueryDsl;
1211
use http::request::Parts;
1312

13+
#[derive(Debug, Serialize, utoipa::ToSchema)]
14+
pub struct RevDepsResponse {
15+
/// The list of reverse dependencies of the crate.
16+
dependencies: Vec<EncodableDependency>,
17+
18+
/// The versions referenced in the `dependencies` field.
19+
versions: Vec<EncodableVersion>,
20+
21+
#[schema(inline)]
22+
meta: RevDepsMeta,
23+
}
24+
25+
#[derive(Debug, Serialize, utoipa::ToSchema)]
26+
pub struct RevDepsMeta {
27+
#[schema(example = 32)]
28+
total: i64,
29+
}
30+
1431
/// List reverse dependencies of a crate.
1532
#[utoipa::path(
1633
get,
1734
path = "/api/v1/crates/{name}/reverse_dependencies",
1835
params(CratePath),
1936
tag = "crates",
20-
responses((status = 200, description = "Successful Response")),
37+
responses((status = 200, description = "Successful Response", body = inline(RevDepsResponse))),
2138
)]
2239
pub async fn list_reverse_dependencies(
2340
app: AppState,
2441
path: CratePath,
2542
req: Parts,
26-
) -> AppResult<ErasedJson> {
43+
) -> AppResult<Json<RevDepsResponse>> {
2744
let mut conn = app.db_read().await?;
2845

2946
let pagination_options = PaginationOptions::builder().gather(&req)?;
@@ -64,9 +81,9 @@ pub async fn list_reverse_dependencies(
6481
})
6582
.collect::<Vec<_>>();
6683

67-
Ok(json!({
68-
"dependencies": rev_deps,
69-
"versions": versions,
70-
"meta": { "total": total },
84+
Ok(Json(RevDepsResponse {
85+
dependencies: rev_deps,
86+
versions,
87+
meta: RevDepsMeta { total },
7188
}))
7289
}

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

+41
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,47 @@ expression: response.json()
22882288
],
22892289
"responses": {
22902290
"200": {
2291+
"content": {
2292+
"application/json": {
2293+
"schema": {
2294+
"properties": {
2295+
"dependencies": {
2296+
"description": "The list of reverse dependencies of the crate.",
2297+
"items": {
2298+
"$ref": "#/components/schemas/EncodableDependency"
2299+
},
2300+
"type": "array"
2301+
},
2302+
"meta": {
2303+
"properties": {
2304+
"total": {
2305+
"example": 32,
2306+
"format": "int64",
2307+
"type": "integer"
2308+
}
2309+
},
2310+
"required": [
2311+
"total"
2312+
],
2313+
"type": "object"
2314+
},
2315+
"versions": {
2316+
"description": "The versions referenced in the `dependencies` field.",
2317+
"items": {
2318+
"$ref": "#/components/schemas/Version"
2319+
},
2320+
"type": "array"
2321+
}
2322+
},
2323+
"required": [
2324+
"dependencies",
2325+
"versions",
2326+
"meta"
2327+
],
2328+
"type": "object"
2329+
}
2330+
}
2331+
},
22912332
"description": "Successful Response"
22922333
}
22932334
},

0 commit comments

Comments
 (0)