Skip to content
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

Merged
merged 3 commits into from
May 10, 2022

Conversation

jgramoll
Copy link
Contributor

@jgramoll jgramoll commented May 9, 2022

@juhaku not sure what you had in mind for supporting this. Welcome to any feedback

@jgramoll
Copy link
Contributor Author

jgramoll commented May 9, 2022

Fixes #131

Copy link
Owner

@juhaku juhaku left a 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 :)

const END_MARKER: &str = "//</editor-fold>";

// https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/oauth2.md
#[derive(Default, Clone, Debug, Serialize)]
Copy link
Owner

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.

Suggested change
#[derive(Default, Clone, Debug, Serialize)]
#[derive(Default, Clone, Serialize)]
#[cfg(feature = "debug", derive(Debug))]

Copy link
Contributor Author

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>,
Copy link
Owner

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.

Copy link
Contributor Author

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,
}))
Copy link
Owner

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 :)

Copy link
Contributor Author

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 {
Copy link
Owner

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.

Copy link
Contributor Author

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,
}
}
Copy link
Owner

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.

Copy link
Contributor Author

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;
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@juhaku juhaku linked an issue May 9, 2022 that may be closed by this pull request
Copy link
Contributor Author

@jgramoll jgramoll left a 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,
}))
Copy link
Contributor Author

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;
Copy link
Contributor Author

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>,
Copy link
Contributor Author

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,
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const END_MARKER: &str = "//</editor-fold>";

// https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/oauth2.md
#[derive(Default, Clone, Debug, Serialize)]
Copy link
Contributor Author

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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@juhaku
Copy link
Owner

juhaku commented May 10, 2022

Thanks for the great feedback

Welcome you can try running tests locally with scripts/test.sh utoipa-swagger-ui to get started with. And further use the commands found from that file to run specific tests if needed as well.

@jgramoll jgramoll force-pushed the swagger-ui-oauth branch from b286f72 to f5984ee Compare May 10, 2022 16:41
Copy link
Owner

@juhaku juhaku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super 🚀

@juhaku juhaku merged commit 5cfd77f into juhaku:master May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support pkce with oauth2
2 participants