From d4ff92c2ae6e3250fff9be1840dc5fa66bc3f375 Mon Sep 17 00:00:00 2001 From: Tim Fritzen Date: Tue, 19 Mar 2024 21:07:51 +0100 Subject: [PATCH 1/2] 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<()> { From 9af2c9ea20cbcba26f4bf31b89f576d7ae898c16 Mon Sep 17 00:00:00 2001 From: Tim Fritzen Date: Mon, 19 Aug 2024 14:36:41 +0200 Subject: [PATCH 2/2] updated to rocket 0.5.1 --- examples/custom_schema/Cargo.toml | 9 +++++--- examples/dyn_templates/Cargo.toml | 12 ++++++---- examples/json-web-api/Cargo.toml | 9 +++++--- examples/lifetimes/Cargo.toml | 7 ++++-- examples/nested/Cargo.toml | 4 ++-- examples/openapi_attributes/Cargo.toml | 9 +++++--- examples/raw_identifiers/Cargo.toml | 9 +++++--- examples/secure_request_guard/Cargo.toml | 11 ++++++++-- examples/special-types/Cargo.toml | 9 +++++--- examples/streams/Cargo.toml | 9 +++++--- examples/uuid_usage/Cargo.toml | 15 +++++++++---- examples/websocket/Cargo.toml | 10 ++++++--- rocket-okapi-codegen/Cargo.toml | 8 +++---- rocket-okapi/Cargo.toml | 28 ++++++++++++------------ 14 files changed, 96 insertions(+), 53 deletions(-) diff --git a/examples/custom_schema/Cargo.toml b/examples/custom_schema/Cargo.toml index ee37f5a..0924cc2 100644 --- a/examples/custom_schema/Cargo.toml +++ b/examples/custom_schema/Cargo.toml @@ -1,11 +1,14 @@ [package] name = "custom_schema" version = "0.1.0" -authors = [ "Ralph Bisschops " ] +authors = ["Ralph Bisschops "] edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] } -rocket_okapi = { path = "../../rocket-okapi", features = [ "swagger", "rapidoc" ] } +rocket = { version = "0.5.1", default-features = false, features = ["json"] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "swagger", + "rapidoc", +] } serde = "1.0" serde_json = "1.0" diff --git a/examples/dyn_templates/Cargo.toml b/examples/dyn_templates/Cargo.toml index ed5022f..60e5c61 100644 --- a/examples/dyn_templates/Cargo.toml +++ b/examples/dyn_templates/Cargo.toml @@ -1,13 +1,17 @@ [package] name = "dyn_templates" version = "0.1.0" -authors = [ "Ralph Bisschops " ] +authors = ["Ralph Bisschops "] edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] } +rocket = { version = "0.5.1", default-features = false, features = ["json"] } schemars = { version = "0.8" } -rocket_okapi = { path = "../../rocket-okapi", features = [ "swagger", "rapidoc", "rocket_dyn_templates" ] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "swagger", + "rapidoc", + "rocket_dyn_templates", +] } serde = "1.0" -rocket_dyn_templates = { version = "=0.1.0", features = [ "handlebars" ] } +rocket_dyn_templates = { version = "=0.1.0", features = ["handlebars"] } handlebars = "5.0.0" diff --git a/examples/json-web-api/Cargo.toml b/examples/json-web-api/Cargo.toml index 743f19e..d9ede47 100644 --- a/examples/json-web-api/Cargo.toml +++ b/examples/json-web-api/Cargo.toml @@ -1,10 +1,13 @@ [package] name = "json_web_api" version = "0.1.0" -authors = [ "Graham Esau " ] +authors = ["Graham Esau "] edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] } -rocket_okapi = { path = "../../rocket-okapi", features = [ "swagger", "rapidoc" ] } +rocket = { version = "=0.5.1", default-features = false, features = ["json"] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "swagger", + "rapidoc", +] } serde = "1.0" diff --git a/examples/lifetimes/Cargo.toml b/examples/lifetimes/Cargo.toml index bea826b..b0c23a7 100644 --- a/examples/lifetimes/Cargo.toml +++ b/examples/lifetimes/Cargo.toml @@ -6,5 +6,8 @@ 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" ] } +rocket = { version = "0.5.1", default-features = false, features = ["json"] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "swagger", + "rapidoc", +] } diff --git a/examples/nested/Cargo.toml b/examples/nested/Cargo.toml index 4e3d48b..3ef3dc2 100644 --- a/examples/nested/Cargo.toml +++ b/examples/nested/Cargo.toml @@ -8,7 +8,7 @@ authors = [ edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] } -rocket_okapi = { path = "../../rocket-okapi", features = [ "rapidoc" ] } +rocket = { version = "0.5.1", default-features = false, features = ["json"] } +rocket_okapi = { path = "../../rocket-okapi", features = ["rapidoc"] } serde = "1.0" serde_json = "1.0" diff --git a/examples/openapi_attributes/Cargo.toml b/examples/openapi_attributes/Cargo.toml index e656152..64813c3 100644 --- a/examples/openapi_attributes/Cargo.toml +++ b/examples/openapi_attributes/Cargo.toml @@ -1,10 +1,13 @@ [package] name = "openapi_attributes" version = "0.1.0" -authors = [ "Ralph Bisschops " ] +authors = ["Ralph Bisschops "] edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] } -rocket_okapi = { path = "../../rocket-okapi", features = [ "swagger", "rapidoc" ] } +rocket = { version = "0.5.1", default-features = false, features = ["json"] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "swagger", + "rapidoc", +] } serde = "1.0" diff --git a/examples/raw_identifiers/Cargo.toml b/examples/raw_identifiers/Cargo.toml index e2b5792..01a219f 100644 --- a/examples/raw_identifiers/Cargo.toml +++ b/examples/raw_identifiers/Cargo.toml @@ -1,11 +1,14 @@ [package] name = "raw_identifiers" version = "0.1.0" -authors = [ "Alex Payne " ] +authors = ["Alex Payne "] 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 = [ "rapidoc", "swagger" ] } +rocket = { version = "0.5.1", default-features = false, features = ["json"] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "rapidoc", + "swagger", +] } diff --git a/examples/secure_request_guard/Cargo.toml b/examples/secure_request_guard/Cargo.toml index 0db1920..413cd85 100644 --- a/examples/secure_request_guard/Cargo.toml +++ b/examples/secure_request_guard/Cargo.toml @@ -8,8 +8,15 @@ authors = [ edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json", "secrets" ] } -rocket_okapi = { path = "../../rocket-okapi", features = [ "rapidoc", "swagger", "secrets" ] } +rocket = { version = "0.5.1", default-features = false, features = [ + "json", + "secrets", +] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "rapidoc", + "swagger", + "secrets", +] } serde = "1.0" tokio = "1.6" serde_json = "1.0" diff --git a/examples/special-types/Cargo.toml b/examples/special-types/Cargo.toml index 1e44b77..bc547d1 100644 --- a/examples/special-types/Cargo.toml +++ b/examples/special-types/Cargo.toml @@ -1,10 +1,13 @@ [package] name = "special-types" version = "0.1.0" -authors = [ "Ralph Bisschops " ] +authors = ["Ralph Bisschops "] edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] } -rocket_okapi = { path = "../../rocket-okapi", features = [ "swagger", "rapidoc" ] } +rocket = { version = "0.5.1", default-features = false, features = ["json"] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "swagger", + "rapidoc", +] } serde = "1.0" diff --git a/examples/streams/Cargo.toml b/examples/streams/Cargo.toml index 0793b3c..a9b5b6c 100644 --- a/examples/streams/Cargo.toml +++ b/examples/streams/Cargo.toml @@ -1,10 +1,13 @@ [package] name = "streams" version = "0.1.0" -authors = [ "Ralph Bisschops " ] +authors = ["Ralph Bisschops "] edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] } -rocket_okapi = { path = "../../rocket-okapi", features = [ "swagger", "rapidoc" ] } +rocket = { version = "0.5.1", default-features = false, features = ["json"] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "swagger", + "rapidoc", +] } serde = "1.0" diff --git a/examples/uuid_usage/Cargo.toml b/examples/uuid_usage/Cargo.toml index 16fecd8..e1f0be3 100644 --- a/examples/uuid_usage/Cargo.toml +++ b/examples/uuid_usage/Cargo.toml @@ -9,8 +9,15 @@ authors = [ edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json", "uuid" ] } -schemars = { version = "0.8", features = [ "uuid1" ] } -rocket_okapi = { path = "../../rocket-okapi", features = [ "swagger", "rapidoc", "uuid" ] } +rocket = { version = "=0.5.1", default-features = false, features = [ + "json", + "uuid", +] } +schemars = { version = "0.8", features = ["uuid1"] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "swagger", + "rapidoc", + "uuid", +] } serde = "1.0" -uuid = { version = "1.1.1", features = [ "v4" ] } +uuid = { version = "1.1.1", features = ["v4"] } diff --git a/examples/websocket/Cargo.toml b/examples/websocket/Cargo.toml index 4e36039..2a18346 100644 --- a/examples/websocket/Cargo.toml +++ b/examples/websocket/Cargo.toml @@ -1,11 +1,15 @@ [package] name = "websocket" version = "0.1.0" -authors = [ "Ralph Bisschops " ] +authors = ["Ralph Bisschops "] edition = "2021" [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] } +rocket = { version = "0.5.1", default-features = false, features = ["json"] } rocket_ws = "0.1.0" -rocket_okapi = { path = "../../rocket-okapi", features = [ "swagger", "rapidoc", "rocket_ws" ] } +rocket_okapi = { path = "../../rocket-okapi", features = [ + "swagger", + "rapidoc", + "rocket_ws", +] } serde = "1.0" diff --git a/rocket-okapi-codegen/Cargo.toml b/rocket-okapi-codegen/Cargo.toml index 9247a70..07f13ef 100644 --- a/rocket-okapi-codegen/Cargo.toml +++ b/rocket-okapi-codegen/Cargo.toml @@ -3,18 +3,18 @@ name = "rocket_okapi_codegen" description = "Macros supporting rocket_okapi" repository = "https://github.com/GREsau/okapi" version = "0.8.0" -authors = [ "Graham Esau " ] +authors = ["Graham Esau "] edition = "2021" license = "MIT" readme = "../README.md" -keywords = [ "rust", "openapi", "swagger", "rocket" ] -categories = [ "web-programming" ] +keywords = ["rust", "openapi", "swagger", "rocket"] +categories = ["web-programming"] [lib] proc-macro = true [dependencies] -rocket_http = { version = "=0.5.0" } +rocket_http = { version = "0.5.1" } darling = "0.13" syn = "1.0" proc-macro2 = "1.0" diff --git a/rocket-okapi/Cargo.toml b/rocket-okapi/Cargo.toml index 070a1a6..b4fa45d 100644 --- a/rocket-okapi/Cargo.toml +++ b/rocket-okapi/Cargo.toml @@ -3,15 +3,15 @@ name = "rocket_okapi" description = "OpenAPI (AKA Swagger) document generation for Rocket applications" repository = "https://github.com/GREsau/okapi" version = "0.8.0" -authors = [ "Graham Esau " ] +authors = ["Graham Esau "] edition = "2021" license = "MIT" readme = "../README.md" -keywords = [ "rust", "openapi", "swagger", "rocket" ] -categories = [ "web-programming" ] +keywords = ["rust", "openapi", "swagger", "rocket"] +categories = ["web-programming"] [dependencies] -rocket = { version = "=0.5.0", default-features = false, features = [ "json" ] } +rocket = { version = "=0.5.1", default-features = false, features = ["json"] } schemars = { version = "0.8.16" } okapi = { version = "0.7.0", path = "../okapi" } rocket_okapi_codegen = { version = "=0.8.0", path = "../rocket-okapi-codegen" } @@ -24,33 +24,33 @@ log = "0.4" rocket_dyn_templates = { version = "=0.1.0", optional = true } rocket_db_pools = { version = "=0.1.0", optional = true } rocket_sync_db_pools = { version = "=0.1.0", optional = true } -rocket_ws = { version = "=0.1.0", optional = true } +rocket_ws = { version = "0.1.1", optional = true } [dev-dependencies] -rocket_sync_db_pools = { version = "0.1.0", features = [ "diesel_sqlite_pool" ] } +rocket_sync_db_pools = { version = "0.1.0", features = ["diesel_sqlite_pool"] } [features] -default = [ "preserve_order" ] +default = ["preserve_order"] # Preserve the order of items in schema and other part of the OpenAPI documentation. -preserve_order = [ "schemars/preserve_order", "okapi/preserve_order" ] +preserve_order = ["schemars/preserve_order", "okapi/preserve_order"] # Feature to enable Swagger UI for rendering documentation # Project: https://github.com/swagger-api/swagger-ui -swagger = [ ] +swagger = [] # Feature to enable RapiDoc for rendering documentation # Project: https://github.com/mrin9/RapiDoc -rapidoc = [ ] +rapidoc = [] # Allow the use of UUIDs -uuid = [ "rocket/uuid", "schemars/uuid" ] +uuid = ["rocket/uuid", "schemars/uuid"] # Re-export Rocket feature flag # https://docs.rs/rocket/latest/rocket/serde/msgpack/struct.MsgPack.html -msgpack = [ "rocket/msgpack" ] +msgpack = ["rocket/msgpack"] # Re-export Rocket feature flag # https://rocket.rs/v0.5/guide/requests/#secret-key -secrets = [ "rocket/secrets" ] +secrets = ["rocket/secrets"] # Re-export Rocket feature flag # https://rocket.rs/v0.5/guide/configuration/#mutual-tls -mtls = [ "rocket/mtls" ] +mtls = ["rocket/mtls"] [package.metadata.docs.rs] all-features = true