Skip to content

Commit b240999

Browse files
authored
Add OpenAPI documentation for GET /api/v1/users/{id}/stats response payload (#10702)
1 parent 37c9912 commit b240999

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/controllers/user/other.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use axum::Json;
22
use axum::extract::Path;
3-
use axum_extra::json;
4-
use axum_extra::response::ErasedJson;
53
use bigdecimal::{BigDecimal, ToPrimitive};
64
use diesel::prelude::*;
75
use diesel_async::RunQueryDsl;
@@ -46,6 +44,13 @@ pub async fn find_user(
4644
Ok(Json(GetResponse { user: user.into() }))
4745
}
4846

47+
#[derive(Debug, Serialize, utoipa::ToSchema)]
48+
pub struct StatsResponse {
49+
/// The total number of downloads for crates owned by the user.
50+
#[schema(example = 123_456_789)]
51+
pub total_downloads: u64,
52+
}
53+
4954
/// Get user stats.
5055
///
5156
/// This currently only returns the total number of downloads for crates owned
@@ -57,15 +62,18 @@ pub async fn find_user(
5762
("id" = i32, Path, description = "ID of the user"),
5863
),
5964
tag = "users",
60-
responses((status = 200, description = "Successful Response")),
65+
responses((status = 200, description = "Successful Response", body = inline(StatsResponse))),
6166
)]
62-
pub async fn get_user_stats(state: AppState, Path(user_id): Path<i32>) -> AppResult<ErasedJson> {
67+
pub async fn get_user_stats(
68+
state: AppState,
69+
Path(user_id): Path<i32>,
70+
) -> AppResult<Json<StatsResponse>> {
6371
let mut conn = state.db_read_prefer_primary().await?;
6472

6573
use diesel::dsl::sum;
6674
use diesel_async::RunQueryDsl;
6775

68-
let data = CrateOwner::by_owner_kind(OwnerKind::User)
76+
let total_downloads = CrateOwner::by_owner_kind(OwnerKind::User)
6977
.inner_join(crates::table)
7078
.inner_join(crate_downloads::table.on(crates::id.eq(crate_downloads::crate_id)))
7179
.filter(crate_owners::owner_id.eq(user_id))
@@ -75,5 +83,5 @@ pub async fn get_user_stats(state: AppState, Path(user_id): Path<i32>) -> AppRes
7583
.map(|d| d.to_u64().unwrap_or(u64::MAX))
7684
.unwrap_or(0);
7785

78-
Ok(json!({ "total_downloads": data }))
86+
Ok(Json(StatsResponse { total_downloads }))
7987
}

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,25 @@ expression: response.json()
20212021
],
20222022
"responses": {
20232023
"200": {
2024+
"content": {
2025+
"application/json": {
2026+
"schema": {
2027+
"properties": {
2028+
"total_downloads": {
2029+
"description": "The total number of downloads for crates owned by the user.",
2030+
"example": 123456789,
2031+
"format": "int64",
2032+
"minimum": 0,
2033+
"type": "integer"
2034+
}
2035+
},
2036+
"required": [
2037+
"total_downloads"
2038+
],
2039+
"type": "object"
2040+
}
2041+
}
2042+
},
20242043
"description": "Successful Response"
20252044
}
20262045
},

0 commit comments

Comments
 (0)