utoipauto icon indicating copy to clipboard operation
utoipauto copied to clipboard

Importing structs through their public re-export

Open RemiBardon opened this issue 1 year ago • 5 comments

Hello 👋🏻

I'm trying to use this crate to automatically list my routes and models in the the utoipa macro. I tried to modularize my API into nested modules, each containing a models and a routes private submodule, with their contents re-exported publicly by mod.rs (pub use models::*; pub use routes::*;). However, because the structures are defined in a private module, I get the following error:

error[E0603]: module `routes` is private
  --> src/v1/routes.rs:12:10
   |
12 | #[derive(OpenApi)]
   |          ^^^^^^^ private module
   |
note: the module `routes` is defined here
  --> src/v1/members/mod.rs:7:1
   |
7  | mod routes;
   | ^^^^^^^^^^^
help: consider importing this struct through its public re-export instead
   |
12 | #[derive(crate::v1::members::__path_get_members)]
   |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0603]: module `models` is private
  --> src/v1/routes.rs:11:1
   |
11 | #[utoipauto]
   | ^^^^^^^^^^^^ private module
   |
note: the module `models` is defined here
  --> src/v1/members/mod.rs:6:1
   |
6  | mod models;
   | ^^^^^^^^^^^
help: consider importing this struct through its public re-export instead
   |
11 | crate::v1::members::Member
   |

warning: unused import: `super::members`
 --> src/v1/routes.rs:6:5
  |
6 | use super::members;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0603`.

My src folder has the following structure

src
├── helpers
│   └── mod.rs
├── main.rs
└── v1
    ├── members
    │   ├── mod.rs
    │   ├── models.rs
    │   └── routes.rs
    ├── mod.rs
    ├── routes.rs # Here is the `utoipa` route
    └── server
        ├── features
        │   ├── compliance_tests
        │   │   ├── mod.rs
        │   │   ├── models.rs
        │   │   └── routes.rs
        │   ├── config
        │   │   ├── mod.rs
        │   │   ├── models.rs
        │   │   └── routes.rs
        │   └── mod.rs
        └── mod.rs

Do you know a way to work around this issue, other than putting routes and models in the same mod.rs file (which I'd like to avoid) or having the modules public (which generates a bad-looking OpenAPI spec)?

Edit: As a workaround, I switched the submodules to be public and I added a tag field in all utoipa::path macros so the generated tags are not ugly (i.e. members instead of crate::v1::members::routes).

RemiBardon avatar Dec 19 '23 16:12 RemiBardon