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

Conversation

TobiasDeBruijn
Copy link
Contributor

This PR adds a 6 character (A-Z) prefix to every generated definition (the Apiv2Schema proc macro). The reason for this PR is described in #357, this PR solves #357 too.

Feedback is highly appreciated!

@@ -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.

@tiagolobocastro
Copy link
Collaborator

Replaced by #373

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants