From d4ff92c2ae6e3250fff9be1840dc5fa66bc3f375 Mon Sep 17 00:00:00 2001 From: Tim Fritzen Date: Tue, 19 Mar 2024 21:07:51 +0100 Subject: [PATCH] Added Generics to fn_name building to solve lifetimes issues from routes regarding issue #84. Also added an example to showcase the behaviour --- Cargo.toml | 1 + examples/lifetimes/Cargo.toml | 10 ++++ examples/lifetimes/src/main.rs | 55 ++++++++++++++++++++ rocket-okapi-codegen/src/openapi_attr/mod.rs | 3 +- 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 examples/lifetimes/Cargo.toml create mode 100644 examples/lifetimes/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index cddfb7f..7d40ba4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,6 @@ members = [ "examples/openapi_attributes", "examples/raw_identifiers", "examples/websocket", + "examples/lifetimes", ] resolver = "2" diff --git a/examples/lifetimes/Cargo.toml b/examples/lifetimes/Cargo.toml new file mode 100644 index 0000000..bea826b --- /dev/null +++ b/examples/lifetimes/Cargo.toml @@ -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" ] } diff --git a/examples/lifetimes/src/main.rs b/examples/lifetimes/src/main.rs new file mode 100644 index 0000000..7ffa7b0 --- /dev/null +++ b/examples/lifetimes/src/main.rs @@ -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 { + 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/")] +fn get_with_lifetimes<'a>(state: &'a State, val: bool) -> String { + val.to_string() +} diff --git a/rocket-okapi-codegen/src/openapi_attr/mod.rs b/rocket-okapi-codegen/src/openapi_attr/mod.rs index 67d88fe..6beec4c 100644 --- a/rocket-okapi-codegen/src/openapi_attr/mod.rs +++ b/rocket-okapi-codegen/src/openapi_attr/mod.rs @@ -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() @@ -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<()> {