From b3af4c08b2247da91e8e9c9ecb86899bb3e26069 Mon Sep 17 00:00:00 2001 From: Chrislearn Young Date: Fri, 3 Jan 2025 10:06:21 +0800 Subject: [PATCH 1/2] fix(oapi): Inconsistent separator in some oapi macros --- crates/oapi-macros/src/response/mod.rs | 4 +++ examples/__oapi-test/Cargo.toml | 13 ++++++++++ examples/__oapi-test/src/main.rs | 34 ++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 examples/__oapi-test/Cargo.toml create mode 100644 examples/__oapi-test/src/main.rs diff --git a/crates/oapi-macros/src/response/mod.rs b/crates/oapi-macros/src/response/mod.rs index bc8ffbf21..57c382f69 100644 --- a/crates/oapi-macros/src/response/mod.rs +++ b/crates/oapi-macros/src/response/mod.rs @@ -573,6 +573,10 @@ impl Parse for DeriveToResponsesValue { if response.status_code.to_token_stream().is_empty() { return Err(Error::new(first_span, MISSING_STATUS_ERROR)); } + + if !input.is_empty() { + input.parse::()?; + } while !input.is_empty() { let ident = input.parse::()?; diff --git a/examples/__oapi-test/Cargo.toml b/examples/__oapi-test/Cargo.toml new file mode 100644 index 000000000..99557469a --- /dev/null +++ b/examples/__oapi-test/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "example-oapi-test" +version.workspace = true +edition.workspace = true +publish.workspace = true + + +[dependencies] +salvo = { workspace = true, features = ["oapi"] } +tokio = { workspace = true, features = ["macros"] } +tracing.workspace = true +tracing-subscriber.workspace = true +serde = { workspace = true } \ No newline at end of file diff --git a/examples/__oapi-test/src/main.rs b/examples/__oapi-test/src/main.rs new file mode 100644 index 000000000..e73e20649 --- /dev/null +++ b/examples/__oapi-test/src/main.rs @@ -0,0 +1,34 @@ +use salvo::oapi::extract::*; +use salvo::prelude::*; +use serde::*; + +#[derive(Debug, Serialize, Deserialize, ToResponses)] +pub enum GeneralError { + #[salvo(response(status_code = 400, description = "Bad Request"))] + BadRequest(String), + #[salvo(response(status_code = 429, description = "Rate Limit Exceeded"))] + RateLimit { message: String, retry_after: u64 }, + #[salvo(response(status_code = 500, description = "Something went wrong"))] + InternalServerError(String), +} + +#[endpoint] +async fn hello(name: QueryParam) -> String { + format!("Hello, {}!", name.as_deref().unwrap_or("World")) +} + +#[tokio::main] +async fn main() { + tracing_subscriber::fmt().init(); + + let router = Router::new().push(Router::with_path("hello").get(hello)); + + let doc = OpenApi::new("test api", "0.0.1").merge_router(&router); + + let router = router + .unshift(doc.into_router("/api-doc/openapi.json")) + .unshift(SwaggerUi::new("/api-doc/openapi.json").into_router("/swagger-ui")); + + let acceptor = TcpListener::new("0.0.0.0:5800").bind().await; + Server::new(acceptor).serve(router).await; +} From f3620b77bb3a23a1799e0f6d2d21a54fd8068859 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 3 Jan 2025 02:06:44 +0000 Subject: [PATCH 2/2] Format Rust code using rustfmt --- crates/oapi-macros/src/response/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/oapi-macros/src/response/mod.rs b/crates/oapi-macros/src/response/mod.rs index 57c382f69..e861e4728 100644 --- a/crates/oapi-macros/src/response/mod.rs +++ b/crates/oapi-macros/src/response/mod.rs @@ -573,7 +573,7 @@ impl Parse for DeriveToResponsesValue { if response.status_code.to_token_stream().is_empty() { return Err(Error::new(first_span, MISSING_STATUS_ERROR)); } - + if !input.is_empty() { input.parse::()?; }