diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java index ebb9536e34c..9ebb1df10f2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java @@ -750,10 +750,21 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig { } if (op.authMethods != null) { + boolean headerAuthMethods = false; + for (CodegenSecurity s : op.authMethods) { if (s.isApiKey && s.isKeyInHeader) { s.vendorExtensions.put("x-apiKeyName", toModelName(s.keyParamName)); + headerAuthMethods = true; } + + if (s.isBasicBasic || s.isBasicBearer || s.isOAuth) { + headerAuthMethods = true; + } + } + + if (headerAuthMethods) { + op.vendorExtensions.put("hasHeaderAuthMethods", "true"); } } } diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-mod.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-mod.mustache index c15b9e32ddb..b24727a7d93 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/client-mod.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/client-mod.mustache @@ -417,29 +417,53 @@ impl Api for Client where {{/vendorExtensions}} request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); -{{#authMethods}} - {{#isBasic}} - if let Some(auth_data) = (context as &Has>).get().as_ref() { - if let AuthData::Basic(ref basic_header) = *auth_data { - request.headers_mut().set(hyper::header::Authorization( - basic_header.clone(), - )) +{{#vendorExtensions.hasHeaderAuthMethods}} + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + {{#authMethods}} + {{#isApiKey}} + {{#isKeyInHeader}} + &AuthData::ApiKey(ref api_key) => { + header! { ({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}, "{{keyParamName}}") => [String] } + request.headers_mut().set( + {{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}(api_key.to_string()) + ) + }, + {{/isKeyInHeader}} + {{/isApiKey}} + {{#isBasicBasic}} + &AuthData::Basic(ref basic_header) => { + request.headers_mut().set(hyper::header::Authorization( + basic_header.clone(), + )) + }, + {{/isBasicBasic}} + {{#isBasicBearer}} + &AuthData::Bearer(ref bearer_header) => { + request.headers_mut().set(hyper::header::Authorization( + bearer_header.clone(), + )) + }, + {{/isBasicBearer}} + {{#isOAuth}} + {{^isBasicBearer}} + &AuthData::Bearer(ref bearer_header) => { + request.headers_mut().set(hyper::header::Authorization( + bearer_header.clone(), + )) + }, + {{/isBasicBearer}} + {{/isOAuth}} + {{/authMethods}} + _ => {} } - } - {{/isBasic}} - {{#isApiKey}} - {{#isKeyInHeader}} - header! { ({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}, "{{keyParamName}}") => [String] } - if let Some(auth_data) = (context as &Has>).get().as_ref() { - if let AuthData::ApiKey(ref api_key) = *auth_data { - request.headers_mut().set({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}(api_key.to_string())); - } - } - {{/isKeyInHeader}} - {{/isApiKey}} -{{/authMethods}} + }); +{{/vendorExtensions.hasHeaderAuthMethods}} {{#headerParams}} {{#-first}} + // Header parameters {{/-first}}{{^isMapContainer}} header! { (Request{{vendorExtensions.typeName}}, "{{{baseName}}}") => {{#isListContainer}}({{{baseType}}})*{{/isListContainer}}{{^isListContainer}}[{{{dataType}}}]{{/isListContainer}} } {{#required}} request.headers_mut().set(Request{{vendorExtensions.typeName}}(param_{{{paramName}}}{{#isListContainer}}.clone(){{/isListContainer}})); diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs index 14b71f70b52..d7a48cd205a 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs @@ -879,13 +879,18 @@ impl Api for Client where request.set_body(body.into_bytes()); request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); - if let Some(auth_data) = (context as &Has>).get().as_ref() { - if let AuthData::Basic(ref basic_header) = *auth_data { - request.headers_mut().set(hyper::header::Authorization( - basic_header.clone(), - )) + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::Basic(ref basic_header) => { + request.headers_mut().set(hyper::header::Authorization( + basic_header.clone(), + )) + }, + _ => {} } - } + }); Box::new(self.client_service.call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| { @@ -975,6 +980,7 @@ impl Api for Client where request.set_body(body.into_bytes()); request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); + // Header parameters header! { (RequestEnumHeaderStringArray, "enum_header_string_array") => (String)* } param_enum_header_string_array.map(|header| request.headers_mut().set(RequestEnumHeaderStringArray(header.clone()))); @@ -1272,6 +1278,18 @@ impl Api for Client where request.headers_mut().set(ContentType(mimetypes::requests::ADD_PET.clone())); request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::Bearer(ref bearer_header) => { + request.headers_mut().set(hyper::header::Authorization( + bearer_header.clone(), + )) + }, + _ => {} + } + }); Box::new(self.client_service.call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| { @@ -1333,6 +1351,19 @@ impl Api for Client where request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::Bearer(ref bearer_header) => { + request.headers_mut().set(hyper::header::Authorization( + bearer_header.clone(), + )) + }, + _ => {} + } + }); + // Header parameters header! { (RequestApiKey, "api_key") => [String] } param_api_key.map(|header| request.headers_mut().set(RequestApiKey(header))); @@ -1398,6 +1429,18 @@ impl Api for Client where request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::Bearer(ref bearer_header) => { + request.headers_mut().set(hyper::header::Authorization( + bearer_header.clone(), + )) + }, + _ => {} + } + }); Box::new(self.client_service.call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| { @@ -1483,6 +1526,18 @@ impl Api for Client where request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::Bearer(ref bearer_header) => { + request.headers_mut().set(hyper::header::Authorization( + bearer_header.clone(), + )) + }, + _ => {} + } + }); Box::new(self.client_service.call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| { @@ -1567,12 +1622,19 @@ impl Api for Client where request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); - header! { (ApiKey, "api_key") => [String] } - if let Some(auth_data) = (context as &Has>).get().as_ref() { - if let AuthData::ApiKey(ref api_key) = *auth_data { - request.headers_mut().set(ApiKey(api_key.to_string())); + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::ApiKey(ref api_key) => { + header! { (ApiKey, "api_key") => [String] } + request.headers_mut().set( + ApiKey(api_key.to_string()) + ) + }, + _ => {} } - } + }); Box::new(self.client_service.call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| { @@ -1670,6 +1732,18 @@ impl Api for Client where request.headers_mut().set(ContentType(mimetypes::requests::UPDATE_PET.clone())); request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::Bearer(ref bearer_header) => { + request.headers_mut().set(hyper::header::Authorization( + bearer_header.clone(), + )) + }, + _ => {} + } + }); Box::new(self.client_service.call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| { @@ -1757,6 +1831,18 @@ impl Api for Client where request.set_body(body.into_bytes()); request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::Bearer(ref bearer_header) => { + request.headers_mut().set(hyper::header::Authorization( + bearer_header.clone(), + )) + }, + _ => {} + } + }); Box::new(self.client_service.call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| { @@ -1867,6 +1953,18 @@ impl Api for Client where request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::Bearer(ref bearer_header) => { + request.headers_mut().set(hyper::header::Authorization( + bearer_header.clone(), + )) + }, + _ => {} + } + }); Box::new(self.client_service.call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| { @@ -2011,12 +2109,19 @@ impl Api for Client where request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); - header! { (ApiKey, "api_key") => [String] } - if let Some(auth_data) = (context as &Has>).get().as_ref() { - if let AuthData::ApiKey(ref api_key) = *auth_data { - request.headers_mut().set(ApiKey(api_key.to_string())); + + (context as &Has>).get().as_ref().map(|auth_data| { + // Currently only authentication with Basic, API Key, and Bearer are supported + match auth_data { + &AuthData::ApiKey(ref api_key) => { + header! { (ApiKey, "api_key") => [String] } + request.headers_mut().set( + ApiKey(api_key.to_string()) + ) + }, + _ => {} } - } + }); Box::new(self.client_service.call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| {