Skip to content

Commit 145579d

Browse files
committed
Improve OpenAPI documentation for GET /api/v1/crates/{name}/following endpoint
1 parent efd8cc7 commit 145579d

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/controllers/krate/follow.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use crate::controllers::krate::CratePath;
77
use crate::models::{Crate, Follow};
88
use crate::schema::*;
99
use crate::util::errors::{AppResult, crate_not_found};
10-
use axum_extra::json;
11-
use axum_extra::response::ErasedJson;
10+
use axum::Json;
1211
use diesel::prelude::*;
1312
use diesel_async::{AsyncPgConnection, RunQueryDsl};
1413
use http::request::Parts;
@@ -74,20 +73,26 @@ pub async fn unfollow_crate(app: AppState, path: CratePath, req: Parts) -> AppRe
7473
Ok(OkResponse::new())
7574
}
7675

76+
#[derive(Debug, Serialize, utoipa::ToSchema)]
77+
pub struct FollowingResponse {
78+
/// Whether the authenticated user is following the crate.
79+
pub following: bool,
80+
}
81+
7782
/// Check if a crate is followed.
7883
#[utoipa::path(
7984
get,
8085
path = "/api/v1/crates/{name}/following",
8186
params(CratePath),
8287
security(("cookie" = [])),
8388
tag = "crates",
84-
responses((status = 200, description = "Successful Response")),
89+
responses((status = 200, description = "Successful Response", body = inline(FollowingResponse))),
8590
)]
8691
pub async fn get_following_crate(
8792
app: AppState,
8893
path: CratePath,
8994
req: Parts,
90-
) -> AppResult<ErasedJson> {
95+
) -> AppResult<Json<FollowingResponse>> {
9196
use diesel::dsl::exists;
9297

9398
let mut conn = app.db_read_prefer_primary().await?;
@@ -98,8 +103,8 @@ pub async fn get_following_crate(
98103

99104
let follow = follow_target(&path.name, &mut conn, user_id).await?;
100105
let following = diesel::select(exists(follows::table.find(follow.id())))
101-
.get_result::<bool>(&mut conn)
106+
.get_result(&mut conn)
102107
.await?;
103108

104-
Ok(json!({ "following": following }))
109+
Ok(Json(FollowingResponse { following }))
105110
}

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,22 @@ expression: response.json()
15791579
],
15801580
"responses": {
15811581
"200": {
1582+
"content": {
1583+
"application/json": {
1584+
"schema": {
1585+
"properties": {
1586+
"following": {
1587+
"description": "Whether the authenticated user is following the crate.",
1588+
"type": "boolean"
1589+
}
1590+
},
1591+
"required": [
1592+
"following"
1593+
],
1594+
"type": "object"
1595+
}
1596+
}
1597+
},
15821598
"description": "Successful Response"
15831599
}
15841600
},

0 commit comments

Comments
 (0)