Skip to content

Commit 0ba61cf

Browse files
committed
Improve utoipa for tech review routes
1 parent 889079d commit 0ba61cf

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

apps/labrinth/src/main.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::sync::Arc;
2121
use tracing::{Instrument, error, info, info_span};
2222
use tracing_actix_web::TracingLogger;
2323
use utoipa::OpenApi;
24+
use utoipa::openapi::security::{ApiKey, ApiKeyValue, SecurityScheme};
2425
use utoipa_actix_web::AppExt;
2526
use utoipa_swagger_ui::SwaggerUi;
2627

@@ -262,9 +263,23 @@ async fn main() -> std::io::Result<()> {
262263
}
263264

264265
#[derive(utoipa::OpenApi)]
265-
#[openapi(info(title = "Labrinth"))]
266+
#[openapi(info(title = "Labrinth"), modifiers(&SecurityAddon))]
266267
struct ApiDoc;
267268

269+
struct SecurityAddon;
270+
271+
impl utoipa::Modify for SecurityAddon {
272+
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
273+
let components = openapi.components.as_mut().unwrap();
274+
components.add_security_scheme(
275+
"bearer_auth",
276+
SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::new(
277+
"authorization",
278+
))),
279+
);
280+
}
281+
}
282+
268283
fn log_error(err: &actix_web::Error) {
269284
if err.as_response_error().status_code().is_client_error() {
270285
tracing::debug!(

apps/labrinth/src/routes/internal/moderation/tech_review.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ pub fn config(cfg: &mut utoipa_actix_web::service_config::ServiceConfig) {
3434
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
3535
pub struct SearchProjects {
3636
#[serde(default = "default_limit")]
37+
#[schema(default = 20)]
3738
pub limit: u64,
3839
#[serde(default)]
40+
#[schema(default = 0)]
3941
pub page: u64,
4042
#[serde(default)]
4143
pub filter: SearchProjectsFilter,
@@ -163,7 +165,10 @@ pub struct FileIssueDetail {
163165
}
164166

165167
/// Searches all projects which are awaiting technical review.
166-
#[utoipa::path]
168+
#[utoipa::path(
169+
security(("bearer_auth" = [])),
170+
responses((status = OK, body = inline(Vec<ProjectReview>)))
171+
)]
167172
#[post("/search")]
168173
async fn search_projects(
169174
req: HttpRequest,
@@ -209,7 +214,7 @@ async fn search_projects(
209214

210215
let sort_by = search_req.sort_by.to_string();
211216
let limit = search_req.limit.max(50);
212-
let offset = limit * search_req.page;
217+
let offset = limit.saturating_mul(search_req.page);
213218

214219
let limit =
215220
i64::try_from(limit).wrap_request_err("limit cannot fit into `i64`")?;
@@ -435,14 +440,18 @@ async fn search_projects(
435440
Ok(web::Json(projects))
436441
}
437442

438-
/// Updates the state of a technical review issue.
443+
/// See [`update_issue`].
439444
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
440445
pub struct UpdateIssue {
441446
/// Status to set the issue to.
442447
pub status: DelphiReportIssueStatus,
443448
}
444449

445-
#[utoipa::path]
450+
/// Updates the state of a technical review issue.
451+
#[utoipa::path(
452+
security(("bearer_auth" = [])),
453+
responses((status = NO_CONTENT))
454+
)]
446455
#[post("/issue/{id}")]
447456
async fn update_issue(
448457
req: HttpRequest,

0 commit comments

Comments
 (0)