Skip to content

Commit

Permalink
Cookies support
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowneee committed Aug 6, 2022
1 parent 8eed7aa commit 8242a9c
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 7 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ fn main() {

## TODO

* [ ] support cookies
* [ ] support examples
* [ ] support examples on MediaType or Parameter (examples supported on types via `JsonSchema` macro)
* [ ] support inferring schemas of parameters from function definitions
* [ ] support for renaming or changing paths to okapi/schemars/okapi-operations in macro
* [ ] more examples
Expand Down
6 changes: 6 additions & 0 deletions okapi-operation-macro/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
All notable changes to this project will be documented in the changelog of the respective crates.
This project follows the [Semantic Versioning standard](https://semver.org/).


## [0.1.1] - 2022-08-06
### Added
- Cookie parameters.


## [0.1.0] - 2022-07-10
Initial implementation.
2 changes: 1 addition & 1 deletion okapi-operation-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "okapi-operation-macro"
description = "Macro implementation for okapi-operation"
version = "0.1.0"
version = "0.1.1"
authors = ["Andrey Kononov [email protected]"]
edition = "2021"
license = "MIT"
Expand Down
61 changes: 61 additions & 0 deletions okapi-operation-macro/src/operation/cookie.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use darling::FromMeta;
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::Path;

use crate::{operation::parameters::ParameterStyle, utils::quote_option};

pub(super) static COOKIE_ATTRIBUTE_NAME: &str = "cookie";

/// Cookie parameter.
#[derive(Debug, FromMeta)]
pub(super) struct Cookie {
name: String,
#[darling(default)]
description: Option<String>,
#[darling(default)]
required: bool,
#[darling(default)]
deprecated: bool,
#[darling(default)]
explode: Option<bool>,
#[darling(default)]
allow_empty_value: bool,
schema: Path,
// TODO: support content as well
}

impl ToTokens for Cookie {
fn to_tokens(&self, tokens: &mut TokenStream) {
let name = &self.name;
let description = quote_option(&self.description);
let required = &self.required;
let deprecated = &self.deprecated;
let style = ParameterStyle::Form;
let explode = quote_option(&self.explode);
let allow_empty_values = &self.allow_empty_value;
let allow_reserved = false;
let ty = &self.schema;
tokens.extend(quote! {
okapi::openapi3::Parameter {
name: #name.into(),
location: "Cookie".into(),
description: #description,
required: #required,
deprecated: #deprecated,
allow_empty_value: #allow_empty_values,
value: {
okapi::openapi3::ParameterValue::Schema {
style: #style,
explode: #explode,
allow_reserved: #allow_reserved,
schema: components.schema_for::<#ty>(),
example: Default::default(),
examples: Default::default(),
}
},
extensions: Default::default(),
}
});
}
}
1 change: 1 addition & 0 deletions okapi-operation-macro/src/operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
OPENAPI_FUNCTION_NAME_SUFFIX,
};

mod cookie;
mod external_docs;
mod header;
mod parameters;
Expand Down
5 changes: 5 additions & 0 deletions okapi-operation-macro/src/operation/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::{
utils::{meta_to_meta_list, nested_meta_to_meta},
};

use super::cookie::{Cookie, COOKIE_ATTRIBUTE_NAME};

// TODO: support cookie parameters
// TODO: support parameters from function signature

Expand Down Expand Up @@ -49,6 +51,7 @@ pub(super) struct Parameters {
header_parameters: Vec<Header>,
path_parameters: Vec<Path>,
query_parameters: Vec<Query>,
cookie_parameters: Vec<Cookie>,
ref_parameters: Vec<Reference>,
}

Expand All @@ -68,6 +71,8 @@ impl FromMeta for Parameters {
this.path_parameters.push(Path::from_meta(meta)?);
} else if meta_ident == QUERY_ATTRIBUTE_NAME {
this.query_parameters.push(Query::from_meta(meta)?);
} else if meta_ident == COOKIE_ATTRIBUTE_NAME {
this.cookie_parameters.push(Cookie::from_meta(meta)?);
} else if meta_ident == REFERENCE_ATTRIBUTE_NAME {
this.ref_parameters.push(Reference::from_meta(meta)?);
} else {
Expand Down
5 changes: 4 additions & 1 deletion okapi-operation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ All notable changes to this project will be documented in the changelog of the r
This project follows the [Semantic Versioning standard](https://semver.org/).


## [Unreleased]
## [0.1.2] - 2022-08-06
### Added
- Cookie parameters.

### Fixed
- Macro `openapi_handler` now correctly handle paths.

Expand Down
2 changes: 1 addition & 1 deletion okapi-operation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "okapi-operation"
description = "Procedural macro for generating OpenAPI operation specification (using okapi)"
version = "0.1.1"
version = "0.1.2"
authors = ["Andrey Kononov [email protected]"]
edition = "2021"
license = "MIT"
Expand Down
34 changes: 32 additions & 2 deletions okapi-operation/docs/root.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Header](#header)
- [Query](#query)
- [Path](#path)
- [Cookie](#cookie)
- [Reference](#reference)
+ [Multiple parameters](#multiple-parameters)
+ [Request body](#request-body)
Expand Down Expand Up @@ -248,6 +249,36 @@ Unlike header and query parameters, all path parameters is mandatory.
async fn handler() {}
```

#### Cookie

`cookie` have following attributes:

* name (string, mandatory);
* description (string, optional);
* required (bool, optional);
* deprecated (bool, optional);
* explode (bool, optional) - specifies whether arrays and objects should generate separate parameters for each array item or object property;
* allow_empty_value (bool, optional) - allow empty value for this parameter;
* schema (path, mandatory) - path to type of parameter.

```rust,compile
# use okapi_operation::*;
#[openapi(
parameters(
cookie(
name = "session_id",
description = "Session ID",
required = false,
deprecated = false,
explode = true,
allow_empty_value = false,
schema = "std::string::String",
)
)
)]
async fn handler() {}
```

#### Reference

```rust,compile
Expand Down Expand Up @@ -600,8 +631,7 @@ assert!(generate_openapi_specification().is_ok());

## TODO

* [ ] support cookies
* [ ] support examples
* [ ] support examples on MediaType or Parameter (examples supported on types via `JsonSchema` macro)
* [ ] support inferring schemas of parameters from function definitions
* [ ] support for renaming or changing paths to okapi/schemars/okapi-operations in macro
* [ ] more examples
Expand Down

0 comments on commit 8242a9c

Please sign in to comment.