-
Notifications
You must be signed in to change notification settings - Fork 229
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(swagger-ui): add support for oauth config #134
Conversation
Fixes #131 |
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.
Yeah this is pretty much something I planned actually add. Later I will refactor this a bit and add the swagger-ui other configuration options as well but that is another PR then.
Overall pretty good. Some minor things to check through though :)
utoipa-swagger-ui/src/oauth.rs
Outdated
const END_MARKER: &str = "//</editor-fold>"; | ||
|
||
// https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/oauth2.md | ||
#[derive(Default, Clone, Debug, Serialize)] |
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 Debug
trait could be placed under debug
feature flag to reduce possible clutter for people who does not wish to have that there by default.
#[derive(Default, Clone, Debug, Serialize)] | |
#[derive(Default, Clone, Serialize)] | |
#[cfg(feature = "debug", derive(Debug))] |
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.
Done
@@ -467,6 +502,7 @@ async fn serve_swagger_ui(path: web::Path<String>, data: web::Data<Config<'_>>) | |||
pub struct Config<'a> { | |||
/// [`Url`]s the Swagger UI is serving. | |||
urls: Vec<Url<'a>>, | |||
oauth: Option<oauth::Config>, |
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.
This could field could have a short doc comment on it.
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.
Done
.app_data(Data::new(Config { | ||
urls: urls, | ||
oauth: self.oauth, | ||
})) |
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.
This block should also be added for rocket framework. Look at line 309 :)
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.
Done
// https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/oauth2.md | ||
#[derive(Default, Clone, Debug, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Config { |
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.
And this should also have a proper doc comment along with doc comments on the options.
You can see the docs with cargo doc --workspace --no-deps --features serde_json,json,actix-web,rocket,actix_extras
and browse to the docs in targer folder e.g. file:///home/me/development/utoipa/target/doc/utoipa/index.html
.
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.
Done
@@ -481,6 +517,7 @@ impl<'a> Config<'a> { | |||
pub fn new<I: IntoIterator<Item = U>, U: Into<Url<'a>>>(urls: I) -> Self { | |||
Self { | |||
urls: urls.into_iter().map(|url| url.into()).collect(), | |||
oauth: None, | |||
} | |||
} |
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 would add here a method with_oauth_config
something like this.
pub fn with_oauth_config<I: IntoIterator<Item = U>, U: Into<Url<'a>>>(urls: I,
oauth_config: oauth::Config) -> Self {
Self {
urls: urls.into_iter().map(|url| url.into()).collect(),
oauth: Some(oauth_config),
}
}
This Config
type is non_exhaustive
for backwards compatibility purposes
Outside of the defining crate, types annotated with non_exhaustive have limitations that preserve backwards compatibility when new fields or variants are added.
Non-exhaustive types cannot be constructed outside of the defining crate:
Non-exhaustive variants (struct or enum variant) cannot be constructed with a StructExpression (including with functional update syntax).
enum instances can be constructed.
https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute
It is in Rust a good idea to define outside looking API with non_exhaustive
and instead provide constructors new()
or / and accessor fields or builder pattern to construct objects. This way the API implementator is at control and not every change to the API nor adding / changing / deleting one field will be a breaking change for API consumer.
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.
Done
@@ -0,0 +1,81 @@ | |||
use std::collections::HashMap; |
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.
This could also have some module level comments like
//! Implements Swagger UI [oauth configuration](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/oauth2.md) options.
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.
Done
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.
Thanks for the great feedback
.app_data(Data::new(Config { | ||
urls: urls, | ||
oauth: self.oauth, | ||
})) |
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.
Done
@@ -0,0 +1,81 @@ | |||
use std::collections::HashMap; |
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.
Done
@@ -467,6 +502,7 @@ async fn serve_swagger_ui(path: web::Path<String>, data: web::Data<Config<'_>>) | |||
pub struct Config<'a> { | |||
/// [`Url`]s the Swagger UI is serving. | |||
urls: Vec<Url<'a>>, | |||
oauth: Option<oauth::Config>, |
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.
Done
@@ -481,6 +517,7 @@ impl<'a> Config<'a> { | |||
pub fn new<I: IntoIterator<Item = U>, U: Into<Url<'a>>>(urls: I) -> Self { | |||
Self { | |||
urls: urls.into_iter().map(|url| url.into()).collect(), | |||
oauth: None, | |||
} | |||
} |
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.
Done
utoipa-swagger-ui/src/oauth.rs
Outdated
const END_MARKER: &str = "//</editor-fold>"; | ||
|
||
// https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/oauth2.md | ||
#[derive(Default, Clone, Debug, Serialize)] |
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.
Done
// https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/oauth2.md | ||
#[derive(Default, Clone, Debug, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Config { |
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.
Done
Welcome you can try running tests locally with |
b286f72
to
f5984ee
Compare
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.
Super 🚀
@juhaku not sure what you had in mind for supporting this. Welcome to any feedback