Skip to content

Commit

Permalink
Release 0.3.0-rc3. Forbid multiple #[openapi] on single function
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowneee committed Aug 7, 2024
1 parent d94da30 commit af90c55
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
5 changes: 4 additions & 1 deletion okapi-operation-macro/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ 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] - 2024-07-21
## [0.2.0] - 2024-08-07
### Added
- Feature `axum` for enable axum-specific functionality;
- Request body detection from function arguments for specific frameworks (i.e. axum);
- `#[body]` attribute as replacement for `#[request_body]` (now considered deprecated);
- Updates `syn` crate to version 2;
- `crate` attribute to support renaming base crate, by default `okapi_operation`;
- `#[openapi]` macro takes care of reimporting necessary types and traits from base crate.

### Removed
- Support for multiple `#[openapi]` appearances above function.


## [0.1.4] - 2024-07-18
Expand Down
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.4"
version = "0.2.0"
authors = ["Andrey Kononov [email protected]"]
edition = "2021"
license = "MIT"
Expand Down
32 changes: 26 additions & 6 deletions okapi-operation-macro/src/operation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cell::RefCell;

use darling::{ast::NestedMeta, FromMeta};
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
Expand All @@ -7,7 +9,7 @@ use self::{external_docs::ExternalDocs, request_body::RequestBody, response::Res
use crate::{
error::Error,
operation::{parameters::Parameters, security::Security},
utils::{attribute_to_args, quote_option, take_attributes},
utils::quote_option,
OPENAPI_FUNCTION_NAME_SUFFIX,
};

Expand All @@ -31,6 +33,10 @@ mod security;
static DEFAULT_OPENAPI_ATTRIBUTE_NAME: &str = "openapi";
static DEFAULT_CRATE_NAME: &str = "okapi_operation";

thread_local! {
pub static MACRO_ATTRIBUTE_NAME: RefCell<String> = RefCell::new(DEFAULT_OPENAPI_ATTRIBUTE_NAME.into());
}

#[derive(Debug, FromMeta)]
struct OperationAttrs {
#[darling(default)]
Expand All @@ -54,6 +60,11 @@ struct OperationAttrs {

#[darling(default = "OperationAttrs::default_crate_name", rename = "crate")]
crate_name: String,
#[darling(
default = "OperationAttrs::default_attribute_name",
rename = "rename_attribute"
)]
attribute_name: String,
}

impl ToTokens for OperationAttrs {
Expand Down Expand Up @@ -105,12 +116,11 @@ pub(crate) fn openapi(
attrs: proc_macro::TokenStream,
mut input: ItemFn,
) -> Result<TokenStream, Error> {
let mut attrs = NestedMeta::parse_meta_list(attrs.into())?;

for attr in take_attributes(&mut input.attrs, DEFAULT_OPENAPI_ATTRIBUTE_NAME) {
attrs.extend(attribute_to_args(&attr)?);
}
let attrs = NestedMeta::parse_meta_list(attrs.into())?;
let mut operation_attrs = OperationAttrs::from_list(&attrs)?;

set_current_attribute_name(operation_attrs.attribute_name.clone());

operation_attrs
.responses
.add_return_type(&input, operation_attrs.responses.ignore_return_type);
Expand Down Expand Up @@ -163,3 +173,13 @@ fn build_openapi_generator_fn(
}
})
}

// TODO: use
#[allow(unused)]
fn current_attribute_name() -> String {
MACRO_ATTRIBUTE_NAME.with_borrow(|x| x.clone())
}

fn set_current_attribute_name(value: String) {
MACRO_ATTRIBUTE_NAME.set(value);
}
2 changes: 2 additions & 0 deletions okapi-operation-macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub(super) fn attribute_to_args(attr: &Attribute) -> Result<Vec<NestedMeta>, Err
}
}

// TODO: use
#[allow(unused)]
pub(super) fn take_attributes(attrs: &mut Vec<Attribute>, attr_name: &str) -> Vec<Attribute> {
let mut non_matched_attrs = vec![];
let mut result = vec![];
Expand Down
2 changes: 1 addition & 1 deletion okapi-operation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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/).

## [Unreleased] - 2024-07-21
## [0.3.0-rc3] - 2024-08-07
### Added
- Feature `axum` as replacement for `axum-integration` (now considered deprecated);
- Request body detection from function arguments for specific frameworks (i.e. axum);
Expand Down
4 changes: 2 additions & 2 deletions 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.3.0-rc2"
version = "0.3.0-rc3"
authors = ["Andrey Kononov [email protected]"]
edition = "2021"
license = "MIT"
Expand All @@ -11,7 +11,7 @@ readme = "../README.md"
repository = "https://github.com/Flowneee/okapi-operation"

[dependencies]
okapi-operation-macro = { path = "../okapi-operation-macro", version = "0.1", optional = true }
okapi-operation-macro = { path = "../okapi-operation-macro", version = "0.2", optional = true }

anyhow = "1"
bytes = "1.4"
Expand Down

0 comments on commit af90c55

Please sign in to comment.