Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various Cli-ng fixes #547

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ _book/
.vscode/
.idea/
/rust-toolchain.toml
testgen/
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.9.4] - 2024-10-23
### Fixed
- Various cli-ng fixes. [PR#547](https://github.com/paperclip-rs/paperclip/pull/547)

## [0.9.3] - 2024-10-21
### Added
- Experimental openapiv3 cli codegen. [PR#506](https://github.com/paperclip-rs/paperclip/pull/506)
Expand Down Expand Up @@ -86,7 +90,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow automatically adding the module path to the openapi component name, via a feature "path-in-definition" [PR#373](https://github.com/paperclip-rs/paperclip/pull/373)
- Add missing ip, ipv4 and ipv6 string format types
- Add support for actix-web 4
- Middleware support does not support non-`BoxBody` response payload types.
- Middleware support does not support non-`BoxBody` response payload types.
As a workaround you can use `actix-web::middlware::Compat`.
- Add support for Schemas wrapping Generic types (e.g. `DataResponse<T>` where `T` also derives
`Apiv2Schema`) [PR#332](https://github.com/paperclip-rs/paperclip/pull/332)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "paperclip"
version = "0.9.3"
version = "0.9.4"
edition = "2018"
description = "OpenAPI tooling library for type-safe compile-time checked HTTP APIs"
documentation = "https://paperclip-rs.github.io/paperclip/paperclip"
Expand Down
2 changes: 1 addition & 1 deletion cli-ng/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "paperclip-ng"
version = "0.1.0"
version = "0.1.1"
edition = "2018"
license = "MIT OR Apache-2.0"
keywords = [ "openapi", "openapiv3", "cli", "codegen" ]
Expand Down
16 changes: 16 additions & 0 deletions cli-ng/src/v3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ struct SupportingTpl<'a> {
package_version: &'a str,
package_libname: &'a str,
package_edition: &'a str,
// todo: should be configurable.
api_doc_path: &'a str,
model_doc_path: &'a str,
}
#[derive(Clone, Content)]
#[ramhorns(rename_all = "camelCase")]
Expand All @@ -98,12 +101,14 @@ struct OperationsTpl<'a> {
pub(super) struct OperationsApiTpl<'a> {
classname: &'a str,
class_filename: &'a str,
has_auth_methods: bool,

operations: OperationsTpl<'a>,
}
pub(super) struct OperationsApi {
classname: String,
class_filename: String,
has_auth_methods: bool,

operations: Vec<Operation>,
}
Expand All @@ -119,6 +124,7 @@ impl OpenApiV3 {
.map(|o| OperationsApiTpl {
classname: o.classname(),
class_filename: o.class_filename(),
has_auth_methods: o.has_auth_methods,
operations: OperationsTpl {
operation: &o.operations,
},
Expand Down Expand Up @@ -244,6 +250,8 @@ impl OpenApiV3 {
package_version: self.package_info.version.as_str(),
package_libname: self.package_info.libname.as_str(),
package_edition: self.package_info.edition.as_str(),
api_doc_path: "docs/apis/",
model_doc_path: "docs/models/",
},
)?;

Expand Down Expand Up @@ -322,10 +330,17 @@ impl OpenApiV3 {
.api
.operations()
.map(|(path, method, operation)| Operation::new(self, path, method, operation))
.sorted_by(Self::sort_op_id)
.collect::<Vec<Operation>>();

Ok(operation)
}
fn sort_op_id(a: &Operation, b: &Operation) -> std::cmp::Ordering {
a.operation_id_original
.clone()
.unwrap_or_default()
.cmp(&b.operation_id_original.clone().unwrap())
}
fn apis(&self, operations: &Vec<Operation>) -> Result<Vec<OperationsApi>, std::io::Error> {
let mut tags = std::collections::HashMap::<String, OperationsApi>::new();
for op in operations {
Expand Down Expand Up @@ -485,6 +500,7 @@ impl From<&Operation> for OperationsApi {
class_filename: src.class_filename().into(),
classname: src.classname().into(),
operations: vec![src.clone()],
has_auth_methods: src.has_auth_methods,
}
}
}
Expand Down
87 changes: 76 additions & 11 deletions cli-ng/src/v3/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use super::{OpenApiV3, Parameter, Property};

use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase};
use itertools::Itertools;
use ramhorns_derive::Content;

use log::debug;
Expand Down Expand Up @@ -38,7 +39,7 @@ pub(crate) struct Operation {

path: String,
operation_id: Option<String>,
return_type: String,
return_type: Option<String>,
return_format: String,
http_method: String,
return_base_type: String,
Expand All @@ -54,8 +55,6 @@ pub(crate) struct Operation {
has_produces: bool,
prioritized_content_types: Vec<std::collections::HashMap<String, String>>,

body_param: Parameter,

all_params: Vec<Parameter>,
has_params: bool,
path_params: Vec<Parameter>,
Expand All @@ -64,6 +63,8 @@ pub(crate) struct Operation {
has_query_params: bool,
header_params: Vec<Parameter>,
has_header_params: bool,
has_body_param: bool,
body_param: Option<Parameter>,
implicit_headers_params: Vec<Parameter>,
has_implicit_headers_params: bool,
form_params: Vec<Parameter>,
Expand All @@ -72,8 +73,8 @@ pub(crate) struct Operation {
has_required_params: bool,
optional_params: Vec<Parameter>,
has_optional_params: bool,
auth_methods: Vec<Parameter>,
has_auth_methods: bool,
auth_methods: Vec<AuthMethod>,
pub(crate) has_auth_methods: bool,

tags: Vec<String>,
responses: Vec<()>,
Expand All @@ -84,9 +85,15 @@ pub(crate) struct Operation {

vendor_extensions: HashMap<String, String>,

operation_id_original: Option<String>,
pub(crate) operation_id_original: Option<String>,
operation_id_camel_case: Option<String>,
operation_id_lower_case: Option<String>,
support_multiple_responses: bool,

description: Option<String>,

api_doc_path: &'static str,
model_doc_path: &'static str,
}

fn query_param(api: &OpenApiV3, value: &openapiv3::Parameter) -> Option<Parameter> {
Expand Down Expand Up @@ -117,6 +124,9 @@ fn header_param(api: &OpenApiV3, value: &openapiv3::Parameter) -> Option<Paramet
_ => None,
}
}
fn body_param(api: &OpenApiV3, value: &openapiv3::RequestBody) -> Option<Parameter> {
Parameter::from_body(api, value)
}

impl Operation {
/// Create an Operation based on the deserialized openapi operation.
Expand Down Expand Up @@ -161,22 +171,33 @@ impl Operation {
openapiv3::ReferenceOr::Item(item) => path_param(root, item),
}
})
.sorted_by(|a, b| b.required().cmp(&a.required()))
.collect::<Vec<_>>();
let body_param = operation.request_body.as_ref().and_then(|p| {
match p {
// todo: need to handle this
openapiv3::ReferenceOr::Reference { .. } => todo!(),
openapiv3::ReferenceOr::Item(item) => body_param(root, item),
}
});

let mut ext_path = path.to_string();
for param in &path_params {
if param.data_format() == "url" {
//info!("path: {path}");
//info!("path_params: {param:?}");
ext_path = path.replace(param.name(), &format!("{}:.*", param.base_name()));
vendor_extensions.insert("x-actix-query-string".into(), "true".into());
}
}
vendor_extensions.insert("x-actixPath".into(), ext_path);

let all_params = query_params
let all_params = path_params
.iter()
.chain(&path_params)
.chain(
query_params
.iter()
.sorted_by(|a, b| b.required().cmp(&a.required())),
)
.chain(&body_param)
.cloned()
.collect::<Vec<_>>();
// todo: support multiple responses
Expand All @@ -200,11 +221,13 @@ impl Operation {
None => (String::new(), String::new()),
};
Self {
description: operation.description.as_ref().map(|d| d.replace('\n', " ")),
classname: class,
class_filename: class_file,
summary: operation.summary.clone(),
tags: operation.tags.clone(),
is_deprecated: Some(operation.deprecated),
operation_id_lower_case: operation.operation_id.as_ref().map(|o| o.to_lowercase()),
operation_id_camel_case: operation
.operation_id
.as_ref()
Expand All @@ -219,12 +242,45 @@ impl Operation {
query_params,
header_params: vec![],
has_header_params: false,
has_body_param: body_param.is_some(),
body_param,
path: path.to_string(),
http_method: method.to_upper_camel_case(),
support_multiple_responses: false,
return_type: return_model.data_type(),
return_type: {
let data_type = return_model.data_type();
if data_type == "()" {
None
} else {
Some(data_type)
}
},
has_auth_methods: operation.security.is_some(),
auth_methods: match &operation.security {
None => vec![],
Some(sec) => sec
.iter()
.flat_map(|a| {
a.iter()
.map(|(key, _)| match key.as_str() {
"JWT" => AuthMethod {
scheme: "JWT".to_string(),
is_basic: true,
is_basic_bearer: true,
},
scheme => AuthMethod {
scheme: scheme.to_string(),
is_basic: false,
is_basic_bearer: false,
},
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>(),
},
vendor_extensions,
api_doc_path: "docs/apis/",
model_doc_path: "docs/models/",
..Default::default()
}
}
Expand All @@ -241,3 +297,12 @@ impl Operation {
&self.class_filename
}
}

#[derive(Default, Content, Clone, Debug)]
#[ramhorns(rename_all = "camelCase")]
struct AuthMethod {
scheme: String,

is_basic: bool,
is_basic_bearer: bool,
}
Loading
Loading