-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10186 from Turbo87/utoipa
Use `utoipa` crates to generate and serve basic OpenAPI description
- Loading branch information
Showing
7 changed files
with
169 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use utoipa::OpenApi; | ||
use utoipa_axum::router::OpenApiRouter; | ||
|
||
#[derive(OpenApi)] | ||
#[openapi( | ||
info( | ||
title = "crates.io", | ||
description = "API documentation for the [crates.io](https://crates.io/) package registry", | ||
terms_of_service = "https://crates.io/policies", | ||
contact(name = "the crates.io team", email = "[email protected]"), | ||
license(), | ||
version = "0.0.0", | ||
), | ||
servers( | ||
(url = "https://crates.io"), | ||
(url = "https://staging.crates.io"), | ||
), | ||
)] | ||
pub struct BaseOpenApi; | ||
|
||
impl BaseOpenApi { | ||
pub fn router<S>() -> OpenApiRouter<S> | ||
where | ||
S: Send + Sync + Clone + 'static, | ||
{ | ||
OpenApiRouter::with_openapi(Self::openapi()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::tests::util::{RequestHelper, TestApp}; | ||
use http::StatusCode; | ||
use insta::assert_json_snapshot; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_openapi_snapshot() { | ||
let (_app, anon) = TestApp::init().empty().await; | ||
|
||
let response = anon.get::<()>("/api/openapi.json").await; | ||
assert_eq!(response.status(), StatusCode::OK); | ||
assert_json_snapshot!(response.json()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/snapshots/crates_io__openapi__tests__openapi_snapshot.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
source: src/openapi.rs | ||
expression: response.json() | ||
snapshot_kind: text | ||
--- | ||
{ | ||
"components": {}, | ||
"info": { | ||
"contact": { | ||
"email": "[email protected]", | ||
"name": "the crates.io team" | ||
}, | ||
"description": "API documentation for the [crates.io](https://crates.io/) package registry", | ||
"license": { | ||
"name": "" | ||
}, | ||
"termsOfService": "https://crates.io/policies", | ||
"title": "crates.io", | ||
"version": "0.0.0" | ||
}, | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/api/v1/crates": { | ||
"get": { | ||
"description": "Called in a variety of scenarios in the front end, including:\n- Alphabetical listing of crates\n- List of crates under a specific owner\n- Listing a user's followed crates", | ||
"operationId": "crates_list", | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response" | ||
} | ||
}, | ||
"summary": "Returns a list of crates.", | ||
"tags": [ | ||
"crates" | ||
] | ||
} | ||
} | ||
}, | ||
"servers": [ | ||
{ | ||
"url": "https://crates.io" | ||
}, | ||
{ | ||
"url": "https://staging.crates.io" | ||
} | ||
] | ||
} |