Skip to content

Commit ac02fe7

Browse files
committed
Update
1 parent 722ca19 commit ac02fe7

File tree

30 files changed

+353
-32
lines changed

30 files changed

+353
-32
lines changed

bin/configs/manual/rust-axum-apikey-authorization.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ generateAliasAsModel: true
66
additionalProperties:
77
hideGenerationTimestamp: "true"
88
packageName: apikey-authorization
9-
havingAuthorization: true
9+
basicAuthorization: true
10+
basicAnalytic: true
1011
globalProperties:
1112
skipFormModel: false
1213
enablePostProcessFile: true

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustAxumServerCodegen.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public class RustAxumServerCodegen extends AbstractRustCodegen implements Codege
6161
private Boolean allowBlockingValidator = false;
6262
private Boolean allowBlockingResponseSerialize = false;
6363
private String externCrateName;
64-
private Boolean havingAuthorization = false;
64+
private Boolean basicAuthorization = false;
65+
private Boolean basicAnalytic = false;
6566

6667
// Types
6768
private static final String uuidType = "uuid::Uuid";
@@ -227,11 +228,6 @@ public RustAxumServerCodegen() {
227228
optAllowBlockingResponseSerialize.setType("bool");
228229
optAllowBlockingResponseSerialize.defaultValue(allowBlockingResponseSerialize.toString());
229230

230-
CliOption optHavingAuthorization = new CliOption("havingAuthorization",
231-
String.join("", "Set this option to true will generate authorization handle for all authenticated operations."));
232-
optHavingAuthorization.setType("bool");
233-
optHavingAuthorization.defaultValue(havingAuthorization.toString());
234-
235231
cliOptions = new ArrayList<>(
236232
List.of(
237233
new CliOption(CodegenConstants.PACKAGE_NAME,
@@ -241,8 +237,7 @@ public RustAxumServerCodegen() {
241237
"Rust crate version."),
242238
optDisableValidator,
243239
optAllowBlockingValidator,
244-
optAllowBlockingResponseSerialize,
245-
optHavingAuthorization
240+
optAllowBlockingResponseSerialize
246241
)
247242
);
248243

@@ -324,10 +319,16 @@ public void processOpts() {
324319
additionalProperties.put("allowBlockingResponseSerialize", allowBlockingResponseSerialize);
325320
}
326321

327-
if (additionalProperties.containsKey("havingAuthorization")) {
328-
havingAuthorization = convertPropertyToBooleanAndWriteBack("havingAuthorization");
322+
if (additionalProperties.containsKey("basicAuthorization")) {
323+
basicAuthorization = convertPropertyToBooleanAndWriteBack("basicAuthorization");
324+
} else {
325+
additionalProperties.put("basicAuthorization", basicAuthorization);
326+
}
327+
328+
if (additionalProperties.containsKey("basicAnalytic")) {
329+
basicAnalytic = convertPropertyToBooleanAndWriteBack("basicAnalytic");
329330
} else {
330-
additionalProperties.put("havingAuthorization ", havingAuthorization);
331+
additionalProperties.put("basicAnalytic", basicAnalytic);
331332
}
332333
}
333334

@@ -736,12 +737,17 @@ public OperationsMap postProcessOperationsWithModels(final OperationsMap operati
736737
operations.getOperation().forEach(op -> op.vendorExtensions.put("havingAuthMethod", true));
737738
this.havingAuthMethods = true;
738739

739-
if (havingAuthorization) {
740-
operations.put("havingAuthorization", true);
741-
operations.getOperation().forEach(op -> op.vendorExtensions.put("havingAuthorization", true));
740+
if (basicAuthorization) {
741+
operations.put("basicAuthorization", true);
742+
operations.getOperation().forEach(op -> op.vendorExtensions.put("basicAuthorization", true));
742743
}
743744
}
744745

746+
if (basicAnalytic) {
747+
operations.put("basicAnalytic", true);
748+
operations.getOperation().forEach(op -> op.vendorExtensions.put("basicAnalytic", true));
749+
}
750+
745751
return operationsMap;
746752
}
747753

modules/openapi-generator/src/main/resources/rust-axum/apis-mod.mustache

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,38 @@ pub mod {{classFilename}};
44
{{/apis}}
55
{{/apiInfo}}
66

7-
{{#havingAuthorization}}
7+
{{#basicAuthorization}}
88
#[allow(dead_code)]
99
#[derive(Debug, Eq, PartialEq)]
1010
pub enum Authorization {
1111
Authorized,
1212
Forbidden,
1313
}
14-
{{/havingAuthorization}}
14+
{{/basicAuthorization}}
15+
16+
{{#basicAnalytic}}
17+
pub mod event {
18+
/// Anything to be recorded.
19+
pub type Event = std::collections::HashMap<String, String>;
20+
21+
pub mod convention {
22+
pub const EVENT_SERVICE: &str = "_service_";
23+
pub const EVENT_ACTOR: &str = "_actor_";
24+
pub const EVENT_ACTION: &str = "_action_";
25+
pub const EVENT_RESOURCE_TYPE: &str = "_resource_type_";
26+
pub const EVENT_RESOURCE: &str = "_resource_";
27+
pub const EVENT_STATUS_CODE: &str = "_status_code_";
28+
pub const EVENT_LATENCY_SECS: &str = "_latency_secs_";
29+
pub const EVENT_TIMESTAMP: &str = "timestamp";
30+
}
31+
}
32+
33+
#[async_trait::async_trait]
34+
pub trait EventDispatcher {
35+
fn service_name(&self) -> String;
36+
async fn dispatch(&self, event: event::Event) {}
37+
}
38+
{{/basicAnalytic}}
1539

1640
{{#authMethods}}
1741
{{#isApiKey}}

modules/openapi-generator/src/main/resources/rust-axum/apis.mustache

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{models, types::*};
1616

1717
{{#operations}}
1818

19-
{{#havingAuthorization}}
19+
{{#basicAuthorization}}
2020
/// {{classnamePascalCase}} APIs - Authorization.
2121
#[async_trait]
2222
#[allow(clippy::ptr_arg)]
@@ -26,7 +26,7 @@ pub trait {{classnamePascalCase}}Authorization {
2626
{{#operation}}
2727
{{#vendorExtensions}}
2828
{{#x-has-auth-methods}}
29-
{{#havingAuthorization}}
29+
{{#basicAuthorization}}
3030
{{#vendorExtensions}}
3131
/// Authorization{{#summary}} - {{{.}}}{{/summary}}.
3232
/// {{{operationId}}} - {{{httpMethod}}} {{{basePathWithoutHost}}}{{{path}}}
@@ -74,15 +74,15 @@ pub trait {{classnamePascalCase}}Authorization {
7474
Ok(super::Authorization::Authorized)
7575
}
7676
{{/vendorExtensions}}
77-
{{/havingAuthorization}}
77+
{{/basicAuthorization}}
7878
{{/x-has-auth-methods}}
7979
{{/vendorExtensions}}
8080
{{^-last}}
8181

8282
{{/-last}}
8383
{{/operation}}
8484
}
85-
{{/havingAuthorization}}
85+
{{/basicAuthorization}}
8686

8787
/// {{classnamePascalCase}}
8888
#[async_trait]
@@ -101,6 +101,7 @@ pub trait {{classnamePascalCase}}<E: std::fmt::Debug + Send + Sync + 'static = (
101101
/// {{{operationId}}} - {{{httpMethod}}} {{{basePathWithoutHost}}}{{{path}}}
102102
async fn {{{x-operation-id}}}(
103103
&self,
104+
{{#basicAnalytic}}event: &mut super::event::Event,{{/basicAnalytic}}
104105
method: &Method,
105106
host: &Host,
106107
cookies: &CookieJar,

modules/openapi-generator/src/main/resources/rust-axum/server-operation.mustache

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ async fn {{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}<I, A, E{
5959
) -> Result<Response, StatusCode>
6060
where
6161
I: AsRef<A> + Send + Sync,
62-
A: apis::{{classFilename}}::{{classnamePascalCase}}<E{{#havingAuthMethod}}, Claims = C{{/havingAuthMethod}}>{{#havingAuthMethod}}{{#havingAuthorization}} + apis::{{classFilename}}::{{classnamePascalCase}}Authorization<Claims = C>{{/havingAuthorization}}{{/havingAuthMethod}}{{#vendorExtensions}}{{#x-has-cookie-auth-methods}}+ apis::CookieAuthentication<Claims = C>{{/x-has-cookie-auth-methods}}{{#x-has-header-auth-methods}}+ apis::ApiKeyAuthHeader<Claims = C>{{/x-has-header-auth-methods}}{{#x-has-basic-auth-methods}}+ apis::ApiAuthBasic<Claims = C>{{/x-has-basic-auth-methods}}{{/vendorExtensions}} + Send + Sync,
62+
A: {{#basicAnalytic}}apis::EventDispatcher + {{/basicAnalytic}}apis::{{classFilename}}::{{classnamePascalCase}}<E{{#havingAuthMethod}}, Claims = C{{/havingAuthMethod}}>{{#havingAuthMethod}}{{#basicAuthorization}} + apis::{{classFilename}}::{{classnamePascalCase}}Authorization<Claims = C>{{/basicAuthorization}}{{/havingAuthMethod}}{{#vendorExtensions}}{{#x-has-cookie-auth-methods}}+ apis::CookieAuthentication<Claims = C>{{/x-has-cookie-auth-methods}}{{#x-has-header-auth-methods}}+ apis::ApiKeyAuthHeader<Claims = C>{{/x-has-header-auth-methods}}{{#x-has-basic-auth-methods}}+ apis::ApiAuthBasic<Claims = C>{{/x-has-basic-auth-methods}}{{/vendorExtensions}} + Send + Sync,
6363
E: std::fmt::Debug + Send + Sync + 'static,
6464
{
65+
{{#basicAnalytic}}let start_at = chrono::Utc::now();{{/basicAnalytic}}
66+
6567
{{#vendorExtensions}}
6668
{{#x-has-auth-methods}}
6769
// Authentication
@@ -196,7 +198,7 @@ where
196198

197199
{{#vendorExtensions}}
198200
{{#x-has-auth-methods}}
199-
{{#havingAuthorization}}
201+
{{#basicAuthorization}}
200202
{{#vendorExtensions}}
201203
// Authorization
202204
let authorization = api_impl.as_ref().{{{x-operation-id}}}_authorize(
@@ -239,11 +241,13 @@ where
239241
Err(_) => { return response_with_status_code_only(StatusCode::INTERNAL_SERVER_ERROR); }
240242
}
241243
{{/vendorExtensions}}
242-
{{/havingAuthorization}}
244+
{{/basicAuthorization}}
243245
{{/x-has-auth-methods}}
244246
{{/vendorExtensions}}
245247

248+
{{#basicAnalytic}}let mut event = apis::event::Event::default();{{/basicAnalytic}}
246249
let result = api_impl.as_ref().{{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}(
250+
{{#basicAnalytic}}&mut event,{{/basicAnalytic}}
247251
&method,
248252
&host,
249253
&cookies,
@@ -407,5 +411,16 @@ where
407411
},
408412
};
409413

414+
{{#basicAnalytic}}
415+
if let Ok(resp) = resp.as_ref() && !event.is_empty() {
416+
event.insert(apis::event::convention::EVENT_TIMESTAMP.to_string(), format!("{start_at:?}"));
417+
event.insert(apis::event::convention::EVENT_SERVICE.to_string(), api_impl.as_ref().service_name());
418+
event.insert(apis::event::convention::EVENT_STATUS_CODE.to_string(), resp.status().as_u16().to_string());
419+
event.insert(apis::event::convention::EVENT_ACTION.to_string(), "{{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}".to_string());
420+
event.insert(apis::event::convention::EVENT_LATENCY_SECS.to_string(), format!("{:.6}", chrono::Utc::now().signed_duration_since(start_at).as_seconds_f64()));
421+
api_impl.as_ref().dispatch(event).await;
422+
}
423+
{{/basicAnalytic}}
424+
410425
resp.map_err(|e| { error!(error = ?e); StatusCode::INTERNAL_SERVER_ERROR })
411426
}

modules/openapi-generator/src/main/resources/rust-axum/server-route.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pub fn new<I, A, E{{#havingAuthMethods}}, C{{/havingAuthMethods}}>(api_impl: I) -> Router
33
where
44
I: AsRef<A> + Clone + Send + Sync + 'static,
5-
A: {{#apiInfo}}{{#apis}}{{#operations}}apis::{{classFilename}}::{{classnamePascalCase}}<E{{#havingAuthMethod}}, Claims = C{{/havingAuthMethod}}>{{#havingAuthMethod}}{{#havingAuthorization}} + apis::{{classFilename}}::{{classnamePascalCase}}Authorization<Claims = C>{{/havingAuthorization}}{{/havingAuthMethod}} + {{/operations}}{{/apis}}{{/apiInfo}}{{#authMethods}}{{#isApiKey}}{{#isKeyInCookie}}apis::CookieAuthentication<Claims = C> + {{/isKeyInCookie}}{{#isKeyInHeader}}apis::ApiKeyAuthHeader<Claims = C> + {{/isKeyInHeader}}{{/isApiKey}}{{#isBasic}}apis::ApiAuthBasic<Claims = C> + {{/isBasic}}{{/authMethods}}Send + Sync + 'static,
5+
A: {{#basicAnalytic}}apis::EventDispatcher + {{/basicAnalytic}}{{#apiInfo}}{{#apis}}{{#operations}}apis::{{classFilename}}::{{classnamePascalCase}}<E{{#havingAuthMethod}}, Claims = C{{/havingAuthMethod}}>{{#havingAuthMethod}}{{#basicAuthorization}} + apis::{{classFilename}}::{{classnamePascalCase}}Authorization<Claims = C>{{/basicAuthorization}}{{/havingAuthMethod}} + {{/operations}}{{/apis}}{{/apiInfo}}{{#authMethods}}{{#isApiKey}}{{#isKeyInCookie}}apis::CookieAuthentication<Claims = C> + {{/isKeyInCookie}}{{#isKeyInHeader}}apis::ApiKeyAuthHeader<Claims = C> + {{/isKeyInHeader}}{{/isApiKey}}{{#isBasic}}apis::ApiAuthBasic<Claims = C> + {{/isBasic}}{{/authMethods}}Send + Sync + 'static,
66
E: std::fmt::Debug + Send + Sync + 'static,
77
{{#havingAuthMethods}}C: Send + Sync + 'static,{{/havingAuthMethods}}
88
{

samples/server/petstore/rust-axum/output/apikey-authorization/src/apis/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ pub enum Authorization {
77
Forbidden,
88
}
99

10+
pub mod event {
11+
/// Anything to be recorded.
12+
pub type Event = std::collections::HashMap<String, String>;
13+
14+
pub mod convention {
15+
pub const EVENT_SERVICE: &str = "_service_";
16+
pub const EVENT_ACTOR: &str = "_actor_";
17+
pub const EVENT_ACTION: &str = "_action_";
18+
pub const EVENT_RESOURCE_TYPE: &str = "_resource_type_";
19+
pub const EVENT_RESOURCE: &str = "_resource_";
20+
pub const EVENT_STATUS_CODE: &str = "_status_code_";
21+
pub const EVENT_LATENCY_SECS: &str = "_latency_secs_";
22+
pub const EVENT_TIMESTAMP: &str = "timestamp";
23+
}
24+
}
25+
26+
#[async_trait::async_trait]
27+
pub trait EventDispatcher {
28+
fn service_name(&self) -> String;
29+
async fn dispatch(&self, event: event::Event) {}
30+
}
31+
1032
/// API Key Authentication - Header.
1133
#[async_trait::async_trait]
1234
pub trait ApiKeyAuthHeader {

samples/server/petstore/rust-axum/output/apikey-authorization/src/apis/payments.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub trait Payments<E: std::fmt::Debug + Send + Sync + 'static = ()>:
9393
/// GetPaymentMethodById - GET /v71/paymentMethods/{id}
9494
async fn get_payment_method_by_id(
9595
&self,
96+
event: &mut super::event::Event,
9697
method: &Method,
9798
host: &Host,
9899
cookies: &CookieJar,
@@ -105,6 +106,7 @@ pub trait Payments<E: std::fmt::Debug + Send + Sync + 'static = ()>:
105106
/// GetPaymentMethods - GET /v71/paymentMethods
106107
async fn get_payment_methods(
107108
&self,
109+
event: &mut super::event::Event,
108110
method: &Method,
109111
host: &Host,
110112
cookies: &CookieJar,
@@ -116,6 +118,7 @@ pub trait Payments<E: std::fmt::Debug + Send + Sync + 'static = ()>:
116118
/// PostMakePayment - POST /v71/payments
117119
async fn post_make_payment(
118120
&self,
121+
event: &mut super::event::Event,
119122
method: &Method,
120123
host: &Host,
121124
cookies: &CookieJar,

0 commit comments

Comments
 (0)