-
-
Notifications
You must be signed in to change notification settings - Fork 296
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
feat: OpenAPI integration #984
Open
NexVeridian
wants to merge
62
commits into
loco-rs:master
Choose a base branch
from
NexVeridian:OpenAPI-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
62 commits
Select commit
Hold shift + click to select a range
8ed9348
inti
NexVeridian 6deca2b
hook initial_openapi_spec
NexVeridian def3dd5
config.yaml and merge hosted doc with router
NexVeridian 8db4443
update existing tests
NexVeridian 34d10e6
test: OpenAPI config from file
NexVeridian c887042
snapshot tests
NexVeridian 4ed2668
fix snapshot path
NexVeridian 2aa3d66
match title, upload snapshots
NexVeridian 0a304c1
OpenAPI json snapshot test
NexVeridian 8c0fd96
Another snapshot
NexVeridian 5f63b08
LocoMethodRouter
NexVeridian 1c67cc6
fix AppContext
NexVeridian c1e953e
missing cfg for tests
NexVeridian 74f78cc
clippy
NexVeridian ac0f1ce
openapi.josn and openapi.yaml endpoints for all types
NexVeridian 2082e12
SecurityAddon
NexVeridian ea48919
fix cfg flag for import
NexVeridian c849c73
Merge branch 'upstream' into OpenAPI-integration
NexVeridian 022909d
fix: snapshots and imports
NexVeridian 165d162
Merge branch 'master' into OpenAPI-integration
kaplanelad 00abce3
split feature openapi into feature swagger-ui redoc scalar, extract s…
NexVeridian 2e7e750
move OpenAPIType.url
NexVeridian e27be7f
drop test for JWT_LOCATION.get_or_init, not possible with cargo test
NexVeridian b848348
rstest feature flagged cases
NexVeridian 7a76f3a
some docs
NexVeridian 3781c13
Merge branch 'master' into OpenAPI-integration
NexVeridian 162ae44
clippy
NexVeridian bef0eb2
Merge branch 'master' into OpenAPI-integration
NexVeridian e1a64a6
docs: docs-site OpenAPI
NexVeridian 8872a10
docs: SecurityAddon
NexVeridian e352e88
feat: format::yaml
NexVeridian dc97291
tests: add headers content-type to snapshots
NexVeridian e691e29
Merge branch 'master' into OpenAPI-integration
NexVeridian 93c1123
mark layer as sync
NexVeridian edccb0c
update snapshots
NexVeridian a23dd55
block doc test for openapi_spec_yaml
NexVeridian 00e72b9
cargo loco generate controller --openapi
NexVeridian 42e9789
update features
NexVeridian 152de4b
prelude and docs update
NexVeridian d7d8774
Merge branch 'master' into OpenAPI-integration
NexVeridian 4958d36
more prelude changes
NexVeridian f833e6b
make unused openapi optional in config file
NexVeridian 2569837
snapshots
NexVeridian b492d68
fix panic for set_jwt_location_ctx
NexVeridian 970e6c6
remove demo app test
NexVeridian e0c1f89
bump version of utoipa
NexVeridian d72c460
Merge branch 'master' into OpenAPI-integration
kaplanelad ba43fcf
swap assert_debug_snapshot to json or yaml snapshot
NexVeridian facb0ae
cargo format
NexVeridian 06da6e9
hide loco version from snapshot
NexVeridian 819b45e
allow and defualt to None jwt_location
NexVeridian 6c3d1aa
Merge branch 'master' into OpenAPI-integration
NexVeridian 05d90af
clippy
NexVeridian 9107d3e
drop demo test
NexVeridian 0a7df8c
drop generator command for now
NexVeridian 9f6bac2
clippy
NexVeridian 8a0c996
clippy fix from main branch
NexVeridian 0a09a5c
fmt from main branch
NexVeridian 902f65a
fix doc tests
NexVeridian fb9e237
some derive(ToSchema) and derive(IntoParams)
NexVeridian 9a74966
more ToSchema
NexVeridian 5f4c63f
Revert "more ToSchema" and "some derive(ToSchema) and derive(IntoPara…
NexVeridian 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
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 |
---|---|---|
@@ -1,2 +1,8 @@ | ||
#[cfg(feature = "auth_jwt")] | ||
pub mod jwt; | ||
#[cfg(any( | ||
feature = "openapi_swagger", | ||
feature = "openapi_redoc", | ||
feature = "openapi_scalar" | ||
))] | ||
pub mod openapi; |
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,68 @@ | ||
use std::sync::OnceLock; | ||
|
||
use utoipa::{ | ||
openapi::security::{ApiKey, ApiKeyValue, HttpAuthScheme, HttpBuilder, SecurityScheme}, | ||
Modify, | ||
}; | ||
|
||
use crate::{app::AppContext, config::JWTLocation}; | ||
|
||
static JWT_LOCATION: OnceLock<Option<JWTLocation>> = OnceLock::new(); | ||
|
||
#[must_use] | ||
pub fn get_jwt_location_from_ctx(ctx: &AppContext) -> JWTLocation { | ||
ctx.config | ||
.auth | ||
.as_ref() | ||
.and_then(|auth| auth.jwt.as_ref()) | ||
.and_then(|jwt| jwt.location.as_ref()) | ||
.unwrap_or(&JWTLocation::Bearer) | ||
.clone() | ||
} | ||
|
||
pub fn set_jwt_location_ctx(ctx: &AppContext) { | ||
set_jwt_location(get_jwt_location_from_ctx(ctx)); | ||
} | ||
|
||
pub fn set_jwt_location(jwt_location: JWTLocation) -> &'static Option<JWTLocation> { | ||
JWT_LOCATION.get_or_init(|| Some(jwt_location)) | ||
} | ||
|
||
fn get_jwt_location() -> Option<&'static JWTLocation> { | ||
JWT_LOCATION.get().unwrap_or(&None).as_ref() | ||
} | ||
|
||
pub struct SecurityAddon; | ||
|
||
/// Adds security to the `OpenAPI` doc, using the JWT location in the config | ||
impl Modify for SecurityAddon { | ||
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) { | ||
if let Some(jwt_location) = get_jwt_location() { | ||
if let Some(components) = openapi.components.as_mut() { | ||
components.add_security_schemes_from_iter([ | ||
( | ||
"jwt_token", | ||
match jwt_location { | ||
JWTLocation::Bearer => SecurityScheme::Http( | ||
HttpBuilder::new() | ||
.scheme(HttpAuthScheme::Bearer) | ||
.bearer_format("JWT") | ||
.build(), | ||
), | ||
JWTLocation::Query { name } => { | ||
SecurityScheme::ApiKey(ApiKey::Query(ApiKeyValue::new(name))) | ||
} | ||
JWTLocation::Cookie { name } => { | ||
SecurityScheme::ApiKey(ApiKey::Cookie(ApiKeyValue::new(name))) | ||
} | ||
}, | ||
), | ||
( | ||
"api_key", | ||
SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::new("apikey"))), | ||
), | ||
]); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
seems like this might be flaky in
cargo test
because the global state isn't reset between test runstests are good in
cargo nextest
and should be fine for users