Skip to content

Commit

Permalink
Added Generics to fn_name building to solve lifetimes issues from rou…
Browse files Browse the repository at this point in the history
…tes regarding issue GREsau#84.

Also added an example to showcase the behaviour
  • Loading branch information
PixelboysTM committed Mar 19, 2024
1 parent 1608ff7 commit d4ff92c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ members = [
"examples/openapi_attributes",
"examples/raw_identifiers",
"examples/websocket",
"examples/lifetimes",
]
resolver = "2"
10 changes: 10 additions & 0 deletions examples/lifetimes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "lifetimes"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] }
rocket_okapi = { path = "../../rocket-okapi", features = [ "swagger", "rapidoc" ] }
55 changes: 55 additions & 0 deletions examples/lifetimes/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use rocket::{Build, get, Rocket, State};
use rocket_okapi::{mount_endpoints_and_merged_docs, openapi, openapi_get_routes_spec};
use rocket_okapi::rapidoc::{GeneralConfig, HideShowConfig, make_rapidoc, RapiDocConfig};
use rocket_okapi::settings::{OpenApiSettings, UrlObject};
use rocket_okapi::swagger_ui::{make_swagger_ui, SwaggerUIConfig};

#[rocket::main]
async fn main() {
let launch_result = create_server().launch().await;
match launch_result {
Ok(_) => println!("Rocket shut down gracefully."),
Err(err) => println!("Rocket had an error: {}", err),
};
}

pub fn create_server() -> Rocket<Build> {
let mut building_rocket = rocket::build()
.mount(
"/swagger-ui/",
make_swagger_ui(&SwaggerUIConfig {
url: "../v1/openapi.json".to_owned(),
..Default::default()
}),
)
.mount(
"/rapidoc/",
make_rapidoc(&RapiDocConfig {
title: Some("My special documentation | RapiDoc".to_owned()),
general: GeneralConfig {
spec_urls: vec![UrlObject::new("General", "../v1/openapi.json")],
..Default::default()
},
hide_show: HideShowConfig {
allow_spec_url_load: false,
allow_spec_file_load: false,
..Default::default()
},
..Default::default()
}),
);


let (route, spec) = openapi_get_routes_spec![get_with_lifetimes];


building_rocket.manage(Test).mount("/v1",route).mount("/v1/", vec![rocket_okapi::handlers::OpenApiHandler::new(spec).into_route(OpenApiSettings::new().json_path)])
}

struct Test;

#[openapi]
#[get("/get/<val>")]
fn get_with_lifetimes<'a>(state: &'a State<Test>, val: bool) -> String {
val.to_string()
}
3 changes: 2 additions & 1 deletion rocket-okapi-codegen/src/openapi_attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ fn create_route_operation_fn(
}

let fn_name = get_add_operation_fn_name(&route_fn.sig.ident);
let generics = route_fn.sig.generics;
let path = route
.origin
.path()
Expand Down Expand Up @@ -322,7 +323,7 @@ fn create_route_operation_fn(

TokenStream::from(quote! {
#[doc(hidden)]
pub fn #fn_name(
pub fn #fn_name #generics(
gen: &mut ::rocket_okapi::gen::OpenApiGenerator,
operation_id: String,
) -> ::rocket_okapi::Result<()> {
Expand Down

0 comments on commit d4ff92c

Please sign in to comment.