Skip to content

Commit

Permalink
Fixed raw identifiers in parameters
Browse files Browse the repository at this point in the history
Closes #117
  • Loading branch information
ralpha committed May 29, 2023
1 parent 1baec94 commit 69f3dc4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ members = [
"examples/streams",
"examples/dyn_templates",
"examples/openapi_attributes",
"examples/raw_identifiers",
]
4 changes: 4 additions & 0 deletions examples/raw_identifiers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
/.idea
11 changes: 11 additions & 0 deletions examples/raw_identifiers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "raw_identifiers"
version = "0.1.0"
authors = ["Alex Payne <apayne@esri.com>"]
edition = "2018"

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

[dependencies]
rocket = { version = "=0.5.0-rc.3", default-features = false, features = ["json"] }
rocket_okapi = { path = "../../rocket-okapi", features = ["rapidoc", "swagger"] }
39 changes: 39 additions & 0 deletions examples/raw_identifiers/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use rocket_okapi::settings::UrlObject;
use rocket_okapi::{openapi, openapi_get_routes, rapidoc::*, swagger_ui::*};

#[macro_use]
extern crate rocket;

#[openapi]
#[get("/hello/<type>")]
fn hello(r#type: String) -> String {
r#type
}

#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", openapi_get_routes![hello])
.mount(
"/swagger-ui/",
make_swagger_ui(&SwaggerUIConfig {
url: "../openapi.json".to_owned(),
..Default::default()
}),
)
.mount(
"/rapidoc/",
make_rapidoc(&RapiDocConfig {
general: GeneralConfig {
spec_urls: vec![UrlObject::new("General", "../openapi.json")],
..Default::default()
},
hide_show: HideShowConfig {
allow_spec_url_load: false,
allow_spec_file_load: false,
..Default::default()
},
..Default::default()
}),
)
}
5 changes: 4 additions & 1 deletion rocket-okapi-codegen/src/openapi_attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use quote::quote;
use quote::ToTokens;
use rocket_http::Method;
use std::collections::BTreeMap as Map;
use syn::ext::IdentExt;
use syn::{
parse_macro_input, AttributeArgs, FnArg, GenericArgument, Ident, ItemFn, PathArguments,
PathSegment, ReturnType, Type, TypeTuple,
Expand Down Expand Up @@ -416,7 +417,9 @@ fn get_arg_types(args: impl Iterator<Item = FnArg>) -> Map<String, Type> {
if let syn::FnArg::Typed(arg) = arg {
if let syn::Pat::Ident(ident) = *arg.pat {
// Use only identifier name as key, so lookups succeed even if argument is mutable
let name = ident.ident.into_token_stream().to_string();
// The `unraw()` strips the raw marker r#, if any, from the beginning of an ident.
// This fixed https://github.com/GREsau/okapi/issues/117
let name = ident.ident.unraw().into_token_stream().to_string();
let ty = *arg.ty;
result.insert(name, ty);
}
Expand Down
2 changes: 1 addition & 1 deletion rocket-okapi-codegen/src/openapi_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn create_add_operations(paths: Punctuated<Path, Comma>) -> TokenStream2 {
}

fn fn_name_for_add_operation(mut fn_path: Path) -> Path {
let mut last_seg = fn_path.segments.last_mut().expect("syn::Path has segments");
let last_seg = fn_path.segments.last_mut().expect("syn::Path has segments");
last_seg.ident = get_add_operation_fn_name(&last_seg.ident);
fn_path
}
Expand Down

0 comments on commit 69f3dc4

Please sign in to comment.