Skip to content

Commit

Permalink
Prepare apistos v1
Browse files Browse the repository at this point in the history
  • Loading branch information
rlebran committed Jul 11, 2024
1 parent ff7bee6 commit e4dceb2
Show file tree
Hide file tree
Showing 89 changed files with 7,468 additions and 1,213 deletions.
1 change: 1 addition & 0 deletions .github/workflows/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- schemars-v1

env:
CARGO_TERM_COLOR: always
Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ documentation = "https://docs.rs/apistos/"
license = "MIT OR Apache-2.0"
rust-version = "1.75"
publish = true
version = "0.3.3"
version = "1.0.0-pre-release.3"

[workspace.dependencies]
actix-service = "2"
Expand All @@ -34,6 +34,7 @@ convert_case = "0.6"
darling = "0.20"
futures-core = "0.3.28"
indexmap = "2"
ipnetwork = "0.20"
log = "0.4.20"
num_cpus = "1.16"
md5 = "0.7.0"
Expand All @@ -43,8 +44,8 @@ proc-macro2 = "1.0"
proc-macro-error = "1.0"
quote = "1.0"
regex = ">=1.5.5"
schemars = { package = "apistos-schemars", version = "0.8", features = ["chrono", "uuid1", "url", "rust_decimal"] }
serde = "1"
schemars = { package = "apistos-schemars", version = "1.0.0-alpha.2", features = ["chrono04", "uuid1", "url2", "rust_decimal1", "ipnetwork"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
syn = "2.0"

Expand Down
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ carpenters, craftsmen, metallurgy ... which can also be considered by some as th
[dependencies]
#schemars = "0.8"
# sadly we currently rely on a fork to fix multiple flatten for enums, related PR can be found here: https://github.com/GREsau/schemars/pull/264
schemars = { package = "apistos-schemars", version = "0.8" }
apistos = "0.3"
schemars = { package = "apistos-schemars", version = "1.0.0-alpha.2" }
apistos = "1.0.0-pre-release.3"
```

### Usage example
Expand Down Expand Up @@ -168,23 +168,23 @@ For a complete example, see [the sample petstore](https://github.com/netwo-io/ap

### Feature flags

| name | description | extra dependencies |
|--------------------|--------------------------------------------------------------------------|-----------------------------------------------------------------|
| `query` (default) | Enables documenting `actix_web::web::Query` | |
| `actix` (default) | Enables documenting types from `actix` | |
| `lab_query` | Enables documenting `actix_web_lab::extract::Query` | [`actix-web-lab`](https://crates.io/crates/actix-web-lab) |
| `garde` | Enables input validation through `garde` | [`garde`](https://crates.io/crates/garde) |
| `actix-web-grants` | Enables support for `actix-web-grants` | [`actix-web-grants`](https://crates.io/crates/actix-web-grants) |
| `rapidoc` | Enables RapiDoc to expose the generated openapi file | |
| `redoc` | Enables Redoc to expose the generated openapi file | |
| `swagger-ui` | Enables Swagger UI to expose the generated openapi file | |
| `qs_query` | Enables documenting types from `serde_qs` | [`serde_qs`](https://crates.io/crates/serde-qs) |
| `chrono` | Enables documenting types from `chrono` | [`chrono`](https://crates.io/crates/chrono) |
| `multipart` | Enables documenting types from `actix-multipart` | [`actix-multipart`](https://crates.io/crates/actix-multipart) |
| `rust_decimal` | Enables documenting types from `rust_decimal` | [`rust_decimal`](https://crates.io/crates/rust-decimal) |
| `uuid` | Enables documenting types from `uuid` | [`uuid`](https://crates.io/crates/uuid) |
| `url` | Enables documenting types from `url` | [`url`](https://crates.io/crates/url) |
| `extras` | Enables `chrono`, `multipart`, `rust_decimal`, `uuid` and `url` features | All from previous features |
| name | description | extra dependencies |
|-------------------|--------------------------------------------------------------------------|---------------------------------------------------------------|
| `query` (default) | Enables documenting `actix_web::web::Query` | |
| `actix` (default) | Enables documenting types from `actix` | |
| `lab_query` | Enables documenting `actix_web_lab::extract::Query` | [`actix-web-lab`](https://crates.io/crates/actix-web-lab) |
| `garde` | Enables input validation through `garde` | [`garde`](https://crates.io/crates/garde) |
| `rapidoc` | Enables RapiDoc to expose the generated openapi file | |
| `redoc` | Enables Redoc to expose the generated openapi file | |
| `swagger-ui` | Enables Swagger UI to expose the generated openapi file | |
| `qs_query` | Enables documenting types from `serde_qs` | [`serde_qs`](https://crates.io/crates/serde-qs) |
| `chrono` | Enables documenting types from `chrono` | [`chrono`](https://crates.io/crates/chrono) |
| `ipnetwork` | Enables documenting types from `ipnetwork` | [`ipnetwork`](https://crates.io/crates/ipnetwork) |
| `multipart` | Enables documenting types from `actix-multipart` | [`actix-multipart`](https://crates.io/crates/actix-multipart) |
| `rust_decimal` | Enables documenting types from `rust_decimal` | [`rust_decimal`](https://crates.io/crates/rust-decimal) |
| `uuid` | Enables documenting types from `uuid` | [`uuid`](https://crates.io/crates/uuid) |
| `url` | Enables documenting types from `url` | [`url`](https://crates.io/crates/url) |
| `extras` | Enables `chrono`, `multipart`, `rust_decimal`, `uuid` and `url` features | All from previous features |

### What's next

Expand Down
14 changes: 9 additions & 5 deletions apistos-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ rust-version.workspace = true
license.workspace = true

[dependencies]
log = { workspace = true }
pin-project = { workspace = true }
serde_json = { workspace = true }
schemars = { workspace = true }

actix-web = { workspace = true, optional = true }
Expand All @@ -23,13 +25,14 @@ actix-web-grants = { workspace = true, optional = true }
actix-multipart = { workspace = true, optional = true }
garde-actix-web = { workspace = true, optional = true }
chrono = { workspace = true, optional = true }
ipnetwork = { workspace = true, optional = true }
rust_decimal = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_qs = { workspace = true, features = ["actix4"], optional = true }
uuid = { workspace = true, optional = true }
url = { workspace = true, optional = true }

apistos-models = { path = "../apistos-models", version = "0.3.3", features = ["deserialize"] }
apistos-models = { path = "../apistos-models", version = "1.0.0-pre-release.3", features = ["deserialize"] }

[dev-dependencies]
assert-json-diff = { workspace = true }
Expand All @@ -52,11 +55,12 @@ lab_query = ["actix", "dep:actix-web-lab", "garde-actix-web?/lab_query"]
garde = ["actix", "dep:garde-actix-web"]

# extra types related features
chrono = ["dep:chrono", "schemars/chrono"]
chrono = ["dep:chrono", "schemars/chrono04"]
ipnetwork = ["dep:ipnetwork", "schemars/ipnetwork"]
multipart = ["actix", "dep:serde", "dep:actix-multipart"]
rust_decimal = ["dep:rust_decimal", "schemars/rust_decimal"]
rust_decimal = ["dep:rust_decimal", "schemars/rust_decimal1"]
uuid = ["dep:uuid", "schemars/uuid1"]
url = ["dep:url", "schemars/url"]
extras = ["chrono", "multipart", "rust_decimal", "uuid", "url"]
url = ["dep:url", "schemars/url2"]
extras = ["chrono", "ipnetwork", "multipart", "rust_decimal", "uuid", "url"]

default = ["actix", "query"]
80 changes: 80 additions & 0 deletions apistos-core/src/__internal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::collections::BTreeMap;

use apistos_models::paths::{MediaType, Response, Responses};
use apistos_models::reference_or::ReferenceOr;
use apistos_models::{ApistosSchema, OpenApiVersion, VersionSpecificSchema};

pub fn response_from_schema(
oas_version: OpenApiVersion,
status: &str,
schema: Option<(String, ReferenceOr<ApistosSchema>)>,
) -> Option<Responses> {
schema.map(|(name, schema)| match schema {
ReferenceOr::Reference { _ref } => Responses {
responses: BTreeMap::from_iter(vec![(status.to_string(), ReferenceOr::Reference { _ref })]),
..Default::default()
},
ReferenceOr::Object(sch) => {
let schema = match oas_version {
OpenApiVersion::OAS3_0 => VersionSpecificSchema::OAS3_0(ReferenceOr::Reference {
_ref: format!("#/components/schemas/{}", name),
}),
OpenApiVersion::OAS3_1 => VersionSpecificSchema::OAS3_1(sch),
};
let response = Response {
content: BTreeMap::from_iter(vec![(
"application/json".to_string(),
MediaType {
schema: Some(schema),
..Default::default()
},
)]),
..Default::default()
};
Responses {
responses: BTreeMap::from_iter(vec![(status.to_string(), ReferenceOr::Object(response))]),
..Default::default()
}
}
})
}

pub fn response_from_raw_schema(
oas_version: OpenApiVersion,
status: &str,
raw_schema: Option<ReferenceOr<ApistosSchema>>,
) -> Option<Responses> {
raw_schema.map(|schema| match schema {
ReferenceOr::Reference { _ref } => Responses {
responses: BTreeMap::from_iter(vec![(status.to_string(), ReferenceOr::Reference { _ref })]),
..Default::default()
},
ReferenceOr::Object(sch) => {
let schema = match oas_version {
OpenApiVersion::OAS3_0 => VersionSpecificSchema::OAS3_0(sch.into()),
OpenApiVersion::OAS3_1 => VersionSpecificSchema::OAS3_1(sch),
};
let response = Response {
content: BTreeMap::from_iter(vec![(
"application/json".to_string(),
MediaType {
schema: Some(schema),
..Default::default()
},
)]),
..Default::default()
};
Responses {
responses: BTreeMap::from_iter(vec![(status.to_string(), ReferenceOr::Object(response))]),
..Default::default()
}
}
})
}

pub fn response_for_status(status: &str) -> Responses {
Responses {
responses: BTreeMap::from_iter(vec![(status.to_string(), ReferenceOr::Object(Default::default()))]),
..Default::default()
}
}
Loading

0 comments on commit e4dceb2

Please sign in to comment.