-
Notifications
You must be signed in to change notification settings - Fork 645
Use utoipa
crates to generate and serve basic OpenAPI description
#10186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
189e496
Use `utoipa` crate to generate and serve basic OpenAPI description
Turbo87 2f484cc
openapi: Override derived metadata
Turbo87 f9176c8
openapi: Add explicit `servers` declaration
Turbo87 a095082
controllers/krate/search: Use basic `utoipa` annotation
Turbo87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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" | ||
} | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the OpenAPI spec does not specify the format of the
version
field other than that it's a "string". we could potentially use the git SHA1 or commit timestamp as theversion
, if that seems useful 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thinking a combination of both would probably be most informative: something like
2024-12-11-21-45-00-4b33fdda250e7a8ebb4c40eff4d6205ce09c7e2a
.