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

Fix preserve_order feature #562

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions utoipa-swagger-ui/src/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,20 @@ use actix_web::{
Responder as ActixResponder,
};

use crate::{Config, SwaggerUi};
use crate::{ApiDoc, Config, SwaggerUi};

impl HttpServiceFactory for SwaggerUi {
fn register(self, config: &mut actix_web::dev::AppService) {
let mut urls = self
.urls
.into_iter()
.map(|url| {
let (url, openapi) = url;
register_api_doc_url_resource(
url.url.as_ref(),
serde_json::to_value(openapi)
.expect("Cannot convert OpenApi to serde_json::Value"),
config,
);
.map(|(url, openapi)| {
register_api_doc_url_resource(url.url.as_ref(), ApiDoc::Utoipa(openapi), config);
url
})
.collect::<Vec<_>>();
let external_api_docs = self.external_urls.into_iter().map(|(url, api_doc)| {
register_api_doc_url_resource(url.url.as_ref(), api_doc, config);
register_api_doc_url_resource(url.url.as_ref(), ApiDoc::Value(api_doc), config);
url
});
urls.extend(external_api_docs);
Expand All @@ -46,12 +40,8 @@ impl HttpServiceFactory for SwaggerUi {
}
}

fn register_api_doc_url_resource(
url: &str,
api: serde_json::Value,
config: &mut actix_web::dev::AppService,
) {
pub async fn get_api_doc(api_doc: web::Data<serde_json::Value>) -> impl ActixResponder {
fn register_api_doc_url_resource(url: &str, api: ApiDoc, config: &mut actix_web::dev::AppService) {
async fn get_api_doc(api_doc: web::Data<ApiDoc>) -> impl ActixResponder {
HttpResponse::Ok().json(api_doc.as_ref())
}

Expand Down
27 changes: 10 additions & 17 deletions utoipa-swagger-ui/src/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use axum::{
Json, Router,
};

use crate::{Config, SwaggerUi, Url};
use crate::{ApiDoc, Config, SwaggerUi, Url};

impl<S, B> From<SwaggerUi> for Router<S, B>
where
Expand All @@ -23,23 +23,16 @@ where
Router::<S, B>::new(),
Vec::<Url>::with_capacity(urls_capacity + external_urls_capacity),
),
|router_and_urls, url| {
let (url, openapi) = url;

add_api_doc_to_urls(
router_and_urls,
(
url,
serde_json::to_value(openapi)
.expect("Cannot convert OpenApi to serde_json::Value"),
),
)
|router_and_urls, (url, openapi)| {
add_api_doc_to_urls(router_and_urls, (url, ApiDoc::Utoipa(openapi)))
},
);
let (router, urls) = swagger_ui.external_urls.into_iter().fold(
(router, urls),
|router_and_urls, (url, openapi)| {
add_api_doc_to_urls(router_and_urls, (url, ApiDoc::Value(openapi)))
},
);
let (router, urls) = swagger_ui
.external_urls
.into_iter()
.fold((router, urls), add_api_doc_to_urls);

let config = if let Some(config) = swagger_ui.config {
if config.url.is_some() || !config.urls.is_empty() {
Expand Down Expand Up @@ -67,7 +60,7 @@ where

fn add_api_doc_to_urls<S, B>(
router_and_urls: (Router<S, B>, Vec<Url<'static>>),
url: (Url<'static>, serde_json::Value),
url: (Url<'static>, ApiDoc),
) -> (Router<S, B>, Vec<Url<'static>>)
where
S: Clone + Send + Sync + 'static,
Expand Down
22 changes: 22 additions & 0 deletions utoipa-swagger-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,28 @@ fn format_config(config: &Config, file: String) -> Result<String, Box<dyn Error>
Ok(file.replace("{{config}}", &config_json[2..&config_json.len() - 2]))
}

/// Is used to provide general way to deliver multiple types of OpenAPI docs via `utoipa-swagger-ui`.
#[cfg(any(feature = "actix-web", feature = "rocket", feature = "axum"))]
#[derive(Clone)]
enum ApiDoc {
Utoipa(utoipa::openapi::OpenApi),
Value(serde_json::Value),
}

// Delegate serde's `Serialize` to the variant itself.
#[cfg(any(feature = "actix-web", feature = "rocket", feature = "axum"))]
impl Serialize for ApiDoc {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Self::Value(value) => value.serialize(serializer),
Self::Utoipa(utoipa) => utoipa.serialize(serializer),
}
}
}

#[cfg(test)]
mod tests {
use similar::TextDiff;
Expand Down
19 changes: 9 additions & 10 deletions utoipa-swagger-ui/src/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rocket::{
Data as RocketData, Request, Response, Route,
};

use crate::{Config, SwaggerFile, SwaggerUi};
use crate::{ApiDoc, Config, SwaggerFile, SwaggerUi};

impl From<SwaggerUi> for Vec<Route> {
fn from(swagger_ui: SwaggerUi) -> Self {
Expand All @@ -25,14 +25,13 @@ impl From<SwaggerUi> for Vec<Route> {
let urls = swagger_ui
.urls
.into_iter()
.map(|(url, openapi)| {
(
url,
serde_json::to_value(openapi)
.expect("Cannot convert OpenApi to serde_json::Value"),
)
})
.chain(swagger_ui.external_urls.into_iter())
.map(|(url, openapi)| (url, ApiDoc::Utoipa(openapi)))
.chain(
swagger_ui
.external_urls
.into_iter()
.map(|(url, api_doc)| (url, ApiDoc::Value(api_doc))),
)
.map(|(url, openapi)| {
api_docs.push(Route::new(
rocket::http::Method::Get,
Expand Down Expand Up @@ -65,7 +64,7 @@ impl From<SwaggerUi> for Vec<Route> {
}

#[derive(Clone)]
struct ServeApiDoc(serde_json::Value);
struct ServeApiDoc(ApiDoc);

#[rocket::async_trait]
impl Handler for ServeApiDoc {
Expand Down