#[derive(ToResponses)]
{
// Attributes available to this derive:
#[salvo]
}
Expand description
Generate responses with status codes what can be used in OpenApi.
This is #[derive] implementation of ToResponses trait. ToResponses
can be used to decorate structs and enums to generate response maps that can be used in
salvo_oapi::endpoint. If struct is decorated with ToResponses it will be
used to create a map of responses containing single response. Decorating enum with
ToResponses will create a map of responses with a response for each variant of the enum.
Named field struct decorated with ToResponses will create a response with inlined schema
generated from the body of the struct. This is a conveniency which allows users to directly
create responses with schemas without first creating a separate response type.
Unit struct behaves similarly to then named field struct. Only difference is that it will create
a response without content since there is no inner fields.
Unnamed field struct decorated with ToResponses will by default create a response with
referenced schema if field is object or schema if type is primitive
type. #[salvo(schema(...))] attribute at field of unnamed struct can be used to inline
the schema if type of the field implements ToSchema trait. Alternatively
#[to_response] and #[ref_response] can be used at field to either reference a reusable
response or inline a reusable response. In both cases the field
type is expected to implement ToResponse trait.
Enum decorated with ToResponses will create a response for each variant of the enum.
Each variant must have it’s own #[salvo(response(...))] definition. Unit variant will behave same
as unit struct by creating a response without content. Similarly named field variant and
unnamed field variant behaves the same as it was named field struct and unnamed field
struct.
#[response] attribute can be used at named structs, unnamed structs, unit structs and enum
variants to alter response attributes of responses.
Doc comment on a struct or enum variant will be used as a description for the response.
It can also be overridden with description = "..." attribute.
§ToResponses #[salvo(response(...))] attributes
-
status = ...Must be provided. Is either a valid http status code integer. E.g.200or a string value representing a range such as"4XX"or"default"or a validhttp::status::StatusCode.StatusCodecan either be use path to the status code or status code constant directly. -
description = "..."Define description for the response as str. This can be used to override the default description resolved from doc comments if present. -
content_type = "..." | content_type = [...]Can be used to override the default behavior of auto resolving the content type from thebodyattribute. If defined the value should be valid content type such asapplication/json. By default the content type istext/plainfor primitive Rust types,application/octet-streamfor[u8]andapplication/jsonfor struct and complex enum types. Content type can also be slice of content_type values if the endpoint support returning multiple response content types. E.g["application/json", "text/xml"]would indicate that endpoint can return bothjsonandxmlformats. The order of the content types define the default example show first in the Swagger UI. Swagger UI will use the firstcontent_typevalue as a default example. -
headers(...)Slice of response headers that are returned back to a caller. -
example = ...Can bejson!(...).json!(...)should be something thatserde_json::json!can parse as aserde_json::Value. -
examples(...)Define multiple examples for single response. This attribute is mutually exclusive to theexampleattribute and if both are defined this will override theexample.name = ...This is first attribute and value must be literal string.summary = ...Short description of example. Value must be literal string.description = ...Long description of example. Attribute supports markdown for rich text representation. Value must be literal string.value = ...Example value. It must bejson!(...).json!(...)should be something thatserde_json::json!can parse as aserde_json::Value.external_value = ...Define URI to literal example value. This is mutually exclusive to thevalueattribute. Value must be literal string.
Example of example definition.
("John" = (summary = "This is John", value = json!({"name": "John"})))
§Examples
Use ToResponses to define salvo_oapi::endpoint responses.
#[derive(salvo_oapi::ToSchema, Debug)]
struct BadRequest {
message: String,
}
#[derive(salvo_oapi::ToResponses, Debug)]
enum UserResponses {
/// Success response
#[salvo(response(status_code = 200))]
Success { value: String },
#[salvo(response(status_code = 404))]
NotFound,
#[salvo(response(status_code = 400))]
BadRequest(BadRequest),
}
impl Scribe for UserResponses {
fn render(self, res: &mut Response) {
res.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("text/plain; charset=utf-8"));
let _ = res.write_body(format!("{self:#?}"));
}
}
#[salvo_oapi::endpoint(
responses(
UserResponses
)
)]
async fn get_user() -> UserResponses {
UserResponses::NotFound
}Named struct response with inlined schema.
/// This is success response
#[derive(salvo_oapi::ToResponses)]
#[salvo(response(status_code = 200))]
struct SuccessResponse {
value: String,
}Unit struct response without content.
#[derive(salvo_oapi::ToResponses)]
#[salvo(response(status_code = NOT_FOUND))]
struct NotFound;Unnamed struct response with inlined response schema.
#[derive(salvo_oapi::ToResponses)]
#[salvo(response(status_code = 201))]
struct CreatedResponse(#[salvo(schema(...))] Foo);Enum with multiple responses.
#[derive(salvo_oapi::ToResponses)]
enum UserResponses {
/// Success response description.
#[salvo(response(status_code = 200))]
Success { value: String },
#[salvo(response(status_code = 404))]
NotFound,
#[salvo(response(status_code = 400))]
BadRequest(BadRequest),
#[salvo(response(status_code = 500))]
ServerError(Response),
#[salvo(response(status_code = 418))]
TeaPot(Response),
}Generate responses with status codes what can be used in OpenAPI, Read more.