Crates.io | okapi-response-mac |
lib.rs | okapi-response-mac |
version | 0.1.1 |
source | src |
created_at | 2024-10-19 02:52:38.053669 |
updated_at | 2024-10-24 20:46:00.051131 |
description | A macro for creating response code based on responses structs rather than typing and implementing OkapiRocketResponseInner manually. |
homepage | https://www.pickupleague.com/ |
repository | https://github.com/morphytron/okapi-response-macro.git |
max_upload_size | |
id | 1415036 |
size | 19,300 |
Make strongly typed Rocket responses while simultaneously adding to your openapi.json definitions.
Example 1
Uses three different structs that you could potentially return at an endpoint.
Also, the last bit of the macro pattern -> (500,501) indicates the responses that will be of type string.
It is not required for your business requirements, but it is for this macro.
You would need to respond an empty tuple -> ().
Note: usage requires the "paste" crate to be imported.
use rocket::get;
use rocket_okapi::openapi;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use okapi_response_mac::okapi_rocket_response;
use paste::paste;
use rocket::serde::json::Json;
#[derive(Default, JsonSchema, Serialize)]
pub struct A {
name: String,
email: String,
other: String,
things: String,
blah: u32,
}
#[derive(JsonSchema, Serialize)]
pub struct B {
name: String,
email: String,
}
#[derive(Serialize, JsonSchema)]
pub struct C {
name: String,
email: String,
}
okapi_rocket_response!(A : (Ok, 200, "application/json"), (Accept, 202, "application/json") ->
B : (Create, 203, "application/json") -> C : (NotFound, 404, "application/json") -> (500,501));
/// Creates documentation for OpenApi
#[openapi(tag = "User Versions")]
#[get("/")]
pub fn schema_derived() -> ABCR {
ABCR::Ok(Json::from(A {
name: "asdfasdf".to_string(),
email: "asdgasdg".to_string(),
other: "hghsfghs".to_string(),
things: "ertret".to_string(),
blah: 0,
}))
}
It combines your struct names into a single Enum followed by R to indicate a valid Rocket Response.