1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
#![doc = include_str!("../docs/lib.md")]
#![doc(html_favicon_url = "https://salvo.rs/favicon-32x32.png")]
#![doc(html_logo_url = "https://salvo.rs/images/logo.svg")]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[macro_use]
mod cfg;
mod openapi;
pub use openapi::*;
#[doc = include_str!("../docs/endpoint.md")]
pub mod endpoint;
pub use endpoint::{Endpoint, EndpointArgRegister, EndpointOutRegister, EndpointRegistry};
pub mod extract;
mod routing;
pub use routing::RouterExt;
cfg_feature! {
#![feature ="swagger-ui"]
pub mod swagger_ui;
}
cfg_feature! {
#![feature ="scalar"]
pub mod scalar;
}
cfg_feature! {
#![feature ="rapidoc"]
pub mod rapidoc;
}
cfg_feature! {
#![feature ="redoc"]
pub mod redoc;
}
#[doc = include_str!("../docs/endpoint.md")]
pub use salvo_oapi_macros::endpoint;
pub(crate) use salvo_oapi_macros::schema;
#[doc = include_str!("../docs/derive_to_parameters.md")]
pub use salvo_oapi_macros::ToParameters;
#[doc = include_str!("../docs/derive_to_response.md")]
pub use salvo_oapi_macros::ToResponse;
#[doc = include_str!("../docs/derive_to_responses.md")]
pub use salvo_oapi_macros::ToResponses;
#[doc = include_str!("../docs/derive_to_schema.md")]
pub use salvo_oapi_macros::ToSchema;
use std::collections::{BTreeMap, HashMap, LinkedList};
use std::marker::PhantomData;
use salvo_core::http::StatusError;
use salvo_core::{extract::Extractible, writing};
use crate::oapi::openapi::schema::OneOf;
// https://github.com/bkchr/proc-macro-crate/issues/10
extern crate self as salvo_oapi;
/// Trait for implementing OpenAPI Schema object.
///
/// Generated schemas can be referenced or reused in path operations.
///
/// This trait is derivable and can be used with `[#derive]` attribute. For a details of
/// `#[derive(ToSchema)]` refer to [derive documentation][derive].
///
/// [derive]: derive.ToSchema.html
///
/// # Examples
///
/// Use `#[derive]` to implement `ToSchema` trait.
/// ```
/// use salvo_oapi::ToSchema;
/// #[derive(ToSchema)]
/// #[salvo(schema(example = json!({"name": "bob the cat", "id": 1})))]
/// struct Pet {
/// id: u64,
/// name: String,
/// age: Option<i32>,
/// }
/// ```
///
/// Following manual implementation is equal to above derive one.
/// ```
/// use salvo_oapi::{Components, ToSchema, RefOr, Schema, SchemaFormat, SchemaType, KnownFormat, Object};
/// # struct Pet {
/// # id: u64,
/// # name: String,
/// # age: Option<i32>,
/// # }
/// #
/// impl ToSchema for Pet {
/// fn to_schema(components: &mut Components) -> RefOr<Schema> {
/// Object::new()
/// .property(
/// "id",
/// Object::new()
/// .schema_type(SchemaType::Integer)
/// .format(SchemaFormat::KnownFormat(
/// KnownFormat::Int64,
/// )),
/// )
/// .required("id")
/// .property(
/// "name",
/// Object::new()
/// .schema_type(SchemaType::String),
/// )
/// .required("name")
/// .property(
/// "age",
/// Object::new()
/// .schema_type(SchemaType::Integer)
/// .format(SchemaFormat::KnownFormat(
/// KnownFormat::Int32,
/// )),
/// )
/// .example(serde_json::json!({
/// "name":"bob the cat","id":1
/// }))
/// .into()
/// }
/// }
/// ```
pub trait ToSchema {
/// Returns a tuple of name and schema or reference to a schema that can be referenced by the
/// name or inlined directly to responses, request bodies or parameters.
fn to_schema(components: &mut Components) -> RefOr<schema::Schema>;
}
/// Represents _`nullable`_ type. This can be used anywhere where "nothing" needs to be evaluated.
/// This will serialize to _`null`_ in JSON and [`schema::empty`] is used to create the
/// [`schema::Schema`] for the type.
pub type TupleUnit = ();
impl ToSchema for TupleUnit {
fn to_schema(_components: &mut Components) -> RefOr<schema::Schema> {
schema::empty().into()
}
}
macro_rules! impl_to_schema {
($ty:path) => {
impl_to_schema!( @impl_schema $ty );
};
(&$ty:path) => {
impl_to_schema!( @impl_schema &$ty );
};
(@impl_schema $($tt:tt)*) => {
impl ToSchema for $($tt)* {
fn to_schema(_components: &mut Components) -> crate::RefOr<crate::schema::Schema> {
schema!( $($tt)* ).into()
}
}
};
}
macro_rules! impl_to_schema_primitive {
($($tt:path),*) => {
$( impl_to_schema!( $tt ); )*
};
}
// Create `salvo-oapi` module so we can use `salvo-oapi-macros` directly
// from `salvo-oapi` crate. ONLY FOR INTERNAL USE!
#[doc(hidden)]
pub mod oapi {
pub use super::*;
}
#[doc(hidden)]
pub mod __private {
pub use inventory;
pub use serde_json;
}
#[rustfmt::skip]
impl_to_schema_primitive!(
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, bool, f32, f64, String, str, char
);
impl_to_schema!(&str);
#[cfg(feature = "chrono")]
impl_to_schema_primitive!(chrono::NaiveDate, chrono::Duration, chrono::NaiveDateTime);
#[cfg(feature = "chrono")]
impl<T: chrono::TimeZone> ToSchema for chrono::DateTime<T> {
fn to_schema(_components: &mut Components) -> RefOr<schema::Schema> {
schema!(#[inline] DateTime<T>).into()
}
}
#[cfg(any(feature = "decimal", feature = "decimal-float"))]
impl_to_schema!(rust_decimal::Decimal);
#[cfg(feature = "url")]
impl_to_schema!(url::Url);
#[cfg(feature = "uuid")]
impl_to_schema!(uuid::Uuid);
#[cfg(feature = "ulid")]
impl_to_schema!(ulid::Ulid);
#[cfg(feature = "time")]
impl_to_schema_primitive!(
time::Date,
time::PrimitiveDateTime,
time::OffsetDateTime,
time::Duration
);
#[cfg(feature = "smallvec")]
impl<T: ToSchema + smallvec::Array> ToSchema for smallvec::SmallVec<T> {
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
schema!(#[inline] smallvec::SmallVec<T>).into()
}
}
#[cfg(feature = "indexmap")]
impl<K: ToSchema, V: ToSchema> ToSchema for indexmap::IndexMap<K, V> {
fn to_schema(_components: &mut Components) -> RefOr<schema::Schema> {
schema!(#[inline] indexmap::IndexMap<K, V>).into()
}
}
impl<T: ToSchema> ToSchema for Vec<T> {
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
schema!(#[inline] Vec<T>).into()
}
}
impl<T: ToSchema> ToSchema for LinkedList<T> {
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
schema!(#[inline] LinkedList<T>).into()
}
}
impl<T: ToSchema> ToSchema for [T] {
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
schema!(
#[inline]
[T]
)
.into()
}
}
impl<T: ToSchema, const N: usize> ToSchema for [T; N] {
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
schema!(
#[inline]
[T; N]
)
.into()
}
}
impl<T: ToSchema> ToSchema for &[T] {
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
schema!(
#[inline]
&[T]
)
.into()
}
}
impl<T: ToSchema> ToSchema for Option<T> {
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
schema!(#[inline] Option<T>).into()
}
}
impl<T> ToSchema for PhantomData<T> {
fn to_schema(_components: &mut Components) -> RefOr<schema::Schema> {
Schema::Object(Object::default()).into()
}
}
impl<K: ToSchema, V: ToSchema> ToSchema for BTreeMap<K, V> {
fn to_schema(_components: &mut Components) -> RefOr<schema::Schema> {
schema!(#[inline]BTreeMap<K, V>).into()
}
}
impl<K: ToSchema, V: ToSchema> ToSchema for HashMap<K, V> {
fn to_schema(_components: &mut Components) -> RefOr<schema::Schema> {
schema!(#[inline]HashMap<K, V>).into()
}
}
impl ToSchema for StatusError {
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
let symbol = std::any::type_name::<StatusError>().replace("::", ".");
let schema = Schema::from(
Object::new()
.property("code", u16::to_schema(components))
.required("code")
.required("name")
.property("name", String::to_schema(components))
.required("brief")
.property("brief", String::to_schema(components))
.required("detail")
.property("detail", String::to_schema(components))
.property("cause", String::to_schema(components)),
);
components.schemas.insert(symbol.clone(), schema.into());
crate::RefOr::Ref(crate::Ref::new(format!("#/components/schemas/{}", symbol)))
}
}
impl ToSchema for salvo_core::Error {
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
StatusError::to_schema(components)
}
}
impl<T, E> ToSchema for Result<T, E>
where
T: ToSchema,
E: ToSchema,
{
fn to_schema(components: &mut Components) -> RefOr<schema::Schema> {
let symbol = std::any::type_name::<Self>().replace("::", ".");
let schema = OneOf::new()
.item(T::to_schema(components))
.item(E::to_schema(components));
components.schemas.insert(symbol.clone(), schema.into());
crate::RefOr::Ref(crate::Ref::new(format!("#/components/schemas/{}", symbol)))
}
}
impl ToSchema for serde_json::Value {
fn to_schema(_components: &mut Components) -> RefOr<schema::Schema> {
Schema::Object(Object::default()).into()
}
}
impl ToSchema for serde_json::Map<String, serde_json::Value> {
fn to_schema(_components: &mut Components) -> RefOr<schema::Schema> {
schema!(#[inline]HashMap<K, V>).into()
}
}
/// Trait used to convert implementing type to OpenAPI parameters.
///
/// This trait is [derivable][derive] for structs which are used to describe `path` or `query` parameters.
/// For more details of `#[derive(ToParameters)]` refer to [derive documentation][derive].
///
/// # Examples
///
/// Derive [`ToParameters`] implementation. This example will fail to compile because [`ToParameters`] cannot
/// be used alone and it need to be used together with endpoint using the params as well. See
/// [derive documentation][derive] for more details.
/// ```
/// use serde::Deserialize;
/// use salvo_oapi::{ToParameters, EndpointArgRegister, Components, Operation};
/// use salvo_core::prelude::*;
///
/// #[derive(Deserialize, ToParameters)]
/// struct PetParams {
/// /// Id of pet
/// id: i64,
/// /// Name of pet
/// name: String,
/// }
/// ```
///
/// Roughly equal manual implementation of [`ToParameters`] trait.
/// ```
/// # use serde::Deserialize;
/// # use salvo_oapi::{ToParameters, EndpointArgRegister, Components, Operation};
/// # use salvo_core::prelude::*;
/// # use salvo_core::extract::{Metadata, Extractible};
/// #[derive(Deserialize)]
/// # struct PetParams {
/// # /// Id of pet
/// # id: i64,
/// # /// Name of pet
/// # name: String,
/// # }
/// impl<'de> salvo_oapi::ToParameters<'de> for PetParams {
/// fn to_parameters(_components: &mut Components) -> salvo_oapi::Parameters {
/// salvo_oapi::Parameters::new().parameter(
/// salvo_oapi::Parameter::new("id")
/// .required(salvo_oapi::Required::True)
/// .parameter_in(salvo_oapi::ParameterIn::Path)
/// .description("Id of pet")
/// .schema(
/// salvo_oapi::Object::new()
/// .schema_type(salvo_oapi::SchemaType::Integer)
/// .format(salvo_oapi::SchemaFormat::KnownFormat(salvo_oapi::schema::KnownFormat::Int64)),
/// ),
/// ).parameter(
/// salvo_oapi::Parameter::new("name")
/// .required(salvo_oapi::Required::True)
/// .parameter_in(salvo_oapi::ParameterIn::Query)
/// .description("Name of pet")
/// .schema(
/// salvo_oapi::Object::new()
/// .schema_type(salvo_oapi::SchemaType::String),
/// ),
/// )
/// }
/// }
///
/// impl<'ex> Extractible<'ex> for PetParams {
/// fn metadata() -> &'ex Metadata {
/// static METADATA: Metadata = Metadata::new("");
/// &METADATA
/// }
/// async fn extract(req: &'ex mut Request) -> Result<Self, salvo_core::http::ParseError> {
/// salvo_core::serde::from_request(req, Self::metadata()).await
/// }
/// async fn extract_with_arg(req: &'ex mut Request, _arg: &str) -> Result<Self, salvo_core::http::ParseError> {
/// Self::extract(req).await
/// }
/// }
///
/// impl EndpointArgRegister for PetParams {
/// fn register(components: &mut Components, operation: &mut Operation, _arg: &str) {
/// operation.parameters.append(&mut PetParams::to_parameters(components));
/// }
/// }
/// ```
/// [derive]: derive.ToParameters.html
pub trait ToParameters<'de>: Extractible<'de> {
/// Provide [`Vec`] of [`Parameter`]s to caller. The result is used in `salvo-oapi-macros` library to
/// provide OpenAPI parameter information for the endpoint using the parameters.
fn to_parameters(components: &mut Components) -> Parameters;
}
/// Trait used to give [`Parameter`] information for OpenAPI.
pub trait ToParameter {
/// Returns a `Parameter`.
fn to_parameter(components: &mut Components) -> Parameter;
}
/// This trait is implemented to document a type (like an enum) which can represent
/// request body, to be used in operation.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use serde::Deserialize;
/// use salvo_oapi::{ToRequestBody, ToSchema, Components, Content, EndpointArgRegister, Operation, RequestBody };
///
/// #[derive(ToSchema, Deserialize, Debug)]
/// struct MyPayload {
/// name: String,
/// }
///
/// impl ToRequestBody for MyPayload {
/// fn to_request_body(components: &mut Components) -> RequestBody {
/// RequestBody::new()
/// .add_content("application/json", Content::new(MyPayload::to_schema(components)))
/// }
/// }
/// impl EndpointArgRegister for MyPayload {
/// fn register(components: &mut Components, operation: &mut Operation, _arg: &str) {
/// operation.request_body = Some(Self::to_request_body(components));
/// }
/// }
/// ```
pub trait ToRequestBody {
/// Returns `RequestBody`.
fn to_request_body(components: &mut Components) -> RequestBody;
}
/// This trait is implemented to document a type (like an enum) which can represent multiple
/// responses, to be used in operation.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use salvo_oapi::{Components, Response, Responses, RefOr, ToResponses };
///
/// enum MyResponse {
/// Ok,
/// NotFound,
/// }
///
/// impl ToResponses for MyResponse {
/// fn to_responses(_components: &mut Components) -> Responses {
/// Responses::new()
/// .response("200", Response::new("Ok"))
/// .response("404", Response::new("Not Found"))
/// }
/// }
/// ```
pub trait ToResponses {
/// Returns an ordered map of response codes to responses.
fn to_responses(components: &mut Components) -> Responses;
}
impl<C> ToResponses for writing::Json<C>
where
C: ToSchema,
{
fn to_responses(components: &mut Components) -> Responses {
Responses::new().response(
"200",
Response::new("Response json format data")
.add_content("application/json", Content::new(C::to_schema(components))),
)
}
}
impl ToResponses for StatusError {
fn to_responses(components: &mut Components) -> Responses {
let mut responses = Responses::new();
let errors = vec![
StatusError::bad_request(),
StatusError::unauthorized(),
StatusError::payment_required(),
StatusError::forbidden(),
StatusError::not_found(),
StatusError::method_not_allowed(),
StatusError::not_acceptable(),
StatusError::proxy_authentication_required(),
StatusError::request_timeout(),
StatusError::conflict(),
StatusError::gone(),
StatusError::length_required(),
StatusError::precondition_failed(),
StatusError::payload_too_large(),
StatusError::uri_too_long(),
StatusError::unsupported_media_type(),
StatusError::range_not_satisfiable(),
StatusError::expectation_failed(),
StatusError::im_a_teapot(),
StatusError::misdirected_request(),
StatusError::unprocessable_entity(),
StatusError::locked(),
StatusError::failed_dependency(),
StatusError::upgrade_required(),
StatusError::precondition_required(),
StatusError::too_many_requests(),
StatusError::request_header_fields_toolarge(),
StatusError::unavailable_for_legalreasons(),
StatusError::internal_server_error(),
StatusError::not_implemented(),
StatusError::bad_gateway(),
StatusError::service_unavailable(),
StatusError::gateway_timeout(),
StatusError::http_version_not_supported(),
StatusError::variant_also_negotiates(),
StatusError::insufficient_storage(),
StatusError::loop_detected(),
StatusError::not_extended(),
StatusError::network_authentication_required(),
];
for StatusError { code, brief, .. } in errors {
responses.insert(
code.as_str(),
Response::new(brief).add_content("application/json", Content::new(StatusError::to_schema(components))),
)
}
responses
}
}
impl ToResponses for salvo_core::Error {
fn to_responses(components: &mut Components) -> Responses {
StatusError::to_responses(components)
}
}
/// This trait is implemented to document a type which represents a single response which can be
/// referenced or reused as a component in multiple operations.
///
/// _`ToResponse`_ trait can also be derived with [`#[derive(ToResponse)]`][derive].
///
/// # Examples
///
/// ```
/// use salvo_oapi::{RefOr, Response, Components, ToResponse};
///
/// struct MyResponse;
/// impl ToResponse for MyResponse {
/// fn to_response(_components: &mut Components) -> RefOr<Response> {
/// Response::new("My Response").into()
/// }
/// }
/// ```
///
/// [derive]: derive.ToResponse.html
pub trait ToResponse {
/// Returns a tuple of response component name (to be referenced) to a response.
fn to_response(components: &mut Components) -> RefOr<crate::Response>;
}
impl<C> ToResponse for writing::Json<C>
where
C: ToSchema,
{
fn to_response(components: &mut Components) -> RefOr<Response> {
let schema = <C as ToSchema>::to_schema(components);
Response::new("Response with json format data")
.add_content("application/json", Content::new(schema))
.into()
}
}
#[cfg(test)]
mod tests {
use assert_json_diff::assert_json_eq;
use serde_json::json;
use super::*;
#[test]
fn test_primitive_schema() {
let mut components = Components::new();
for (name, schema, value) in [
(
"i8",
i8::to_schema(&mut components),
json!({"type": "integer", "format": "int32"}),
),
(
"i16",
i16::to_schema(&mut components),
json!({"type": "integer", "format": "int32"}),
),
(
"i32",
i32::to_schema(&mut components),
json!({"type": "integer", "format": "int32"}),
),
(
"i64",
i64::to_schema(&mut components),
json!({"type": "integer", "format": "int64"}),
),
("i128", i128::to_schema(&mut components), json!({"type": "integer"})),
("isize", isize::to_schema(&mut components), json!({"type": "integer"})),
(
"u8",
u8::to_schema(&mut components),
json!({"type": "integer", "format": "int32", "minimum": 0.0}),
),
(
"u16",
u16::to_schema(&mut components),
json!({"type": "integer", "format": "int32", "minimum": 0.0}),
),
(
"u32",
u32::to_schema(&mut components),
json!({"type": "integer", "format": "int32", "minimum": 0.0}),
),
(
"u64",
u64::to_schema(&mut components),
json!({"type": "integer", "format": "int64", "minimum": 0.0}),
),
(
"u128",
u128::to_schema(&mut components),
json!({"type": "integer", "minimum": 0.0}),
),
(
"usize",
usize::to_schema(&mut components),
json!({"type": "integer", "minimum": 0.0 }),
),
("bool", bool::to_schema(&mut components), json!({"type": "boolean"})),
("str", str::to_schema(&mut components), json!({"type": "string"})),
("String", String::to_schema(&mut components), json!({"type": "string"})),
("char", char::to_schema(&mut components), json!({"type": "string"})),
(
"f32",
f32::to_schema(&mut components),
json!({"type": "number", "format": "float"}),
),
(
"f64",
f64::to_schema(&mut components),
json!({"type": "number", "format": "double"}),
),
] {
println!("{name}: {json}", json = serde_json::to_string(&schema).unwrap());
let schema = serde_json::to_value(schema).unwrap();
assert_json_eq!(schema, value);
}
}
}