From 8242a9cb1dbecf2b3d43cee9f9836c4ee4a3555f Mon Sep 17 00:00:00 2001 From: Andrey Kononov Date: Sat, 6 Aug 2022 18:50:52 +0400 Subject: [PATCH] Cookies support --- README.md | 3 +- okapi-operation-macro/CHANGELOG.md | 6 ++ okapi-operation-macro/Cargo.toml | 2 +- okapi-operation-macro/src/operation/cookie.rs | 61 +++++++++++++++++++ okapi-operation-macro/src/operation/mod.rs | 1 + .../src/operation/parameters.rs | 5 ++ okapi-operation/CHANGELOG.md | 5 +- okapi-operation/Cargo.toml | 2 +- okapi-operation/docs/root.md | 34 ++++++++++- 9 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 okapi-operation-macro/src/operation/cookie.rs diff --git a/README.md b/README.md index 4941865..2d480cd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/okapi-operation-macro/CHANGELOG.md b/okapi-operation-macro/CHANGELOG.md index e149747..f992669 100644 --- a/okapi-operation-macro/CHANGELOG.md +++ b/okapi-operation-macro/CHANGELOG.md @@ -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. diff --git a/okapi-operation-macro/Cargo.toml b/okapi-operation-macro/Cargo.toml index 04026df..04dc6bd 100644 --- a/okapi-operation-macro/Cargo.toml +++ b/okapi-operation-macro/Cargo.toml @@ -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 flowneee3@gmail.com"] edition = "2021" license = "MIT" diff --git a/okapi-operation-macro/src/operation/cookie.rs b/okapi-operation-macro/src/operation/cookie.rs new file mode 100644 index 0000000..0cee962 --- /dev/null +++ b/okapi-operation-macro/src/operation/cookie.rs @@ -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, + #[darling(default)] + required: bool, + #[darling(default)] + deprecated: bool, + #[darling(default)] + explode: Option, + #[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(), + } + }); + } +} diff --git a/okapi-operation-macro/src/operation/mod.rs b/okapi-operation-macro/src/operation/mod.rs index e7e3cbd..b51b37f 100644 --- a/okapi-operation-macro/src/operation/mod.rs +++ b/okapi-operation-macro/src/operation/mod.rs @@ -11,6 +11,7 @@ use crate::{ OPENAPI_FUNCTION_NAME_SUFFIX, }; +mod cookie; mod external_docs; mod header; mod parameters; diff --git a/okapi-operation-macro/src/operation/parameters.rs b/okapi-operation-macro/src/operation/parameters.rs index e4829a3..21aeccb 100644 --- a/okapi-operation-macro/src/operation/parameters.rs +++ b/okapi-operation-macro/src/operation/parameters.rs @@ -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 @@ -49,6 +51,7 @@ pub(super) struct Parameters { header_parameters: Vec
, path_parameters: Vec, query_parameters: Vec, + cookie_parameters: Vec, ref_parameters: Vec, } @@ -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 { diff --git a/okapi-operation/CHANGELOG.md b/okapi-operation/CHANGELOG.md index 044b628..54f6b3c 100644 --- a/okapi-operation/CHANGELOG.md +++ b/okapi-operation/CHANGELOG.md @@ -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. diff --git a/okapi-operation/Cargo.toml b/okapi-operation/Cargo.toml index 9f56e7e..004a87f 100644 --- a/okapi-operation/Cargo.toml +++ b/okapi-operation/Cargo.toml @@ -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 flowneee3@gmail.com"] edition = "2021" license = "MIT" diff --git a/okapi-operation/docs/root.md b/okapi-operation/docs/root.md index 17b4d60..10fa41e 100644 --- a/okapi-operation/docs/root.md +++ b/okapi-operation/docs/root.md @@ -10,6 +10,7 @@ - [Header](#header) - [Query](#query) - [Path](#path) + - [Cookie](#cookie) - [Reference](#reference) + [Multiple parameters](#multiple-parameters) + [Request body](#request-body) @@ -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 @@ -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