Skip to content

Commit aebebc4

Browse files
committed
Improve OpenAPI documentation for PUT|DELETE /api/v1/crates/{name}/owners endpoints
1 parent ce4fc4a commit aebebc4

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

src/controllers/krate/owners.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ use crate::views::EncodableOwner;
1313
use crate::{App, app::AppState};
1414
use crate::{auth::AuthCheck, email::Email};
1515
use axum::Json;
16-
use axum_extra::json;
17-
use axum_extra::response::ErasedJson;
1816
use chrono::Utc;
1917
use crates_io_github::{GitHubClient, GitHubError};
2018
use diesel::prelude::*;
@@ -102,6 +100,16 @@ pub async fn get_user_owners(state: AppState, path: CratePath) -> AppResult<Json
102100
Ok(Json(UsersResponse { users }))
103101
}
104102

103+
#[derive(Debug, Serialize, utoipa::ToSchema)]
104+
pub struct ModifyResponse {
105+
/// A message describing the result of the operation.
106+
#[schema(example = "user ghost has been invited to be an owner of crate serde")]
107+
pub msg: String,
108+
109+
#[schema(example = true)]
110+
pub ok: bool,
111+
}
112+
105113
/// Add crate owners.
106114
#[utoipa::path(
107115
put,
@@ -112,14 +120,14 @@ pub async fn get_user_owners(state: AppState, path: CratePath) -> AppResult<Json
112120
("cookie" = []),
113121
),
114122
tag = "owners",
115-
responses((status = 200, description = "Successful Response")),
123+
responses((status = 200, description = "Successful Response", body = inline(ModifyResponse))),
116124
)]
117125
pub async fn add_owners(
118126
app: AppState,
119127
path: CratePath,
120128
parts: Parts,
121129
Json(body): Json<ChangeOwnersRequest>,
122-
) -> AppResult<ErasedJson> {
130+
) -> AppResult<Json<ModifyResponse>> {
123131
modify_owners(app, path.name, parts, body, true).await
124132
}
125133

@@ -133,14 +141,14 @@ pub async fn add_owners(
133141
("cookie" = []),
134142
),
135143
tag = "owners",
136-
responses((status = 200, description = "Successful Response")),
144+
responses((status = 200, description = "Successful Response", body = inline(ModifyResponse))),
137145
)]
138146
pub async fn remove_owners(
139147
app: AppState,
140148
path: CratePath,
141149
parts: Parts,
142150
Json(body): Json<ChangeOwnersRequest>,
143-
) -> AppResult<ErasedJson> {
151+
) -> AppResult<Json<ModifyResponse>> {
144152
modify_owners(app, path.name, parts, body, false).await
145153
}
146154

@@ -156,7 +164,7 @@ async fn modify_owners(
156164
parts: Parts,
157165
body: ChangeOwnersRequest,
158166
add: bool,
159-
) -> AppResult<ErasedJson> {
167+
) -> AppResult<Json<ModifyResponse>> {
160168
let logins = body.owners;
161169

162170
// Bound the number of invites processed per request to limit the cost of
@@ -176,7 +184,7 @@ async fn modify_owners(
176184

177185
let user = auth.user();
178186

179-
let (comma_sep_msg, emails) = conn
187+
let (msg, emails) = conn
180188
.transaction(|conn| {
181189
let app = app.clone();
182190
async move {
@@ -291,7 +299,7 @@ async fn modify_owners(
291299
}
292300
}
293301

294-
Ok(json!({ "msg": comma_sep_msg, "ok": true }))
302+
Ok(Json(ModifyResponse { msg, ok: true }))
295303
}
296304

297305
/// Invite `login` as an owner of this crate, returning the created

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,28 @@ expression: response.json()
20882088
],
20892089
"responses": {
20902090
"200": {
2091+
"content": {
2092+
"application/json": {
2093+
"schema": {
2094+
"properties": {
2095+
"msg": {
2096+
"description": "A message describing the result of the operation.",
2097+
"example": "user ghost has been invited to be an owner of crate serde",
2098+
"type": "string"
2099+
},
2100+
"ok": {
2101+
"example": true,
2102+
"type": "boolean"
2103+
}
2104+
},
2105+
"required": [
2106+
"msg",
2107+
"ok"
2108+
],
2109+
"type": "object"
2110+
}
2111+
}
2112+
},
20912113
"description": "Successful Response"
20922114
}
20932115
},
@@ -2160,6 +2182,28 @@ expression: response.json()
21602182
],
21612183
"responses": {
21622184
"200": {
2185+
"content": {
2186+
"application/json": {
2187+
"schema": {
2188+
"properties": {
2189+
"msg": {
2190+
"description": "A message describing the result of the operation.",
2191+
"example": "user ghost has been invited to be an owner of crate serde",
2192+
"type": "string"
2193+
},
2194+
"ok": {
2195+
"example": true,
2196+
"type": "boolean"
2197+
}
2198+
},
2199+
"required": [
2200+
"msg",
2201+
"ok"
2202+
],
2203+
"type": "object"
2204+
}
2205+
}
2206+
},
21632207
"description": "Successful Response"
21642208
}
21652209
},

0 commit comments

Comments
 (0)