Skip to content

Commit 7221003

Browse files
committed
Improve OpenAPI documentation for PUT /api/v1/me/crate_owner_invitations/{crate_id} endpoints
1 parent 513e6af commit 7221003

File tree

3 files changed

+90
-13
lines changed

3 files changed

+90
-13
lines changed

src/controllers/crate_owner_invitation.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use crate::views::{
1414
};
1515
use axum::Json;
1616
use axum::extract::{FromRequestParts, Path, Query};
17-
use axum_extra::json;
18-
use axum_extra::response::ErasedJson;
1917
use chrono::Utc;
2018
use diesel::pg::Pg;
2119
use diesel::prelude::*;
@@ -329,6 +327,12 @@ pub struct OwnerInvitation {
329327
crate_owner_invite: InvitationResponse,
330328
}
331329

330+
#[derive(Debug, Serialize, utoipa::ToSchema)]
331+
pub struct HandleResponse {
332+
#[schema(inline)]
333+
crate_owner_invitation: InvitationResponse,
334+
}
335+
332336
/// Accept or decline a crate owner invitation.
333337
#[utoipa::path(
334338
put,
@@ -341,13 +345,13 @@ pub struct OwnerInvitation {
341345
("cookie" = []),
342346
),
343347
tag = "owners",
344-
responses((status = 200, description = "Successful Response")),
348+
responses((status = 200, description = "Successful Response", body = inline(HandleResponse))),
345349
)]
346350
pub async fn handle_crate_owner_invitation(
347351
state: AppState,
348352
parts: Parts,
349353
Json(crate_invite): Json<OwnerInvitation>,
350-
) -> AppResult<ErasedJson> {
354+
) -> AppResult<Json<HandleResponse>> {
351355
let crate_invite = crate_invite.crate_owner_invite;
352356

353357
let mut conn = state.db_write().await?;
@@ -364,7 +368,9 @@ pub async fn handle_crate_owner_invitation(
364368
invitation.decline(&mut conn).await?;
365369
}
366370

367-
Ok(json!({ "crate_owner_invitation": crate_invite }))
371+
Ok(Json(HandleResponse {
372+
crate_owner_invitation: crate_invite,
373+
}))
368374
}
369375

370376
/// Accept a crate owner invitation with a token.
@@ -375,23 +381,25 @@ pub async fn handle_crate_owner_invitation(
375381
("token" = String, Path, description = "Secret token sent to the user's email address"),
376382
),
377383
tag = "owners",
378-
responses((status = 200, description = "Successful Response")),
384+
responses((status = 200, description = "Successful Response", body = inline(HandleResponse))),
379385
)]
380386
pub async fn accept_crate_owner_invitation_with_token(
381387
state: AppState,
382388
Path(token): Path<String>,
383-
) -> AppResult<ErasedJson> {
389+
) -> AppResult<Json<HandleResponse>> {
384390
let mut conn = state.db_write().await?;
385391
let invitation = CrateOwnerInvitation::find_by_token(&token, &mut conn).await?;
386392

387393
let crate_id = invitation.crate_id;
388394
invitation.accept(&mut conn).await?;
389395

390-
Ok(json!({
391-
"crate_owner_invitation": {
392-
"crate_id": crate_id,
393-
"accepted": true,
394-
},
396+
let crate_owner_invitation = InvitationResponse {
397+
crate_id,
398+
accepted: true,
399+
};
400+
401+
Ok(Json(HandleResponse {
402+
crate_owner_invitation,
395403
}))
396404
}
397405

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,6 +3288,38 @@ expression: response.json()
32883288
],
32893289
"responses": {
32903290
"200": {
3291+
"content": {
3292+
"application/json": {
3293+
"schema": {
3294+
"properties": {
3295+
"crate_owner_invitation": {
3296+
"properties": {
3297+
"accepted": {
3298+
"description": "Whether the invitation was accepted.",
3299+
"example": true,
3300+
"type": "boolean"
3301+
},
3302+
"crate_id": {
3303+
"description": "The opaque identifier for the crate this invitation is for.",
3304+
"example": 42,
3305+
"format": "int32",
3306+
"type": "integer"
3307+
}
3308+
},
3309+
"required": [
3310+
"crate_id",
3311+
"accepted"
3312+
],
3313+
"type": "object"
3314+
}
3315+
},
3316+
"required": [
3317+
"crate_owner_invitation"
3318+
],
3319+
"type": "object"
3320+
}
3321+
}
3322+
},
32913323
"description": "Successful Response"
32923324
}
32933325
},
@@ -3314,6 +3346,38 @@ expression: response.json()
33143346
],
33153347
"responses": {
33163348
"200": {
3349+
"content": {
3350+
"application/json": {
3351+
"schema": {
3352+
"properties": {
3353+
"crate_owner_invitation": {
3354+
"properties": {
3355+
"accepted": {
3356+
"description": "Whether the invitation was accepted.",
3357+
"example": true,
3358+
"type": "boolean"
3359+
},
3360+
"crate_id": {
3361+
"description": "The opaque identifier for the crate this invitation is for.",
3362+
"example": 42,
3363+
"format": "int32",
3364+
"type": "integer"
3365+
}
3366+
},
3367+
"required": [
3368+
"crate_id",
3369+
"accepted"
3370+
],
3371+
"type": "object"
3372+
}
3373+
},
3374+
"required": [
3375+
"crate_owner_invitation"
3376+
],
3377+
"type": "object"
3378+
}
3379+
}
3380+
},
33173381
"description": "Successful Response"
33183382
}
33193383
},

src/views.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,14 @@ pub struct EncodableCrateOwnerInvitation {
128128
pub expires_at: DateTime<Utc>,
129129
}
130130

131-
#[derive(Deserialize, Serialize, Debug, Copy, Clone)]
131+
#[derive(Deserialize, Serialize, Debug, Copy, Clone, utoipa::ToSchema)]
132132
pub struct InvitationResponse {
133+
/// The opaque identifier for the crate this invitation is for.
134+
#[schema(example = 42)]
133135
pub crate_id: i32,
136+
137+
/// Whether the invitation was accepted.
138+
#[schema(example = true)]
134139
pub accepted: bool,
135140
}
136141

0 commit comments

Comments
 (0)