From cf6a040df96631f9f50bcc93ffa55b909c7efd9a Mon Sep 17 00:00:00 2001 From: Tobias de Bruijn Date: Thu, 11 Nov 2021 17:17:21 +0100 Subject: [PATCH 1/2] Modified Apiv2Schema proc macro to add a random prefix to definitions. Solves #357 --- macros/Cargo.toml | 1 + macros/src/actix.rs | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 61d3029e5..838f3d09b 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -22,6 +22,7 @@ heck = { version = "0.3", optional = true } http = { version = "0.2", optional = true } lazy_static = { version = "1.4", optional = true } strum = { version = "0.22", optional = true } +rand = "0.8" strum_macros = { version = "0.22", optional = true } [features] diff --git a/macros/src/actix.rs b/macros/src/actix.rs index 6543c8fbd..6f30fc624 100644 --- a/macros/src/actix.rs +++ b/macros/src/actix.rs @@ -15,6 +15,7 @@ use syn::{ Generics, Ident, ItemFn, Lit, Meta, MetaList, MetaNameValue, NestedMeta, Path, PathArguments, ReturnType, Token, TraitBound, Type, TypeTraitObject, }; +use rand::Rng; use proc_macro2::TokenStream as TokenStream2; use std::collections::HashMap; @@ -719,10 +720,13 @@ pub fn emit_v2_definition(input: TokenStream) -> TokenStream { }; let schema_name = name.to_string(); + let random_prefix = generate_random_uppercase(6); + let prefixed_schema_name = format!("{}{}", random_prefix, schema_name); + let props_gen_empty = props_gen.is_empty(); let gen = quote! { impl #impl_generics paperclip::v2::schema::Apiv2Schema for #name #ty_generics #where_clause { - const NAME: Option<&'static str> = Some(#schema_name); + const NAME: Option<&'static str> = Some(#prefixed_schema_name); const DESCRIPTION: &'static str = #docs; @@ -731,7 +735,7 @@ pub fn emit_v2_definition(input: TokenStream) -> TokenStream { use paperclip::v2::schema::TypedData; let mut schema = DefaultSchemaRaw { - name: Some(#schema_name.into()), // Add name for later use. + name: Some(#prefixed_schema_name.into()), // Add name for later use. .. Default::default() }; #props_gen @@ -739,7 +743,7 @@ pub fn emit_v2_definition(input: TokenStream) -> TokenStream { // as it replaces the struct type with inner type. // make sure we set the name properly if props_gen is not empty if !#props_gen_empty { - schema.name = Some(#schema_name.into()); + schema.name = Some(#prefixed_schema_name.into()); } schema } @@ -751,6 +755,19 @@ pub fn emit_v2_definition(input: TokenStream) -> TokenStream { gen.into() } +/// Generate a random String of uppercase characters (A-Z) with the provided *len* +fn generate_random_uppercase(len: usize) -> String { + const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + let mut rng = rand::thread_rng(); + let ident: String = (0..len) + .map(|_| { + let idx = rng.gen_range(0..CHARSET.len()); + CHARSET[idx] as char + }) + .collect(); + ident +} + /// Actual parser and emitter for `Apiv2Security` derive macro. pub fn emit_v2_security(input: TokenStream) -> TokenStream { let item_ast = match crate::expect_struct_or_enum(input) { From 78d823527baa74ae95627d0f4a3c2c16eb181497 Mon Sep 17 00:00:00 2001 From: Tobias de Bruijn Date: Thu, 11 Nov 2021 17:21:07 +0100 Subject: [PATCH 2/2] Cargo FMT --- macros/src/actix.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/macros/src/actix.rs b/macros/src/actix.rs index 6f30fc624..be69e2592 100644 --- a/macros/src/actix.rs +++ b/macros/src/actix.rs @@ -6,6 +6,7 @@ use http::StatusCode; use lazy_static::lazy_static; use proc_macro::TokenStream; use quote::{quote, ToTokens}; +use rand::Rng; use strum_macros::EnumString; use syn::{ parse_macro_input, @@ -15,7 +16,6 @@ use syn::{ Generics, Ident, ItemFn, Lit, Meta, MetaList, MetaNameValue, NestedMeta, Path, PathArguments, ReturnType, Token, TraitBound, Type, TypeTraitObject, }; -use rand::Rng; use proc_macro2::TokenStream as TokenStream2; use std::collections::HashMap;