Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Flowneee/okapi-operation
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: okapi-operation-0.1.2
Choose a base ref
...
head repository: Flowneee/okapi-operation
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

Commits on Feb 15, 2023

  1. Copy the full SHA
    8831e03 View commit details
  2. fix clippy warning

    Flowneee committed Feb 15, 2023
    Copy the full SHA
    fe8d919 View commit details

Commits on Feb 18, 2023

  1. Upgrade to axum 0.6

    Flowneee committed Feb 18, 2023
    Copy the full SHA
    e6b1714 View commit details

Commits on Mar 7, 2023

  1. Bump darling dependency (#2)

    * `darling` version bump: 0.13 => 0.14.3.
    
    * CHANGELOG.
    
    * Also bumping `axum` in dev-dependencies.
    
    * Revert "Also bumping `axum` in dev-dependencies."
    
    This reverts commit c7f447d.
    
    ---------
    
    Co-authored-by: Artyom Sakharilenko <a.sakharilenko@tinkoff.ru>
    kryvashek and Artyom Sakharilenko authored Mar 7, 2023
    Copy the full SHA
    e16e9d6 View commit details

Commits on Apr 29, 2023

  1. Copy the full SHA
    207ed66 View commit details
  2. Bump axum in macro crate

    Flowneee committed Apr 29, 2023
    Copy the full SHA
    d018e79 View commit details

Commits on May 9, 2023

  1. Add yaml description for spec (#7)

    * Add yaml description for spec
    
    * Review fixes
    
    * Few minor changes
    
    ---------
    
    Co-authored-by: Grigoriy Dmitriev <grigoriy.dmitriev@github.com>
    Co-authored-by: Andrey Kononov <flowneee3@gmail.com>
    3 people authored May 9, 2023
    Copy the full SHA
    af38620 View commit details
  2. Update changelog + version

    Flowneee committed May 9, 2023
    Copy the full SHA
    1f4398d View commit details

Commits on Sep 27, 2023

  1. Copy the full SHA
    941ff39 View commit details

Commits on Oct 23, 2023

  1. Fix accept header (#13)

    lgtm
    slinkydeveloper authored Oct 23, 2023
    Copy the full SHA
    db51ca2 View commit details

Commits on Oct 24, 2023

  1. Add CHANGELOG

    slinkydeveloper committed Oct 24, 2023
    Copy the full SHA
    baf739d View commit details

Commits on Nov 1, 2023

  1. Fix CHANGELOG

    slinkydeveloper committed Nov 1, 2023
    Copy the full SHA
    17f64ff View commit details

Commits on Dec 3, 2023

  1. Merge pull request #12 from slinkydeveloper/issues/11

    Merge routes in Router::Route
    Flowneee authored Dec 3, 2023
    Copy the full SHA
    6cffa9e View commit details
  2. Copy the full SHA
    e9c2565 View commit details
  3. Update to axum 0.7

    Flowneee committed Dec 3, 2023
    Copy the full SHA
    13db160 View commit details
  4. Refactor OpenApiBuilder

    Flowneee committed Dec 3, 2023
    Copy the full SHA
    b840e91 View commit details

Commits on Apr 23, 2024

  1. Copy the full SHA
    8eecea2 View commit details
  2. Copy the full SHA
    841a935 View commit details

Commits on Jul 16, 2024

  1. Copy the full SHA
    02942e1 View commit details

Commits on Jul 17, 2024

  1. Copy the full SHA
    6276da9 View commit details
  2. Merge pull request #14 from JosiahBull/jo/replace-hashmap-with-indexmap

    Replace hashmap with indexmap
    Flowneee authored Jul 17, 2024
    Copy the full SHA
    47470a8 View commit details
  3. Copy the full SHA
    8f3fa43 View commit details

Commits on Jul 18, 2024

  1. Copy the full SHA
    0ca36ab View commit details

Commits on Jul 21, 2024

  1. Automatic body detection.

    Also rename request_body attribute to body and axum-integration feature to axum
    Flowneee committed Jul 21, 2024
    Copy the full SHA
    b91eca2 View commit details

Commits on Jul 23, 2024

  1. Copy the full SHA
    3d25438 View commit details

Commits on Jul 24, 2024

  1. Sort operations before building spec to ensure same order in spec reg…

    …ardless of the order they added to router
    Flowneee committed Jul 24, 2024
    Copy the full SHA
    f90ccde View commit details
  2. Copy the full SHA
    ad54845 View commit details

Commits on Jul 25, 2024

  1. New actions

    Flowneee committed Jul 25, 2024
    Copy the full SHA
    74714de View commit details
  2. Update syn to version 2

    Flowneee committed Jul 25, 2024
    Copy the full SHA
    9df21a2 View commit details

Commits on Jul 26, 2024

  1. First macro tests

    Flowneee committed Jul 26, 2024
    Copy the full SHA
    001dc22 View commit details

Commits on Aug 7, 2024

  1. Copy the full SHA
    d94da30 View commit details
  2. Copy the full SHA
    af90c55 View commit details
Showing with 1,933 additions and 796 deletions.
  1. +17 −50 .github/workflows/ci.yml
  2. +1 −0 .gitignore
  3. +3 −1 Cargo.toml
  4. +14 −16 README.md
  5. +14 −0 okapi-examples/Cargo.toml
  6. +60 −0 okapi-examples/src/main.rs
  7. +28 −0 okapi-operation-macro/CHANGELOG.md
  8. +8 −6 okapi-operation-macro/Cargo.toml
  9. +8 −1 okapi-operation-macro/src/error.rs
  10. +4 −5 okapi-operation-macro/src/lib.rs
  11. +61 −2 okapi-operation-macro/src/operation/external_docs.rs
  12. +64 −19 okapi-operation-macro/src/operation/mod.rs
  13. +10 −12 okapi-operation-macro/src/operation/parameters.rs
  14. +0 −84 okapi-operation-macro/src/operation/request_body.rs
  15. +39 −0 okapi-operation-macro/src/operation/request_body/axum.rs
  16. +122 −0 okapi-operation-macro/src/operation/request_body/mod.rs
  17. +14 −14 okapi-operation-macro/src/operation/response.rs
  18. +74 −13 okapi-operation-macro/src/operation/security.rs
  19. +15 −18 okapi-operation-macro/src/utils.rs
  20. +76 −0 okapi-operation/CHANGELOG.md
  21. +15 −7 okapi-operation/Cargo.toml
  22. +54 −14 okapi-operation/docs/axum_integration.md
  23. +40 −35 okapi-operation/docs/root.md
  24. +64 −69 okapi-operation/src/axum_integration/handler_traits.rs
  25. +154 −129 okapi-operation/src/axum_integration/method_router.rs
  26. +84 −115 okapi-operation/src/axum_integration/mod.rs
  27. +234 −108 okapi-operation/src/axum_integration/router.rs
  28. +41 −3 okapi-operation/src/axum_integration/trait_impls.rs
  29. 0 okapi-operation/src/{ → axum_integration}/utils.rs
  30. +34 −0 okapi-operation/src/axum_integration/yaml.rs
  31. +254 −61 okapi-operation/src/builder.rs
  32. +45 −5 okapi-operation/src/components.rs
  33. +14 −5 okapi-operation/src/lib.rs
  34. +67 −4 okapi-operation/src/to_media_types.rs
  35. +52 −0 okapi-operation/src/to_responses.rs
  36. +133 −0 okapi-operation/tests/axum_integration.rs
  37. +10 −0 okapi-operation/tests/base.rs
  38. +6 −0 rustfmt.toml
67 changes: 17 additions & 50 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -6,64 +6,31 @@ env:
CARGO_TERM_COLOR: always

jobs:
check:
name: Check
clippy:
name: Run linter
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
command: check
args: --all-features
components: clippy
- run: cargo clippy --all-features -- -D warnings

test:
name: Test Suite
# TODO: add coverage
test_cov:
name: Run tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --all-features
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --all-features

fmt:
name: Rustfmt
name: Check code formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
command: clippy
args: --all-features -- -D warnings
components: rustfmt
- run: cargo fmt --all -- --check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.idea
Cargo.lock
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[workspace]
resolver = "2"
members = [
"./okapi-operation-macro",
"./okapi-operation"
"./okapi-operation",
"./okapi-examples"
]
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,11 +4,12 @@
[![docs.rs](https://img.shields.io/docsrs/okapi-operation/latest)](https://docs.rs/okapi-operation/latest)
![CI](https://github.com/Flowneee/okapi-operation/actions/workflows/ci.yml/badge.svg)

Library which allow to generate OpenAPI's operation definitions (using types from `okapi` crate) with procedural macro `#[openapi]`.
Library which allow to generate OpenAPI's operation definitions (using types from `okapi` crate) with procedural
macro `#[openapi]`.

## Example (with axum-integration feature).

```rust,compile
```rust,no_run
use axum::{extract::Query, Json};
use okapi_operation::{axum_integration::*, *};
use serde::Deserialize;
@@ -38,35 +39,32 @@ async fn echo_get(query: Query<Request>) -> Json<String> {
tags = "echo"
)]
async fn echo_post(
#[request_body(description = "Echo data", required = true)] body: Json<Request>,
#[body(description = "Echo data", required = true)] body: Json<Request>,
) -> Json<String> {
Json(body.0.data)
}
fn main() {
// Here you can also add security schemes, other operations, modify internal OpenApi object.
let oas_builder = OpenApiBuilder::new("Demo", "1.0.0");
let app = Router::new()
.route("/echo/get", get(openapi_handler!(echo_get)))
.route("/echo/post", post(openapi_handler!(echo_post)))
.route_openapi_specification("/openapi", oas_builder)
.finish_openapi("/openapi", "Demo", "1.0.0")
.expect("no problem");
let fut = async {
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
};
//tokio::runtime::Runtime::new().block_on(fut);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app.into_make_service()).await.unwrap()
}
```

## Features

* `macro`: enables re-import of `#[openapi]` macro (enabled by default);
* `axum-integration`: enables integration with `axum`(https://github.com/tokio-rs/axum) crate (implement traits for certain `axum` types).
* `axum-integration`: enables integration with `axum`(https://github.com/tokio-rs/axum) crate (implement traits for
certain `axum` types):
* Compatibility with `axum`: since integration heavely rely on `axum` types, this crate will be compatible only with
few (maybe even one) last versions of `axum`;
* Currently supported `axum` versions: `0.7.x`.
* `yaml`: enables ability to serve the spec in yaml format in case of present `Accept` header with `yaml` value.
Otherwise, in case of values `json|*/*` or empty, `json`'s being served (currently affects only `axum-integration`).

## TODO

14 changes: 14 additions & 0 deletions okapi-examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "okapi-examples"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.7"
axum-extra = { version = "0.9", features = ["typed-header"] }
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
okapi-operation = { path = "../okapi-operation", features = ["axum", "yaml"] }
okapi-operation-macro = { path = "../okapi-operation-macro", features = ["axum"] }
60 changes: 60 additions & 0 deletions okapi-examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use axum::{extract::Query, Json};
use okapi_operation::{axum_integration::*, *};
use serde::Deserialize;

#[derive(Deserialize, JsonSchema)]
struct Request {
/// Echo data
data: String,
}

#[openapi(
summary = "Echo using GET request",
operation_id = "echo_get",
tags = "echo",
parameters(
query(name = "echo-data", required = true, schema = "std::string::String",),
header(name = "x-request-id", schema = "std::string::String",),
header(name = "Accept", schema = "std::string::String")
)
)]
async fn echo_get(query: Query<Request>) -> Json<String> {
Json(query.0.data)
}

#[openapi(
summary = "Echo using POST request",
operation_id = "echo_post",
tags = "echo"
)]
async fn echo_post(
#[body(description = "Echo data", required = true)] body: Json<Request>,
) -> Json<String> {
Json(body.0.data)
}

// Detect schema from known types, Json in this case
#[openapi(
summary = "Echo using PUT request",
operation_id = "echo_put",
tags = "echo"
)]
async fn echo_put(body: Json<Request>) -> Json<String> {
Json(body.0.data)
}

#[tokio::main]
async fn main() {
let app = Router::new()
.route(
"/echo",
get(oh!(echo_get)).post(oh!(echo_post)).put(oh!(echo_put)),
)
.finish_openapi("/openapi", "Demo", "1.0.0")
.expect("no problem");

let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap()
}
28 changes: 28 additions & 0 deletions okapi-operation-macro/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,34 @@ 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/).


## [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
### Changed
- `#[request_body]` attribute can be used without braces.


## [0.1.3] - 2023-04-29
### Changed
- `axum` bumped to `0.6`.


## [0.1.2] - 2023-03-07
### Changed
- Used version 0.14.3 of `darling`.


## [0.1.1] - 2022-08-06
### Added
- Cookie parameters.
14 changes: 8 additions & 6 deletions 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.1"
version = "0.2.0"
authors = ["Andrey Kononov flowneee3@gmail.com"]
edition = "2021"
license = "MIT"
@@ -14,13 +14,15 @@ repository = "https://github.com/Flowneee/okapi-operation"
proc-macro = true

[dependencies]
darling = "0.13"
lazy_static = "1.4"
darling = "0.20"
lazy_static = "1"
proc-macro2 = "1"
quote = "1"
syn = { version = "1", features = ["full"] }
syn = { version = "2", features = ["full"] }
thiserror = "1"

