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

Add the module path to the definition name to avoid name collisions #373

Conversation

TobiasDeBruijn
Copy link
Contributor

This PR intends to fix #357, but uses an alternative approach to #358.

With this PR, the behaviour will be as follows:

mod foo {
    pub mod bar {
        use paperclip::actix::{web, api_v2_operation, Apiv2Schema};
        use serde::Serialize;

        #[derive(Serialize, Apiv2Schema)]
        pub struct Response {
            bar: i32
        }

        #[api_v2_operation]
        pub fn b() -> web::Json<Response> {
            web::Json(Response { bar: 10 })
        }
    }
}

results in the following spec:

  foo_bar_Response:
    type: object
    properties:
      bar:
        type: integer
        format: int32
    required:
      - bar
...
          schema:
            $ref: '#/definitions/foo_bar_Response' 

The advantage of this PR over #358 is that the generated spec is always the same for the same input code.

The name is determined by taking the output of std::module_path!(), removing the first 'component' (i.e. the crate name), then joining with _ as a separator.

This will not break code backwards compatibility, but it will break spec backwards compatibility, i.e. the generated spec is different from how it would be without this PR

Copy link
Collaborator

@tiagolobocastro tiagolobocastro left a comment

Choose a reason for hiding this comment

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

Hey @TobiasDeBruijn, as discussed previously this should be opt-in only - we can probably do that by hiding this behind a feature gate?

macros/src/actix.rs Outdated Show resolved Hide resolved
@tiagolobocastro
Copy link
Collaborator

Looks good, can we add a test for this feature?

@TobiasDeBruijn
Copy link
Contributor Author

I'll give writing a test a shot!

@tiagolobocastro tiagolobocastro merged commit dd53b90 into paperclip-rs:master Jan 28, 2022
@tiagolobocastro
Copy link
Collaborator

Thanks @TobiasDeBruijn !

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.

[Actix] Type definition overriden when struct used with same name multiple times
2 participants