Skip to content

Commit

Permalink
fix(oapi): Inconsistent separator in some oapi macros (#1017)
Browse files Browse the repository at this point in the history
* fix(oapi): Inconsistent separator in some oapi macros

* Format Rust code using rustfmt

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
chrislearn and github-actions[bot] authored Jan 3, 2025
1 parent b3f2d1d commit 1d681d3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/oapi-macros/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ impl Parse for DeriveToResponsesValue {
return Err(Error::new(first_span, MISSING_STATUS_ERROR));
}

if !input.is_empty() {
input.parse::<Token![,]>()?;
}

while !input.is_empty() {
let ident = input.parse::<Ident>()?;
let attr_name = &*ident.to_string();
Expand Down
13 changes: 13 additions & 0 deletions examples/__oapi-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 }
34 changes: 34 additions & 0 deletions examples/__oapi-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -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, false>) -> 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;
}

0 comments on commit 1d681d3

Please sign in to comment.