Skip to content

Commit

Permalink
Rename handlers to paths
Browse files Browse the repository at this point in the history
Rename `handlers` attribute of `OpenApi` derive macro to `paths` to make it
consistent with OpenAPI spec.

```rust
    paths(...)
)]
struct ApiDoc;
```
  • Loading branch information
juhaku committed Aug 8, 2022
1 parent 6e3ea85 commit f8a691c
Show file tree
Hide file tree
Showing 24 changed files with 94 additions and 90 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Tie the `Schema` and the endpoint above to the OpenApi schema with following `Op
use utoipa::OpenApi;

#[derive(OpenApi)]
#[openapi(handlers(pet_api::get_pet_by_id), components(schemas(Pet)))]
#[openapi(paths(pet_api::get_pet_by_id), components(schemas(Pet)))]
struct ApiDoc;

println!("{}", ApiDoc::openapi().to_pretty_json().unwrap());
Expand Down
4 changes: 2 additions & 2 deletions examples/actix-web-multiple-api-docs-with-scopes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ async fn main() -> Result<(), impl Error> {
env_logger::init();

#[derive(OpenApi)]
#[openapi(handlers(api1::hello1))]
#[openapi(paths(api1::hello1))]
struct ApiDoc1;

#[derive(OpenApi)]
#[openapi(handlers(api2::hello2))]
#[openapi(paths(api2::hello2))]
struct ApiDoc2;

HttpServer::new(move || {
Expand Down
2 changes: 1 addition & 1 deletion examples/rocket-0_4-hello/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use utoipa_swagger_ui::Config;

fn main() {
#[derive(OpenApi)]
#[openapi(handlers(hello))]
#[openapi(paths(hello))]
struct ApiDoc;

rocket::ignite()
Expand Down
2 changes: 1 addition & 1 deletion examples/rocket-todo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn rocket() -> Rocket<Build> {

#[derive(OpenApi)]
#[openapi(
handlers(
paths(
todo::get_tasks,
todo::create_todo,
todo::mark_done,
Expand Down
2 changes: 1 addition & 1 deletion examples/todo-actix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn main() -> Result<(), impl Error> {

#[derive(OpenApi)]
#[openapi(
handlers(
paths(
todo::get_todos,
todo::create_todo,
todo::delete_todo,
Expand Down
2 changes: 1 addition & 1 deletion examples/todo-axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::todo::Store;
async fn main() -> Result<(), Error> {
#[derive(OpenApi)]
#[openapi(
handlers(
paths(
todo::list_todos,
todo::search_todos,
todo::create_todo,
Expand Down
2 changes: 1 addition & 1 deletion examples/todo-tide/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {

#[derive(OpenApi)]
#[openapi(
handlers(
paths(
todo::list_todos,
todo::create_todo,
todo::delete_todo,
Expand Down
2 changes: 1 addition & 1 deletion examples/todo-warp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() {

#[derive(OpenApi)]
#[openapi(
handlers(todo::list_todos, todo::create_todo, todo::delete_todo),
paths(todo::list_todos, todo::create_todo, todo::delete_todo),
components(
schemas(todo::Todo)
),
Expand Down
8 changes: 5 additions & 3 deletions examples/warp-multiple-api-docs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ async fn main() {
let config = Arc::new(Config::new(["/api-doc1.json", "/api-doc2.json"]));

#[derive(OpenApi)]
#[openapi(handlers(api1::hello1))]
#[openapi(paths(api1::hello1))]
struct ApiDoc1;

#[derive(OpenApi)]
#[openapi(handlers(api2::hello2))]
#[openapi(paths(api2::hello2))]
struct ApiDoc2;

let api_doc1 = warp::path("api-doc1.json")
Expand Down Expand Up @@ -59,7 +59,9 @@ async fn serve_swagger(
config: Arc<Config<'static>>,
) -> Result<Box<dyn Reply + 'static>, Rejection> {
if full_path.as_str() == "/swagger-ui" {
return Ok(Box::new(warp::redirect::found(Uri::from_static("/swagger-ui/"))));
return Ok(Box::new(warp::redirect::found(Uri::from_static(
"/swagger-ui/",
))));
}

let path = tail.as_str();
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
//! # }
//! # use utoipa::OpenApi;
//! #[derive(OpenApi)]
//! #[openapi(handlers(pet_api::get_pet_by_id), components(schemas(Pet)))]
//! #[openapi(paths(pet_api::get_pet_by_id), components(schemas(Pet)))]
//! struct ApiDoc;
//!
//! println!("{}", ApiDoc::openapi().to_pretty_json().unwrap());
Expand Down Expand Up @@ -232,7 +232,7 @@ pub use utoipa_gen::*;
/// ```rust
/// use utoipa::OpenApi;
/// #[derive(OpenApi)]
/// #[openapi(handlers())]
/// #[openapi()]
/// struct OpenApiDoc;
/// ```
///
Expand Down
2 changes: 1 addition & 1 deletion src/openapi/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ builder! {
/// List of tags used for groupping operations.
///
/// When used with derive [`#[utoipa::path(...)]`][derive_path] attribute macro the default
/// value used will be resolved from handler path provided in `#[openapi(handlers(...))]` with
/// value used will be resolved from handler path provided in `#[openapi(paths(...))]` with
/// [`#[derive(OpenApi)]`][derive_openapi] macro. If path resolves to `None` value `crate` will
/// be used by default.
///
Expand Down
14 changes: 7 additions & 7 deletions tests/path_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro_rules! test_api_fn_doc {
( $handler:path, operation: $operation:expr, path: $path:literal ) => {{
use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handlers($handler))]
#[openapi(paths($handler))]
struct ApiDoc;

let doc = &serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -73,7 +73,7 @@ macro_rules! test_path_operation {
paste!{
use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handlers(
#[openapi(paths(
[<mod_ $name>]::test_operation
))]
struct ApiDoc;
Expand Down Expand Up @@ -425,7 +425,7 @@ fn derive_path_params_map() {

use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handlers(use_maps))]
#[openapi(paths(use_maps))]
struct ApiDoc;

let operation: Value = test_api_fn_doc! {
Expand Down Expand Up @@ -521,7 +521,7 @@ fn derive_path_params_intoparams() {

use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handlers(list))]
#[openapi(paths(list))]
struct ApiDoc;

let operation: Value = test_api_fn_doc! {
Expand Down Expand Up @@ -671,7 +671,7 @@ fn derive_path_params_into_params_with_value_type() {
fn get_foo(query: Filter) {}

#[derive(OpenApi, Default)]
#[openapi(handlers(get_foo))]
#[openapi(paths(get_foo))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -786,7 +786,7 @@ fn derive_path_params_into_params_with_raw_identifier() {
fn get_foo(query: Filter) {}

#[derive(OpenApi, Default)]
#[openapi(handlers(get_foo))]
#[openapi(paths(get_foo))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -829,7 +829,7 @@ fn derive_path_with_into_responses() {
fn get_foo() {}

#[derive(OpenApi, Default)]
#[openapi(handlers(get_foo))]
#[openapi(paths(get_foo))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down
28 changes: 14 additions & 14 deletions tests/path_derive_actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod mod_derive_path_actix {
#[test]
fn derive_path_one_value_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers(mod_derive_path_actix::get_foo_by_id))]
#[openapi(paths(mod_derive_path_actix::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -82,7 +82,7 @@ mod mod_derive_path_unnamed_regex_actix {
#[test]
fn derive_path_with_unnamed_regex_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers(mod_derive_path_unnamed_regex_actix::get_foo_by_id))]
#[openapi(paths(mod_derive_path_unnamed_regex_actix::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -125,7 +125,7 @@ mod mod_derive_path_named_regex_actix {
#[test]
fn derive_path_with_named_regex_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers(mod_derive_path_named_regex_actix::get_foo_by_id))]
#[openapi(paths(mod_derive_path_named_regex_actix::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -164,7 +164,7 @@ fn derive_path_with_multiple_args() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(mod_derive_path_multiple_args::get_foo_by_id))]
#[openapi(paths(mod_derive_path_multiple_args::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -212,7 +212,7 @@ fn derive_complex_actix_web_path() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(mod_derive_complex_actix_path::get_foo_by_id))]
#[openapi(paths(mod_derive_complex_actix_path::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -254,7 +254,7 @@ fn derive_path_with_multiple_args_with_descriptions() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(mod_derive_path_multiple_args::get_foo_by_id))]
#[openapi(paths(mod_derive_path_multiple_args::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -300,7 +300,7 @@ fn derive_path_with_context_path() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(get_foo))]
#[openapi(paths(get_foo))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -382,7 +382,7 @@ fn path_with_struct_variables_with_into_params() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(get_foo))]
#[openapi(paths(get_foo))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -450,7 +450,7 @@ fn derive_path_with_struct_variables_with_into_params() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(get_foo))]
#[openapi(paths(get_foo))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -524,7 +524,7 @@ fn derive_path_with_multiple_instances_same_path_params() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(get_foo, delete_foo))]
#[openapi(paths(get_foo, delete_foo))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -567,7 +567,7 @@ fn derive_path_with_multiple_into_params_names() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(get_foo))]
#[openapi(paths(get_foo))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -640,7 +640,7 @@ fn derive_into_params_with_custom_attributes() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(get_foo), components(schemas(Sort)))]
#[openapi(paths(get_foo), components(schemas(Sort)))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -724,7 +724,7 @@ fn derive_into_params_in_another_module() {
}

