Skip to content

Commit 024b29a

Browse files
committed
Update
1 parent a0bc85f commit 024b29a

107 files changed

Lines changed: 3924 additions & 1271 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generatorName: rust-axum
2+
outputDir: samples/server/petstore/rust-axum/output/apikey-authorization
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/jetbrains/CheckoutBasicBearerCookieQueryHeaderBasicBearer.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/rust-axum
5+
generateAliasAsModel: true
6+
additionalProperties:
7+
hideGenerationTimestamp: "true"
8+
packageName: apikey-authorization
9+
havingAuthorization: true
10+
globalProperties:
11+
skipFormModel: false
12+
enablePostProcessFile: true

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ 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;
6465

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

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+
229235
cliOptions = new ArrayList<>(
230236
List.of(
231237
new CliOption(CodegenConstants.PACKAGE_NAME,
@@ -235,7 +241,8 @@ public RustAxumServerCodegen() {
235241
"Rust crate version."),
236242
optDisableValidator,
237243
optAllowBlockingValidator,
238-
optAllowBlockingResponseSerialize
244+
optAllowBlockingResponseSerialize,
245+
optHavingAuthorization
239246
)
240247
);
241248

@@ -286,7 +293,7 @@ public void processOpts() {
286293
LOGGER.info("Warning: Environment variable 'RUST_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
287294
}
288295

289-
if (!Boolean.TRUE.equals(ModelUtils.isGenerateAliasAsModel())) {
296+
if (!ModelUtils.isGenerateAliasAsModel()) {
290297
LOGGER.warn("generateAliasAsModel is set to false, which means array/map will be generated as model instead and the resulting code may have issues. Please enable `generateAliasAsModel` to address the issue.");
291298
}
292299

@@ -316,6 +323,12 @@ public void processOpts() {
316323
} else {
317324
additionalProperties.put("allowBlockingResponseSerialize", allowBlockingResponseSerialize);
318325
}
326+
327+
if (additionalProperties.containsKey("havingAuthorization")) {
328+
havingAuthorization = convertPropertyToBooleanAndWriteBack("havingAuthorization");
329+
} else {
330+
additionalProperties.put("havingAuthorization ", havingAuthorization);
331+
}
319332
}
320333

321334
private void setPackageName(String packageName) {
@@ -722,6 +735,11 @@ public OperationsMap postProcessOperationsWithModels(final OperationsMap operati
722735
operations.put("havingAuthMethod", true);
723736
operations.getOperation().forEach(op -> op.vendorExtensions.put("havingAuthMethod", true));
724737
this.havingAuthMethods = true;
738+
739+
if (havingAuthorization) {
740+
operations.put("havingAuthorization", true);
741+
operations.getOperation().forEach(op -> op.vendorExtensions.put("havingAuthorization", true));
742+
}
725743
}
726744

727745
return operationsMap;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ description = "{{{.}}}"
1313
{{#licenseInfo}}
1414
license = "{{.}}"
1515
{{/licenseInfo}}
16-
edition = "2021"
16+
edition = "2024"
1717
{{#publishRustRegistry}}
1818
publish = ["{{.}}"]
1919
{{/publishRustRegistry}}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ pub mod {{classFilename}};
44
{{/apis}}
55
{{/apiInfo}}
66

7+
{{#havingAuthorization}}
8+
#[allow(dead_code)]
9+
#[derive(Debug, Eq, PartialEq)]
10+
pub enum Authorization {
11+
Authorized,
12+
Forbidden,
13+
}
14+
{{/havingAuthorization}}
15+
716
{{#authMethods}}
817
{{#isApiKey}}
918
{{#isKeyInCookie}}

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,73 @@ use crate::{models, types::*};
1515
{{/operations}}
1616

1717
{{#operations}}
18+
19+
{{#havingAuthorization}}
20+
/// {{classnamePascalCase}} APIs - Authorization.
21+
#[async_trait]
22+
#[allow(clippy::ptr_arg)]
23+
pub trait {{classnamePascalCase}}Authorization {
24+
type Claims;
25+
26+
{{#operation}}
27+
{{#havingAuthMethod}}
28+
{{#havingAuthorization}}
29+
{{#vendorExtensions}}
30+
/// Authorization{{#summary}} - {{{.}}}{{/summary}}.
31+
/// {{{operationId}}} - {{{httpMethod}}} {{{basePathWithoutHost}}}{{{path}}}
32+
async fn {{{x-operation-id}}}_authorize(
33+
&self,
34+
method: &Method,
35+
host: &Host,
36+
cookies: &CookieJar,
37+
claims: &Self::Claims,
38+
{{#headerParams.size}}
39+
header_params: &models::{{{operationIdCamelCase}}}HeaderParams,
40+
{{/headerParams.size}}
41+
{{#pathParams.size}}
42+
path_params: &models::{{{operationIdCamelCase}}}PathParams,
43+
{{/pathParams.size}}
44+
{{#queryParams.size}}
45+
query_params: &models::{{{operationIdCamelCase}}}QueryParams,
46+
{{/queryParams.size}}
47+
{{^x-consumes-multipart-related}}
48+
{{^x-consumes-multipart}}
49+
{{#bodyParam}}
50+
{{#vendorExtensions}}
51+
{{^x-consumes-plain-text}}
52+
body: &{{^required}}Option<{{/required}}{{{dataType}}}{{^required}}>{{/required}},
53+
{{/x-consumes-plain-text}}
54+
{{#x-consumes-plain-text}}
55+
{{#isString}}
56+
body: &str,
57+
{{/isString}}
58+
{{^isString}}
59+
body: &Bytes,
60+
{{/isString}}
61+
{{/x-consumes-plain-text}}
62+
{{/vendorExtensions}}
63+
{{/bodyParam}}
64+
{{/x-consumes-multipart}}
65+
{{/x-consumes-multipart-related}}
66+
{{#x-consumes-multipart}}
67+
body: &Multipart,
68+
{{/x-consumes-multipart}}
69+
{{#x-consumes-multipart-related}}
70+
body: &axum::body::Body,
71+
{{/x-consumes-multipart-related}}
72+
) -> Result<super::Authorization, ()> {
73+
Ok(super::Authorization::Authorized)
74+
}
75+
{{/vendorExtensions}}
76+
{{/havingAuthorization}}
77+
{{/havingAuthMethod}}
78+
{{^-last}}
79+
80+
{{/-last}}
81+
{{/operation}}
82+
}
83+
{{/havingAuthorization}}
84+
1885
/// {{classnamePascalCase}}
1986
#[async_trait]
2087
#[allow(clippy::ptr_arg)]
@@ -58,7 +125,7 @@ pub trait {{classnamePascalCase}}<E: std::fmt::Debug + Send + Sync + 'static = (
58125
{{/x-consumes-plain-text}}
59126
{{#x-consumes-plain-text}}
60127
{{#isString}}
61-
body: &String,
128+
body: &str,
62129
{{/isString}}
63130
{{^isString}}
64131
body: &Bytes,

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

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ macro_rules! ihv_generate {
3030
match hdr_value.to_str() {
3131
Ok(hdr_value) => match hdr_value.parse::<$t>() {
3232
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
33-
Err(e) => Err(format!("Unable to parse {} as a string: {}",
34-
stringify!($t), e)),
33+
Err(e) => Err(format!(r#"Unable to parse {} as a string: {e}"#,
34+
stringify!($t))),
3535
},
36-
Err(e) => Err(format!("Unable to parse header {:?} as a string - {}",
37-
hdr_value, e)),
36+
Err(e) => Err(format!(r#"Unable to parse header {hdr_value:?} as a string - {e}"#)),
3837
}
3938
}
4039
}
@@ -75,8 +74,7 @@ impl TryFrom<HeaderValue> for IntoHeaderValue<Vec<String>> {
7574
y => Some(y.to_string()),
7675
})
7776
.collect())),
78-
Err(e) => Err(format!("Unable to parse header: {:?} as a string - {}",
79-
hdr_value, e)),
77+
Err(e) => Err(format!(r#"Unable to parse header: {hdr_value:?} as a string - {e}"#)),
8078
}
8179
}
8280
}
@@ -87,8 +85,7 @@ impl TryFrom<IntoHeaderValue<Vec<String>>> for HeaderValue {
8785
fn try_from(hdr_value: IntoHeaderValue<Vec<String>>) -> Result<Self, Self::Error> {
8886
match HeaderValue::from_str(&hdr_value.0.join(", ")) {
8987
Ok(hdr_value) => Ok(hdr_value),
90-
Err(e) => Err(format!("Unable to convert {:?} into a header - {}",
91-
hdr_value, e))
88+
Err(e) => Err(format!(r#"Unable to convert {hdr_value:?} into a header - {e}"#))
9289
}
9390
}
9491
}
@@ -101,10 +98,9 @@ impl TryFrom<HeaderValue> for IntoHeaderValue<String> {
10198
fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
10299
match hdr_value.to_str() {
103100
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value.to_string())),
104-
Err(e) => Err(format!("Unable to convert header {:?} to {}",
105-
hdr_value, e)),
101+
Err(e) => Err(format!(r#"Unable to convert header {hdr_value:?} to {e}"#)),
106102
}
107-
}
103+
}
108104
}
109105

110106
impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
@@ -113,8 +109,7 @@ impl TryFrom<IntoHeaderValue<String>> for HeaderValue {
113109
fn try_from(hdr_value: IntoHeaderValue<String>) -> Result<Self, Self::Error> {
114110
match HeaderValue::from_str(&hdr_value.0) {
115111
Ok(hdr_value) => Ok(hdr_value),
116-
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
117-
hdr_value, e))
112+
Err(e) => Err(format!(r#"Unable to convert {hdr_value:?} from a header {e}"#))
118113
}
119114
}
120115
}
@@ -128,11 +123,9 @@ impl TryFrom<HeaderValue> for IntoHeaderValue<bool> {
128123
match hdr_value.to_str() {
129124
Ok(hdr_value) => match hdr_value.parse() {
130125
Ok(hdr_value) => Ok(IntoHeaderValue(hdr_value)),
131-
Err(e) => Err(format!("Unable to parse bool from {} - {}",
132-
hdr_value, e)),
126+
Err(e) => Err(format!(r#"Unable to parse bool from {hdr_value} - {e}"#)),
133127
},
134-
Err(e) => Err(format!("Unable to convert {:?} from a header {}",
135-
hdr_value, e)),
128+
Err(e) => Err(format!(r#"Unable to convert {hdr_value:?} from a header {e}"#)),
136129
}
137130
}
138131
}
@@ -143,9 +136,8 @@ impl TryFrom<IntoHeaderValue<bool>> for HeaderValue {
143136
fn try_from(hdr_value: IntoHeaderValue<bool>) -> Result<Self, Self::Error> {
144137
match HeaderValue::from_str(&hdr_value.0.to_string()) {
145138
Ok(hdr_value) => Ok(hdr_value),
146-
Err(e) => Err(format!("Unable to convert: {:?} into a header: {}",
147-
hdr_value, e))
148-
}
139+
Err(e) => Err(format!(r#"Unable to convert: {hdr_value:?} into a header: {e}"#))
140+
}
149141
}
150142
}
151143

@@ -158,11 +150,9 @@ impl TryFrom<HeaderValue> for IntoHeaderValue<DateTime<Utc>> {
158150
match hdr_value.to_str() {
159151
Ok(hdr_value) => match DateTime::parse_from_rfc3339(hdr_value) {
160152
Ok(date) => Ok(IntoHeaderValue(date.with_timezone(&Utc))),
161-
Err(e) => Err(format!("Unable to parse: {} as date - {}",
162-
hdr_value, e)),
153+
Err(e) => Err(format!(r#"Unable to parse: {hdr_value} as date - {e}"#)),
163154
},
164-
Err(e) => Err(format!("Unable to convert header {:?} to string {}",
165-
hdr_value, e)),
155+
Err(e) => Err(format!(r#"Unable to convert header {hdr_value:?} to string {e}"#)),
166156
}
167157
}
168158
}
@@ -173,8 +163,7 @@ impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
173163
fn try_from(hdr_value: IntoHeaderValue<DateTime<Utc>>) -> Result<Self, Self::Error> {
174164
match HeaderValue::from_str(hdr_value.0.to_rfc3339().as_str()) {
175165
Ok(hdr_value) => Ok(hdr_value),
176-
Err(e) => Err(format!("Unable to convert {:?} to a header: {}",
177-
hdr_value, e)),
166+
Err(e) => Err(format!(r#"Unable to convert {hdr_value:?} to a header: {e}"#)),
178167
}
179168
}
180169
}
@@ -189,12 +178,9 @@ impl TryFrom<HeaderValue> for IntoHeaderValue<uuid::Uuid> {
189178
match hdr_value.to_str() {
190179
Ok(hdr_value) => match uuid::Uuid::from_str(hdr_value) {
191180
Ok(uuid) => Ok(IntoHeaderValue(uuid)),
192-
Err(e) => Err(format!("Unable to parse: {} as uuid - {}", hdr_value, e)),
181+
Err(e) => Err(format!(r#"Unable to parse: {hdr_value} as uuid - {e}"#)),
193182
},
194-
Err(e) => Err(format!(
195-
"Unable to convert header {:?} to string {}",
196-
hdr_value, e
197-
)),
183+
Err(e) => Err(format!(r#"Unable to convert header {hdr_value:?} to string {e}"#)),
198184
}
199185
}
200186
}
@@ -205,10 +191,7 @@ impl TryFrom<IntoHeaderValue<uuid::Uuid>> for HeaderValue {
205191
fn try_from(hdr_value: IntoHeaderValue<uuid::Uuid>) -> Result<Self, Self::Error> {
206192
match HeaderValue::from_bytes(hdr_value.0.as_bytes()) {
207193
Ok(hdr_value) => Ok(hdr_value),
208-
Err(e) => Err(format!(
209-
"Unable to convert {:?} to a header: {}",
210-
hdr_value, e
211-
)),
194+
Err(e) => Err(format!(r#"Unable to convert {hdr_value:?} to a header: {e}"#)),
212195
}
213196
}
214197
}

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ use crate::{models, types::*};
207207
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
208208
pub struct {{{operationIdCamelCase}}}QueryParams {
209209
{{#queryParams}}
210-
{{#description}}
210+
{{#description}}
211211
/// {{{.}}}
212212
{{/description}}
213213
{{#isEnum}}
@@ -313,7 +313,7 @@ use crate::{models, types::*};
313313
{{/isByteArray}}
314314
{{/pattern}}
315315
{{/hasValidation}}
316-
{{/queryParams}}
316+
{{/queryParams}}
317317
{{/queryParams.size}}
318318
{{/vendorExtensions}}
319319
{{/operation}}
@@ -367,7 +367,7 @@ impl std::str::FromStr for {{{classname}}} {
367367
{{{value}}} => std::result::Result::Ok({{{classname}}}::{{{name}}}),
368368
{{/enumVars}}
369369
{{/allowableValues}}
370-
_ => std::result::Result::Err(format!("Value not valid: {}", s)),
370+
_ => std::result::Result::Err(format!(r#"Value not valid: {s}"#)),
371371
}
372372
}
373373
}
@@ -665,7 +665,7 @@ impl std::str::FromStr for {{{classname}}} {
665665
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
666666
pub struct {{{classname}}} {
667667
{{#vars}}
668-
{{#description}}
668+
{{#description}}
669669
/// {{{.}}}
670670
{{/description}}
671671
{{#isEnum}}
@@ -958,9 +958,7 @@ impl std::convert::TryFrom<header::IntoHeaderValue<{{{classname}}}>> for HeaderV
958958
let hdr_value = hdr_value.to_string();
959959
match HeaderValue::from_str(&hdr_value) {
960960
std::result::Result::Ok(value) => std::result::Result::Ok(value),
961-
std::result::Result::Err(e) => std::result::Result::Err(
962-
format!("Invalid header value for {{classname}} - value: {} is invalid {}",
963-
hdr_value, e))
961+
std::result::Result::Err(e) => std::result::Result::Err(format!(r#"Invalid header value for {{classname}} - value: {hdr_value} is invalid {e}"#))
964962
}
965963
}
966964
}
@@ -974,14 +972,10 @@ impl std::convert::TryFrom<HeaderValue> for header::IntoHeaderValue<{{{classname
974972
std::result::Result::Ok(value) => {
975973
match <{{{classname}}} as std::str::FromStr>::from_str(value) {
976974
std::result::Result::Ok(value) => std::result::Result::Ok(header::IntoHeaderValue(value)),
977-
std::result::Result::Err(err) => std::result::Result::Err(
978-
format!("Unable to convert header value '{}' into {{classname}} - {}",
979-
value, err))
975+
std::result::Result::Err(err) => std::result::Result::Err(format!(r#"Unable to convert header value '{value}' into {{classname}} - {err}"#))
980976
}
981977
},
982-
std::result::Result::Err(e) => std::result::Result::Err(
983-
format!("Unable to convert header: {:?} to string: {}",
984-
hdr_value, e))
978+
std::result::Result::Err(e) => std::result::Result::Err(format!(r#"Unable to convert header: {hdr_value:?} to string: {e}"#))
985979
}
986980
}
987981
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
{{/operations}}
1717
{{/apis}}
1818
{{/apiInfo}}
19+
20+
{{>server-utils}}

0 commit comments

Comments
 (0)