Skip to content

Commit 9c6a86d

Browse files
committed
Improve OpenAPI documentation for GET /api/v1/crates/{name}/{version}/readme endpoint
1 parent 0a5c9e7 commit 9c6a86d

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/controllers/version/readme.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
use crate::app::AppState;
22
use crate::controllers::version::CrateVersionPath;
33
use crate::util::{RequestUtils, redirect};
4+
use axum::Json;
45
use axum::response::{IntoResponse, Response};
5-
use axum_extra::json;
66
use http::request::Parts;
77

8+
#[derive(Debug, Serialize, utoipa::ToSchema)]
9+
pub struct UrlResponse {
10+
/// The URL to the readme file.
11+
#[schema(example = "https://static.crates.io/readmes/serde/serde-1.0.0.html")]
12+
pub url: String,
13+
}
14+
815
/// Get the readme of a crate version.
916
#[utoipa::path(
1017
get,
1118
path = "/api/v1/crates/{name}/{version}/readme",
1219
params(CrateVersionPath),
1320
tag = "versions",
14-
responses((status = 200, description = "Successful Response")),
21+
responses(
22+
(status = 302, description = "Successful Response (default)", headers(("location" = String, description = "The URL to the readme file."))),
23+
(status = 200, description = "Successful Response (for `content-type: application/json`)", body = inline(UrlResponse)),
24+
),
1525
)]
1626
pub async fn get_version_readme(app: AppState, path: CrateVersionPath, req: Parts) -> Response {
17-
let redirect_url = app.storage.readme_location(&path.name, &path.version);
27+
let url = app.storage.readme_location(&path.name, &path.version);
1828
if req.wants_json() {
19-
json!({ "url": redirect_url }).into_response()
29+
Json(UrlResponse { url }).into_response()
2030
} else {
21-
redirect(redirect_url)
31+
redirect(url)
2232
}
2333
}

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,35 @@ expression: response.json()
20912091
],
20922092
"responses": {
20932093
"200": {
2094-
"description": "Successful Response"
2094+
"content": {
2095+
"application/json": {
2096+
"schema": {
2097+
"properties": {
2098+
"url": {
2099+
"description": "The URL to the readme file.",
2100+
"example": "https://static.crates.io/readmes/serde/serde-1.0.0.html",
2101+
"type": "string"
2102+
}
2103+
},
2104+
"required": [
2105+
"url"
2106+
],
2107+
"type": "object"
2108+
}
2109+
}
2110+
},
2111+
"description": "Successful Response (for `content-type: application/json`)"
2112+
},
2113+
"302": {
2114+
"description": "Successful Response (default)",
2115+
"headers": {
2116+
"location": {
2117+
"description": "The URL to the readme file.",
2118+
"schema": {
2119+
"type": "string"
2120+
}
2121+
}
2122+
}
20952123
}
20962124
},
20972125
"summary": "Get the readme of a crate version.",

0 commit comments

Comments
 (0)