Skip to content

Commit 58f9d76

Browse files
authored
Improve OpenAPI documentation for GET /api/v1/teams/{team} endpoint (#10751)
1 parent 3a1eb3d commit 58f9d76

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

src/controllers/team.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ use crate::app::AppState;
22
use crate::models::Team;
33
use crate::util::errors::AppResult;
44
use crate::views::EncodableTeam;
5+
use axum::Json;
56
use axum::extract::Path;
6-
use axum_extra::json;
7-
use axum_extra::response::ErasedJson;
87
use diesel::prelude::*;
98
use diesel_async::RunQueryDsl;
109

10+
#[derive(Debug, Serialize, utoipa::ToSchema)]
11+
pub struct GetResponse {
12+
team: EncodableTeam,
13+
}
14+
1115
/// Find team by login.
1216
#[utoipa::path(
1317
get,
@@ -16,12 +20,13 @@ use diesel_async::RunQueryDsl;
1620
("team" = String, Path, description = "Name of the team", example = "github:rust-lang:crates-io"),
1721
),
1822
tag = "teams",
19-
responses((status = 200, description = "Successful Response")),
23+
responses((status = 200, description = "Successful Response", body = inline(GetResponse))),
2024
)]
21-
pub async fn find_team(state: AppState, Path(name): Path<String>) -> AppResult<ErasedJson> {
25+
pub async fn find_team(state: AppState, Path(name): Path<String>) -> AppResult<Json<GetResponse>> {
2226
use crate::schema::teams::dsl::{login, teams};
2327

2428
let mut conn = state.db_read().await?;
2529
let team: Team = teams.filter(login.eq(&name)).first(&mut conn).await?;
26-
Ok(json!({ "team": EncodableTeam::from(team) }))
30+
let team = EncodableTeam::from(team);
31+
Ok(Json(GetResponse { team }))
2732
}

src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,50 @@ expression: response.json()
691691
],
692692
"type": "object"
693693
},
694+
"Team": {
695+
"properties": {
696+
"avatar": {
697+
"description": "The avatar URL of the team.",
698+
"example": "https://avatars2.githubusercontent.com/u/1234567?v=4",
699+
"type": [
700+
"string",
701+
"null"
702+
]
703+
},
704+
"id": {
705+
"description": "An opaque identifier for the team.",
706+
"example": 42,
707+
"format": "int32",
708+
"type": "integer"
709+
},
710+
"login": {
711+
"description": "The login name of the team.",
712+
"example": "github:rust-lang:crates-io",
713+
"type": "string"
714+
},
715+
"name": {
716+
"description": "The display name of the team.",
717+
"example": "Crates.io team",
718+
"type": [
719+
"string",
720+
"null"
721+
]
722+
},
723+
"url": {
724+
"description": "The GitHub profile URL of the team.",
725+
"example": "https://github.com/rust-lang",
726+
"type": [
727+
"string",
728+
"null"
729+
]
730+
}
731+
},
732+
"required": [
733+
"id",
734+
"login"
735+
],
736+
"type": "object"
737+
},
694738
"User": {
695739
"properties": {
696740
"avatar": {
@@ -3803,6 +3847,21 @@ expression: response.json()
38033847
],
38043848
"responses": {
38053849
"200": {
3850+
"content": {
3851+
"application/json": {
3852+
"schema": {
3853+
"properties": {
3854+
"team": {
3855+
"$ref": "#/components/schemas/Team"
3856+
}
3857+
},
3858+
"required": [
3859+
"team"
3860+
],
3861+
"type": "object"
3862+
}
3863+
}
3864+
},
38063865
"description": "Successful Response"
38073866
}
38083867
},

src/views.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,27 @@ impl From<Owner> for EncodableOwner {
583583
}
584584
}
585585

586-
#[derive(Serialize, Debug)]
586+
#[derive(Serialize, Debug, utoipa::ToSchema)]
587+
#[schema(as = Team)]
587588
pub struct EncodableTeam {
589+
/// An opaque identifier for the team.
590+
#[schema(example = 42)]
588591
pub id: i32,
592+
593+
/// The login name of the team.
594+
#[schema(example = "github:rust-lang:crates-io")]
589595
pub login: String,
596+
597+
/// The display name of the team.
598+
#[schema(example = "Crates.io team")]
590599
pub name: Option<String>,
600+
601+
/// The avatar URL of the team.
602+
#[schema(example = "https://avatars2.githubusercontent.com/u/1234567?v=4")]
591603
pub avatar: Option<String>,
604+
605+
/// The GitHub profile URL of the team.
606+
#[schema(example = "https://github.com/rust-lang")]
592607
pub url: Option<String>,
593608
}
594609

0 commit comments

Comments
 (0)