[features]
axum = []

[dev-dependencies]
axum = "0.5"
okapi = { version = "0.7.0-rc.1", features = ["preserve_order"] }
assert_matches = "1"
9 changes: 8 additions & 1 deletion okapi-operation-macro/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Display;

use proc_macro2::TokenStream;
use proc_macro2::{Span, TokenStream};
use quote::ToTokens;

#[derive(Debug, thiserror::Error)]
@@ -9,6 +9,8 @@ pub(crate) enum Error {
Syn(#[from] syn::Error),
#[error(transparent)]
Darling(#[from] darling::Error),
#[error("{0}")]
Custom(String),
}

impl Error {
@@ -20,6 +22,11 @@ impl Error {
match self {
Error::Syn(x) => x.into_compile_error(),
Error::Darling(x) => x.write_errors(),
Error::Custom(x) => syn::Error::new(Span::call_site(), x).into_compile_error(),
}
}

pub(crate) fn custom(message: impl Into<String>) -> Self {
Self::Custom(message.into())
}
}
9 changes: 4 additions & 5 deletions okapi-operation-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use syn::{parse_macro_input, AttributeArgs, ItemFn};
#![allow(clippy::manual_unwrap_or_default)]

use syn::{parse_macro_input, ItemFn};

mod error;
mod operation;
@@ -11,10 +13,7 @@ pub fn openapi(
attr: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
match operation::openapi(
parse_macro_input!(attr as AttributeArgs),
parse_macro_input!(input as ItemFn),
) {
match operation::openapi(attr, parse_macro_input!(input as ItemFn)) {
Ok(x) => x.into(),
Err(err) => err.write().into(),
}
Loading