Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Actix plugin] Add random prefix to definition in API spec #358

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
23 changes: 20 additions & 3 deletions macros/src/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure this is the right thing to do here, otherwise we're generating different specs on different builds?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good point. That is indeed the case here. For what I've read and heard, it's not possible to get the fully qualified path in a proc macro.

Do you have any suggestions for another solution?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even a module name is probably not ideal as we want the type name to be named somewhat appropriately, since it's part of an API - what about a way to explicitly rename the openApi type? eg:

#[derive(Deserialize, Serialize, Apiv2Schema)]
#[apiv2(rename = "PetThatSings")]
struct Pet { }

It would also be helpful if we could issue a warning or error if we encounter a duplicate when stitching up the spec.

Copy link
Contributor Author

@TobiasDeBruijn TobiasDeBruijn Nov 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could also definitely be a good option, though I don't know how to implement this myself. Could we maybe compute a 'hash' of sorts and use that as prefix? That way the name is consistent and only changes when the struct itself does.

This means a user isn't required to rename every struct, which gets tedious, but it doesnt leave everything random either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have figured out a way to get the fully qualified path, so that is now too an option

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would the type names look like?
That would be better than random, but that's still something we wouldn't want to do by default. Maybe we could have an attribute enables using that fqp?

#[derive(Deserialize, Serialize, Apiv2Schema)]
#[apiv2(prefix_fqp)]
struct Pet { }
or 
#[derive(Deserialize, Serialize, FQPApiv2Schema)]
struct Pet { }

Copy link
Contributor Author

@TobiasDeBruijn TobiasDeBruijn Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't generated it out yet, but by default we get e.g foo::bar::Request I believe, but we could omit those double colons (it probably does not work well with yaml, so we should I think), or replace them with e.g hyphens.

An attribute that enables it would be the best solution I think, additionally we could stick it behind a feature flag too.

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;

Expand All @@ -731,15 +735,15 @@ 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
// props_gen may override the schema for unnamed structs with 1 element
// 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
}
Expand All @@ -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) {
Expand Down