Skip to content

Commit 40ec540

Browse files
authored
Add OpenAPI documentation for GET /api/v1/crates/{name}/{version}/dependencies response payload (#10717)
1 parent d154cd0 commit 40ec540

File tree

3 files changed

+127
-7
lines changed

3 files changed

+127
-7
lines changed

src/controllers/version/dependencies.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ use crate::app::AppState;
33
use crate::models::Dependency;
44
use crate::util::errors::AppResult;
55
use crate::views::EncodableDependency;
6-
use axum_extra::json;
7-
use axum_extra::response::ErasedJson;
6+
use axum::Json;
87
use crates_io_database::schema::{crates, dependencies};
98
use diesel::prelude::*;
109
use diesel_async::RunQueryDsl;
1110

11+
#[derive(Debug, Serialize, utoipa::ToSchema)]
12+
pub struct Response {
13+
pub dependencies: Vec<EncodableDependency>,
14+
}
15+
1216
/// Get crate version dependencies.
1317
///
1418
/// This information can also be obtained directly from the index.
@@ -21,16 +25,16 @@ use diesel_async::RunQueryDsl;
2125
path = "/api/v1/crates/{name}/{version}/dependencies",
2226
params(CrateVersionPath),
2327
tag = "versions",
24-
responses((status = 200, description = "Successful Response")),
28+
responses((status = 200, description = "Successful Response", body = inline(Response))),
2529
)]
2630
pub async fn get_version_dependencies(
2731
state: AppState,
2832
path: CrateVersionPath,
29-
) -> AppResult<ErasedJson> {
33+
) -> AppResult<Json<Response>> {
3034
let mut conn = state.db_read().await?;
3135
let version = path.load_version(&mut conn).await?;
3236

33-
let deps = Dependency::belonging_to(&version)
37+
let dependencies = Dependency::belonging_to(&version)
3438
.inner_join(crates::table)
3539
.select((Dependency::as_select(), crates::name))
3640
.order((dependencies::optional, crates::name))
@@ -40,5 +44,5 @@ pub async fn get_version_dependencies(
4044
.map(|(dep, crate_name)| EncodableDependency::from_dep(dep, &crate_name))
4145
.collect::<Vec<_>>();
4246

43-
Ok(json!({ "dependencies": deps }))
47+
Ok(Json(Response { dependencies }))
4448
}

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,78 @@ expression: response.json()
196196
],
197197
"type": "object"
198198
},
199+
"EncodableDependency": {
200+
"properties": {
201+
"crate_id": {
202+
"description": "The name of the crate this dependency points to.",
203+
"example": "serde",
204+
"type": "string"
205+
},
206+
"default_features": {
207+
"description": "Whether default features are enabled for this dependency.",
208+
"example": true,
209+
"type": "boolean"
210+
},
211+
"downloads": {
212+
"description": "The total number of downloads for the crate this dependency points to.",
213+
"example": 123456,
214+
"format": "int64",
215+
"type": "integer"
216+
},
217+
"features": {
218+
"description": "The features explicitly enabled for this dependency.",
219+
"items": {
220+
"type": "string"
221+
},
222+
"type": "array"
223+
},
224+
"id": {
225+
"description": "An opaque identifier for the dependency.",
226+
"example": 169,
227+
"format": "int32",
228+
"type": "integer"
229+
},
230+
"kind": {
231+
"description": "The type of dependency this is (normal, dev, or build).",
232+
"example": "normal",
233+
"type": "string"
234+
},
235+
"optional": {
236+
"description": "Whether this dependency is optional.",
237+
"type": "boolean"
238+
},
239+
"req": {
240+
"description": "The version requirement for this dependency.",
241+
"example": "^1",
242+
"type": "string"
243+
},
244+
"target": {
245+
"description": "The target platform for this dependency, if any.",
246+
"type": [
247+
"string",
248+
"null"
249+
]
250+
},
251+
"version_id": {
252+
"description": "The ID of the version this dependency belongs to.",
253+
"example": 42,
254+
"format": "int32",
255+
"type": "integer"
256+
}
257+
},
258+
"required": [
259+
"id",
260+
"version_id",
261+
"crate_id",
262+
"req",
263+
"optional",
264+
"default_features",
265+
"features",
266+
"kind",
267+
"downloads"
268+
],
269+
"type": "object"
270+
},
199271
"Keyword": {
200272
"properties": {
201273
"crates_cnt": {
@@ -1838,6 +1910,24 @@ expression: response.json()
18381910
],
18391911
"responses": {
18401912
"200": {
1913+
"content": {
1914+
"application/json": {
1915+
"schema": {
1916+
"properties": {
1917+
"dependencies": {
1918+
"items": {
1919+
"$ref": "#/components/schemas/EncodableDependency"
1920+
},
1921+
"type": "array"
1922+
}
1923+
},
1924+
"required": [
1925+
"dependencies"
1926+
],
1927+
"type": "object"
1928+
}
1929+
}
1930+
},
18411931
"description": "Successful Response"
18421932
}
18431933
},

src/views.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,43 @@ pub struct InvitationResponse {
134134
pub accepted: bool,
135135
}
136136

137-
#[derive(Serialize, Deserialize, Debug)]
137+
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
138138
pub struct EncodableDependency {
139+
/// An opaque identifier for the dependency.
140+
#[schema(example = 169)]
139141
pub id: i32,
142+
143+
/// The ID of the version this dependency belongs to.
144+
#[schema(example = 42)]
140145
pub version_id: i32,
146+
147+
/// The name of the crate this dependency points to.
148+
#[schema(example = "serde")]
141149
pub crate_id: String,
150+
151+
/// The version requirement for this dependency.
152+
#[schema(example = "^1")]
142153
pub req: String,
154+
155+
/// Whether this dependency is optional.
143156
pub optional: bool,
157+
158+
/// Whether default features are enabled for this dependency.
159+
#[schema(example = true)]
144160
pub default_features: bool,
161+
162+
/// The features explicitly enabled for this dependency.
145163
pub features: Vec<String>,
164+
165+
/// The target platform for this dependency, if any.
146166
pub target: Option<String>,
167+
168+
/// The type of dependency this is (normal, dev, or build).
169+
#[schema(value_type = String, example = "normal")]
147170
pub kind: DependencyKind,
171+
172+
/// The total number of downloads for the crate this dependency points to.
173+
#[schema(example = 123_456)]
148174
pub downloads: i64,
149175
}
150176

0 commit comments

Comments
 (0)