diff --git a/crates/oapi-macros/src/response/mod.rs b/crates/oapi-macros/src/response/mod.rs index bc8ffbf21..e861e4728 100644 --- a/crates/oapi-macros/src/response/mod.rs +++ b/crates/oapi-macros/src/response/mod.rs @@ -574,6 +574,10 @@ impl Parse for DeriveToResponsesValue { return Err(Error::new(first_span, MISSING_STATUS_ERROR)); } + if !input.is_empty() { + input.parse::()?; + } + while !input.is_empty() { let ident = input.parse::()?; let attr_name = &*ident.to_string(); 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; +}