Skip to content

Commit

Permalink
Fix IntoParams not allowing object types
Browse files Browse the repository at this point in the history
  • Loading branch information
juhaku committed Apr 27, 2022
1 parent 906d965 commit ec93880
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "utoipa"
description = "Compile time generated OpenAPI documentation for Rust"
version = "1.0.0"
version = "1.0.1"
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# utoipa - Auto generated OpenAPI documentation

[![Utoipa build](https://github.com/juhaku/utoipa/actions/workflows/build.yaml/badge.svg)](https://github.com/juhaku/utoipa/actions/workflows/build.yaml)
[![crates.io](https://img.shields.io/static/v1?label=crates.io&message=1.0.0&color=orange&logo=rust)](https://crates.io/crates/utoipa/1.0.0)
[![docs.rs](https://img.shields.io/static/v1?label=docs.rs&message=utoipa&color=blue&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K)](https://docs.rs/utoipa/1.0.0/utoipa/)
[![crates.io](https://img.shields.io/static/v1?label=crates.io&message=1.0.1&color=orange&logo=rust)](https://crates.io/crates/utoipa/1.0.1)
[![docs.rs](https://img.shields.io/static/v1?label=docs.rs&message=utoipa&color=blue&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K)](https://docs.rs/utoipa/1.0.1/utoipa/)
![rustc](https://img.shields.io/static/v1?label=rustc&message=1.60%2B&color=orange&logo=rust)

Want to have your API documented with OpenAPI? But you dont want to see the
Expand Down Expand Up @@ -50,9 +50,9 @@ and the `ipa` is _api_ reversed. Aaand... `ipa` is also awesome type of beer :be
* **yaml** Enables **serde_yaml** serialization of OpenAPI objects.
* **actix_extras** Enhances [actix-web](https://github.com/actix/actix-web/) intgration with being able to
parse `path` and `path and query parameters` from actix web path attribute macros. See
[docs](https://docs.rs/utoipa/1.0.0/utoipa/attr.path.html#actix_extras-support-for-actix-web) or [examples](./examples) for more details.
[docs](https://docs.rs/utoipa/1.0.1/utoipa/attr.path.html#actix_extras-support-for-actix-web) or [examples](./examples) for more details.
* **rocket_extras** Enhances [rocket](https://github.com/SergioBenitez/Rocket) framework integration with being
able to parse `path`, `path and query parameters` from rocket path attribute macros. See [docs](https://docs.rs/utoipa/1.0.0/utoipa/attr.path.html#rocket_extras-support-for-rocket)
able to parse `path`, `path and query parameters` from rocket path attribute macros. See [docs](https://docs.rs/utoipa/1.0.1/utoipa/attr.path.html#rocket_extras-support-for-rocket)
or [examples](./examples) for more details.
* **debug** Add extra traits such as debug traits to openapi definitions and elsewhere.
* **chrono** Add support for [chrono](https://crates.io/crates/chrono) `DateTime`, `Date` and `Duration`
Expand All @@ -64,25 +64,25 @@ and the `ipa` is _api_ reversed. Aaand... `ipa` is also awesome type of beer :be
[RFC3339](https://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14) as `ISO-8601`.
* **decimal** Add support for [rust_decimal](https://crates.io/crates/rust_decimal) `Decimal` type. **By default**
it is interpreted as `String`. If you wish to change the format you need to override the type.
See the `value_type` in [component derive docs](https://docs.rs/utoipa/1.0.0/utoipa/derive.Component.html).
See the `value_type` in [component derive docs](https://docs.rs/utoipa/1.0.1/utoipa/derive.Component.html).
* **uuid** Add support for [uuid](https://github.com/uuid-rs/uuid). `Uuid` type will be presented as `String` with
format `uuid` in OpenAPI spec.

Utoipa implicitly has partial support for `serde` attributes. See [docs](https://docs.rs/utoipa/1.0.0/utoipa/derive.Component.html#partial-serde-attributes-support) for more details.
Utoipa implicitly has partial support for `serde` attributes. See [docs](https://docs.rs/utoipa/1.0.1/utoipa/derive.Component.html#partial-serde-attributes-support) for more details.

## Install

Add minimal dependency declaration to Cargo.toml.
```
[dependencies]
utoipa = "1.0.0"
utoipa = "1.0.1"
```

To enable more features such as use actix framework extras you could define the
dependency as follows.
```
[dependencies]
utoipa = { version = "1.0.0", features = ["actix_extras"] }
utoipa = { version = "1.0.1", features = ["actix_extras"] }
```

**Note!** To use `utoipa` together with Swagger UI you can use the [utoipa-swagger-ui](https://docs.rs/utoipa-swagger-ui/) crate.
Expand Down Expand Up @@ -228,8 +228,8 @@ This would produce api doc something similar to:

* See how to serve OpenAPI doc via Swagger UI check [utoipa-swagger-ui](https://docs.rs/utoipa-swagger-ui/) crate for more details.
* Browse to [examples](https://github.com/juhaku/utoipa/tree/master/examples) for more comprehensive examples.
* Modify generated OpenAPI at runtime check [Modify](https://docs.rs/utoipa/1.0.0/utoipa/trait.Modify.html) trait for more details.
* More about OpenAPI security in [security documentation](https://docs.rs/utoipa/1.0.0/utoipa/openapi/security/index.html).
* Modify generated OpenAPI at runtime check [Modify](https://docs.rs/utoipa/1.0.1/utoipa/trait.Modify.html) trait for more details.
* More about OpenAPI security in [security documentation](https://docs.rs/utoipa/1.0.1/utoipa/openapi/security/index.html).

# License

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@
//! Add minimal dependency declaration to Cargo.toml.
//! ```text
//! [dependencies]
//! utoipa = "1.0.0"
//! utoipa = "1.0.1"
//! ```
//!
//! To enable more features such as use actix framework extras you could define the
//! dependency as follows.
//! ```text
//! [dependencies]
//! utoipa = { version = "1.0.0", features = ["actix_extras"] }
//! utoipa = { version = "1.0.1", features = ["actix_extras"] }
//! ```
//!
//! **Note!** To use `utoipa` together with Swagger UI you can use the [`utoipa-swagger-ui`][utoipa_swagger] crate.
Expand Down
22 changes: 17 additions & 5 deletions tests/path_derive_actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use utoipa::{
path::{Parameter, ParameterBuilder, ParameterIn},
Array, ComponentFormat, PropertyBuilder,
},
IntoParams, OpenApi,
Component, IntoParams, OpenApi,
};

mod common;
Expand Down Expand Up @@ -488,6 +488,13 @@ fn derive_into_params_with_custom_attributes() {
/// Age filter for user
#[param(style = Form, explode, allow_reserved, example = json!(["10"]))]
age: Option<Vec<String>>,
sort: Sort,
}

#[derive(Deserialize, Component)]
enum Sort {
Asc,
Desc,
}

#[utoipa::path(
Expand All @@ -502,15 +509,13 @@ fn derive_into_params_with_custom_attributes() {
}

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

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
let parameters = common::get_json_path(&doc, "paths./foo/{id}/{name}.get.parameters");

dbg!(&parameters);

common::assert_json_array_len(parameters, 3);
common::assert_json_array_len(parameters, 4);
assert_value! {parameters=>
"[0].in" = r#""path""#, "Parameter in"
"[0].name" = r#""id""#, "Parameter name"
Expand Down Expand Up @@ -547,6 +552,13 @@ fn derive_into_params_with_custom_attributes() {
"[2].explode" = r#"true"#, "Parameter explode"
"[2].schema.type" = r#""array""#, "Parameter schema type"
"[2].schema.items.type" = r#""string""#, "Parameter items schema type"

"[3].in" = r#""query""#, "Parameter in"
"[3].name" = r#""sort""#, "Parameter name"
"[3].description" = r#"null"#, "Parameter description"
"[3].required" = r#"true"#, "Parameter required"
"[3].deprecated" = r#"null"#, "Parameter deprecated"
"[3].schema.$ref" = r###""#/components/schemas/Sort""###, "Parameter schema type"
}
}

Expand Down
10 changes: 6 additions & 4 deletions utoipa-gen/src/schema/into_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ impl ToTokens for ParamType<'_> {
})
}
}
ValueType::Object => abort!(
ty.ident,
"unsupported type, only primitive and String types are supported"
),
ValueType::Object => {
let name = ty.ident.to_string();
tokens.extend(quote! {
utoipa::openapi::Ref::from_component_name(#name)
});
}
},
Some(GenericType::Option)
| Some(GenericType::Cow)
Expand Down

0 comments on commit ec93880

Please sign in to comment.