#[derive(OpenApi, Default)]
#[openapi(handlers(foo_todos))]
#[openapi(paths(foo_todos))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -761,7 +761,7 @@ macro_rules! test_derive_path_operations {
#[test]
fn $name() {
#[derive(OpenApi, Default)]
#[openapi(handlers($mod::test_operation))]
#[openapi(paths($mod::test_operation))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions tests/path_derive_axum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn derive_path_params_into_params_axum() {
async fn get_person(person: Path<Person>, query: Query<custom::Filter>) {}

#[derive(OpenApi)]
#[openapi(handlers(get_person))]
#[openapi(paths(get_person))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -117,7 +117,7 @@ fn get_todo_with_extension() {
fn list_todos(Extension(store): Extension<Arc<Store>>) {}

#[derive(OpenApi)]
#[openapi(handlers(list_todos))]
#[openapi(paths(list_todos))]
struct ApiDoc;

serde_json::to_value(ApiDoc::openapi())
Expand All @@ -144,7 +144,7 @@ fn derive_path_params_into_params_unnamed() {
async fn get_person(person: Path<IdAndName>) {}

#[derive(OpenApi)]
#[openapi(handlers(get_person))]
#[openapi(paths(get_person))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down
Loading

0 comments on commit f8a691c

Please sign in to comment.