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

Paths are always assigned to a tag with their module path #978

Closed
theelderbeever opened this issue Jul 26, 2024 · 6 comments · Fixed by #1002
Closed

Paths are always assigned to a tag with their module path #978

theelderbeever opened this issue Jul 26, 2024 · 6 comments · Fixed by #1002
Labels
bug Something isn't working

Comments

@theelderbeever
Copy link

I am trying to use nesting of OpenApi doc structs because you can't register a path that isn't defined in the same module as the utoipa::path macro. However, with a nested api doc I keep getting a an extra tag header with the module location for each path. How can I get rid of that?

image

Additionally, why isn't it possible to register a path in a separate module?

#[derive(OpenApi)]
#[openapi(
    paths(some::other::mod::get_path), // This complains about __path_get_path not being accessible
    components(schemas(crate::api::v1::usage::schemas::Params))
)]
pub struct ApiDoc;
@juhaku
Copy link
Owner

juhaku commented Aug 1, 2024

I am trying to use nesting of OpenApi doc structs because you can't register a path that isn't defined in the same module as the utoipa::path macro. However, with a nested api doc I keep getting a an extra tag header with the module location for each path. How can I get rid of that?

Not sure do I understand the problem, but there is a way to provide custom tag for the operation with the following.

 #[utoipa::path(
   tag = ...
 )]

Then the operations that has the same tag will be grouped together. Depending on which version of utoipa you are currently using you either have the tag = ... and tags = ... attribute as seen in the docs https://docs.rs/utoipa/5.0.0-alpha.1/utoipa/attr.path.html#path-attributes. The utoipa 4.x.x does not have the tags = ... attribute. Tags attribute should allow adding additional tags, while the tag should override the default tag which is the path to the operation function by default.

Additionally, why isn't it possible to register a path in a separate module?

Actually it is but the path must be pub as the #[utoipa::path] attribute macro generates a struct that implements Path trait but the visibility will be the same as the function is. So with this the some::other::mod::get_path should be public function.

#[derive(OpenApi)]
#[openapi(
    paths(some::other::mod::get_path), // This complains about __path_get_path not being accessible
    components(schemas(crate::api::v1::usage::schemas::Params))
)]
pub struct ApiDoc;

@theelderbeever
Copy link
Author

@juhaku I am pointing at your main/5.x.x.

The tag field does not override the default tag. It just make an additional tag. Thats the issue I was trying to get across poorly.

 #[utoipa::path(
   tag = ...
 )]

The following does not appear to be true. Everything I have is pub, the path handler and all the mods above it.

the #[utoipa::path] attribute macro generates a struct that implements Path trait but the visibility will be the same as the function is.

@theelderbeever
Copy link
Author

In the next few days I can link an example using your todo app to demonstrate what I am talking about.

@daniel-mader
Copy link

Is there any news on this? I'm having the same issue (default module path tags I cannot get rid of, no matter if I'm using tag or tags).

@juhaku
Copy link
Owner

juhaku commented Aug 24, 2024

@theelderbeever Okay, thanks for pointing out. I assume there is a bug in the tags processing it should be investigated that what's the deal in the code.

@juhaku juhaku added the bug Something isn't working label Aug 24, 2024
@juhaku juhaku moved this to Todo in utoipa kanban Aug 24, 2024
@juhaku juhaku moved this from Todo to In Progress in utoipa kanban Aug 24, 2024
juhaku added a commit that referenced this issue Aug 24, 2024
The default tag for paths is resolved from the module path of the handler
or `OpenApi` if the type in question is a nested `OpenApi` document. This
was supposed to be only the case when no additional tags are provided for the
paths. However it was resolved in all cases and there was no way to
"not to use" module path as a tag. This commit fixes this and the
module path will be only used if there are no other tags defined.

This commit also changes the old behavior where _`crate`_ was used as
a tag in case there was no module path available. From now on if there is
no module path there will be no default tag added to any of the paths.
This will result the paths to be rendered under _`default`_ tag in a
Swagger UI.

Fixes #978
@juhaku
Copy link
Owner

juhaku commented Aug 24, 2024

The following does not appear to be true. Everything I have is pub, the path handler and all the mods above it.

the #[utoipa::path] attribute macro generates a struct that implements Path trait but the visibility will be the same as the function is.

Yes it seems that the doc has not been updated since the behavior has been changed probably long ago. However it will generate allways pub struct regardless whether the handler fn itself is pub or not.

The following code is possible to nest paths from sub module paths without exposing the module path to the derive macro.

mod test_path {
    #[allow(dead_code)]
    #[utoipa::path(get, path = "/test")]
    #[allow(unused)]
    pub fn test_path() -> &'static str {
        ""
    }
}

mod test_nest {
    #[derive(utoipa::OpenApi)]
    #[openapi(paths(test_path_nested))]
    pub struct NestApi;

    #[allow(dead_code)]
    #[utoipa::path(get, path = "/test")]
    #[allow(unused)]
    fn test_path_nested() -> &'static str {
        ""
    }
}

use test_nest::NestApi;
use test_path::__path_test_path;
#[derive(utoipa::OpenApi)]
#[openapi(
    paths(test_path),
    nest(
        (path = "/api/nest", api = NestApi)
    )
)]
struct ApiDoc;

juhaku added a commit that referenced this issue Aug 24, 2024
The default tag for paths is resolved from the module path of the handler
or `OpenApi` if the type in question is a nested `OpenApi` document. This
was supposed to be only the case when no additional tags are provided for the
paths. However it was resolved in all cases and there was no way to
"not to use" module path as a tag. This commit fixes this and the
module path will be only used if there are no other tags defined.

This commit also changes the old behavior where _`crate`_ was used as
a tag in case there was no module path available. From now on if there is
no module path there will be no default tag added to any of the paths.
This will result the paths to be rendered under _`default`_ tag in a
Swagger UI.

Fixes #978
juhaku added a commit that referenced this issue Aug 24, 2024
The default tag for paths is resolved from the module path of the handler
or `OpenApi` if the type in question is a nested `OpenApi` document. This
was supposed to be only the case when no additional tags are provided for the
paths. However it was resolved in all cases and there was no way to
"not to use" module path as a tag. This commit fixes this and the
module path will be only used if there are no other tags defined.

This commit also changes the old behavior where _`crate`_ was used as
a tag in case there was no module path available. From now on if there is
no module path there will be no default tag added to any of the paths.
This will result the paths to be rendered under _`default`_ tag in a
Swagger UI.

Fixes #978
@github-project-automation github-project-automation bot moved this from In Progress to Done in utoipa kanban Aug 24, 2024
@juhaku juhaku moved this from Done to Released in utoipa kanban Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Released
Development

Successfully merging a pull request may close this issue.

3 participants