Skip to content

Commit e99762b

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

File tree

3 files changed

+445
-16
lines changed

3 files changed

+445
-16
lines changed

src/controllers/krate/metadata.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ use crate::util::errors::{
1515
AppResult, BoxedAppError, bad_request, crate_not_found, version_not_found,
1616
};
1717
use crate::views::{EncodableCategory, EncodableCrate, EncodableKeyword, EncodableVersion};
18+
use axum::Json;
1819
use axum::extract::{FromRequestParts, Query};
19-
use axum_extra::json;
20-
use axum_extra::response::ErasedJson;
2120
use diesel::prelude::*;
2221
use diesel_async::{AsyncPgConnection, RunQueryDsl};
2322
use futures_util::FutureExt;
@@ -41,6 +40,25 @@ pub struct FindQueryParams {
4140
include: Option<String>,
4241
}
4342

43+
#[derive(Debug, Serialize, utoipa::ToSchema)]
44+
pub struct GetResponse {
45+
/// The crate metadata.
46+
#[serde(rename = "crate")]
47+
krate: EncodableCrate,
48+
49+
/// The versions of the crate.
50+
#[schema(example = json!(null))]
51+
versions: Option<Vec<EncodableVersion>>,
52+
53+
/// The keywords of the crate.
54+
#[schema(example = json!(null))]
55+
keywords: Option<Vec<EncodableKeyword>>,
56+
57+
/// The categories of the crate.
58+
#[schema(example = json!(null))]
59+
categories: Option<Vec<EncodableCategory>>,
60+
}
61+
4462
/// Get crate metadata (for the `new` crate).
4563
///
4664
/// This endpoint works around a small limitation in `axum` and is delegating
@@ -49,9 +67,12 @@ pub struct FindQueryParams {
4967
get,
5068
path = "/api/v1/crates/new",
5169
tag = "crates",
52-
responses((status = 200, description = "Successful Response")),
70+
responses((status = 200, description = "Successful Response", body = inline(GetResponse))),
5371
)]
54-
pub async fn find_new_crate(app: AppState, params: FindQueryParams) -> AppResult<ErasedJson> {
72+
pub async fn find_new_crate(
73+
app: AppState,
74+
params: FindQueryParams,
75+
) -> AppResult<Json<GetResponse>> {
5576
let name = "new".to_string();
5677
find_crate(app, CratePath { name }, params).await
5778
}
@@ -62,13 +83,13 @@ pub async fn find_new_crate(app: AppState, params: FindQueryParams) -> AppResult
6283
path = "/api/v1/crates/{name}",
6384
params(CratePath, FindQueryParams),
6485
tag = "crates",
65-
responses((status = 200, description = "Successful Response")),
86+
responses((status = 200, description = "Successful Response", body = inline(GetResponse))),
6687
)]
6788
pub async fn find_crate(
6889
app: AppState,
6990
path: CratePath,
7091
params: FindQueryParams,
71-
) -> AppResult<ErasedJson> {
92+
) -> AppResult<Json<GetResponse>> {
7293
let mut conn = app.db_read().await?;
7394

7495
let include = params
@@ -186,11 +207,11 @@ pub async fn find_crate(
186207
.collect::<Vec<EncodableCategory>>()
187208
});
188209

189-
Ok(json!({
190-
"crate": encodable_crate,
191-
"versions": encodable_versions,
192-
"keywords": encodable_keywords,
193-
"categories": encodable_cats,
210+
Ok(Json(GetResponse {
211+
krate: encodable_crate,
212+
versions: encodable_versions,
213+
keywords: encodable_keywords,
214+
categories: encodable_cats,
194215
}))
195216
}
196217

0 commit comments

Comments
 (0)