Skip to content

Commit fbb068f

Browse files
committed
controllers/krate/metadata: Extract FindQueryParams struct
1 parent dcb32bd commit fbb068f

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/controllers/krate/metadata.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,30 @@ use crate::models::{
1212
};
1313
use crate::schema::*;
1414
use crate::util::errors::{bad_request, crate_not_found, AppResult, BoxedAppError};
15-
use crate::util::RequestUtils;
1615
use crate::views::{EncodableCategory, EncodableCrate, EncodableKeyword, EncodableVersion};
16+
use axum::extract::{FromRequestParts, Query};
1717
use axum_extra::json;
1818
use axum_extra::response::ErasedJson;
1919
use diesel::prelude::*;
2020
use diesel_async::RunQueryDsl;
21-
use http::request::Parts;
2221
use std::cmp::Reverse;
2322
use std::str::FromStr;
2423

24+
#[derive(Debug, Deserialize, FromRequestParts, utoipa::IntoParams)]
25+
#[from_request(via(Query))]
26+
#[into_params(parameter_in = Query)]
27+
pub struct FindQueryParams {
28+
/// Additional data to include in the response.
29+
///
30+
/// Valid values: `versions`, `keywords`, `categories`, `badges`,
31+
/// `downloads`, or `full`.
32+
///
33+
/// Defaults to `full` for backwards compatibility.
34+
///
35+
/// This parameter expects a comma-separated list of values.
36+
include: Option<String>,
37+
}
38+
2539
/// Get crate metadata (for the `new` crate).
2640
///
2741
/// This endpoint works around a small limitation in `axum` and is delegating
@@ -32,26 +46,29 @@ use std::str::FromStr;
3246
tag = "crates",
3347
responses((status = 200, description = "Successful Response")),
3448
)]
35-
pub async fn find_new_crate(app: AppState, req: Parts) -> AppResult<ErasedJson> {
49+
pub async fn find_new_crate(app: AppState, params: FindQueryParams) -> AppResult<ErasedJson> {
3650
let name = "new".to_string();
37-
find_crate(app, CratePath { name }, req).await
51+
find_crate(app, CratePath { name }, params).await
3852
}
3953

4054
/// Get crate metadata.
4155
#[utoipa::path(
4256
get,
4357
path = "/api/v1/crates/{name}",
44-
params(CratePath),
58+
params(CratePath, FindQueryParams),
4559
tag = "crates",
4660
responses((status = 200, description = "Successful Response")),
4761
)]
48-
pub async fn find_crate(app: AppState, path: CratePath, req: Parts) -> AppResult<ErasedJson> {
62+
pub async fn find_crate(
63+
app: AppState,
64+
path: CratePath,
65+
params: FindQueryParams,
66+
) -> AppResult<ErasedJson> {
4967
let mut conn = app.db_read().await?;
5068

51-
let include = req
52-
.query()
53-
.get("include")
54-
.map(|mode| ShowIncludeMode::from_str(mode))
69+
let include = params
70+
.include
71+
.map(|mode| ShowIncludeMode::from_str(&mode))
5572
.transpose()?
5673
.unwrap_or_default();
5774

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,18 @@ snapshot_kind: text
560560
"schema": {
561561
"type": "string"
562562
}
563+
},
564+
{
565+
"description": "Additional data to include in the response.\n\nValid values: `versions`, `keywords`, `categories`, `badges`,\n`downloads`, or `full`.\n\nDefaults to `full` for backwards compatibility.\n\nThis parameter expects a comma-separated list of values.",
566+
"in": "query",
567+
"name": "include",
568+
"required": false,
569+
"schema": {
570+
"type": [
571+
"string",
572+
"null"
573+
]
574+
}
563575
}
564576
],
565577
"responses": {

0 commit comments

Comments
 (0)