Skip to content

[Rust] fix: add location prefix to prevent parameter name collisions #21611

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,29 @@ public void postProcessParameter(CodegenParameter parameter) {
super.postProcessParameter(parameter);
// in order to avoid name conflicts, we map parameters inside the functions
String inFunctionIdentifier = "";
String locationSuffix = "";

// Determine parameter location using the boolean flags in case of parameters with the same name but in different locations
if (parameter.isPathParam) {
locationSuffix = "path_";
} else if (parameter.isQueryParam) {
locationSuffix = "query_";
} else if (parameter.isHeaderParam) {
locationSuffix = "header_";
} else if (parameter.isBodyParam) {
locationSuffix = "body_";
} else if (parameter.isCookieParam) {
locationSuffix = "cookie_";
} else if (parameter.isFormParam) {
locationSuffix = "form_";
}
if (this.useSingleRequestParameter) {
inFunctionIdentifier = "params." + parameter.paramName;
inFunctionIdentifier = "params." + locationSuffix + parameter.paramName;
} else {
if (parameter.paramName.startsWith("r#")) {
inFunctionIdentifier = "p_" + parameter.paramName.substring(2);
inFunctionIdentifier = "p_" + locationSuffix + parameter.paramName.substring(2);
} else {
inFunctionIdentifier = "p_" + parameter.paramName;
inFunctionIdentifier = "p_" + locationSuffix + parameter.paramName;
}
}
if (!parameter.vendorExtensions.containsKey(this.VENDOR_EXTENSION_PARAM_IDENTIFIER)) { // allow to overwrite this value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub enum DemoColorGetError {

pub async fn demo_color_get(configuration: &configuration::Configuration, color: models::Color) -> Result<(), Error<DemoColorGetError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_color = color;
let p_path_color = color;

let uri_str = format!("{}/demo/{color}", configuration.base_path, color=p_color.to_string());
let uri_str = format!("{}/demo/{color}", configuration.base_path, color=p_path_color.to_string());
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ pub enum GetStateError {

pub fn create_state(configuration: &configuration::Configuration, create_state_request: models::CreateStateRequest) -> Result<(), Error<CreateStateError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_create_state_request = create_state_request;
let p_body_create_state_request = create_state_request;

let uri_str = format!("{}/state", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
req_builder = req_builder.json(&p_create_state_request);
req_builder = req_builder.json(&p_body_create_state_request);

let req = req_builder.build()?;
let resp = configuration.client.execute(req)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ pub fn root_get(configuration: &configuration::Configuration, ) -> Result<models

pub fn test(configuration: &configuration::Configuration, body: Option<serde_json::Value>) -> Result<(), Error<TestError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_body = body;
let p_body_body = body;

let uri_str = format!("{}/", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
req_builder = req_builder.json(&p_body);
req_builder = req_builder.json(&p_body_body);

let req = req_builder.build()?;
let resp = configuration.client.execute(req)?;
Expand Down
4 changes: 2 additions & 2 deletions samples/client/others/rust/reqwest/oneOf/src/apis/bar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ pub enum CreateBarError {

pub fn create_bar(configuration: &configuration::Configuration, bar_create: models::BarCreate) -> Result<models::Bar, Error<CreateBarError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_bar_create = bar_create;
let p_body_bar_create = bar_create;

let uri_str = format!("{}/bar", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
req_builder = req_builder.json(&p_bar_create);
req_builder = req_builder.json(&p_body_bar_create);

let req = req_builder.build()?;
let resp = configuration.client.execute(req)?;
Expand Down
4 changes: 2 additions & 2 deletions samples/client/others/rust/reqwest/oneOf/src/apis/foo_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ pub enum GetAllFoosError {

pub fn create_foo(configuration: &configuration::Configuration, foo: Option<models::Foo>) -> Result<models::FooRefOrValue, Error<CreateFooError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_foo = foo;
let p_body_foo = foo;

let uri_str = format!("{}/foo", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
req_builder = req_builder.json(&p_foo);
req_builder = req_builder.json(&p_body_foo);

let req = req_builder.build()?;
let resp = configuration.client.execute(req)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ pub enum GetParameterNameMappingError {

pub fn get_parameter_name_mapping(configuration: &configuration::Configuration, underscore_type: i64, r#type: &str, type_with_underscore: &str, dash_type: &str, http_debug_option: &str) -> Result<(), Error<GetParameterNameMappingError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_underscore_type = underscore_type;
let p_type = r#type;
let p_type_with_underscore = type_with_underscore;
let p_dash_type = dash_type;
let p_http_debug_option = http_debug_option;
let p_header_underscore_type = underscore_type;
let p_query_type = r#type;
let p_header_type_with_underscore = type_with_underscore;
let p_header_dash_type = dash_type;
let p_query_http_debug_option = http_debug_option;

let uri_str = format!("{}/fake/parameter-name-mapping", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

req_builder = req_builder.query(&[("type", &p_type.to_string())]);
req_builder = req_builder.query(&[("http_debug_option", &p_http_debug_option.to_string())]);
req_builder = req_builder.query(&[("type", &p_query_type.to_string())]);
req_builder = req_builder.query(&[("http_debug_option", &p_query_http_debug_option.to_string())]);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
req_builder = req_builder.header("_type", p_underscore_type.to_string());
req_builder = req_builder.header("type_", p_type_with_underscore.to_string());
req_builder = req_builder.header("-type", p_dash_type.to_string());
req_builder = req_builder.header("_type", p_header_underscore_type.to_string());
req_builder = req_builder.header("type_", p_header_type_with_underscore.to_string());
req_builder = req_builder.header("-type", p_header_dash_type.to_string());

let req = req_builder.build()?;
let resp = configuration.client.execute(req)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ pub enum TestNullableRequiredParamError {
///
pub async fn test_nullable_required_param(configuration: &configuration::Configuration, params: TestNullableRequiredParamParams) -> Result<ResponseContent<TestNullableRequiredParamSuccess>, Error<TestNullableRequiredParamError>> {

let uri_str = format!("{}/fake/user/{user_name}", configuration.base_path, user_name=crate::apis::urlencode(params.user_name));
let uri_str = format!("{}/fake/user/{user_name}", configuration.base_path, user_name=crate::apis::urlencode(params.path_user_name));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

if let Some(ref param_value) = params.content {
if let Some(ref param_value) = params.query_content {
req_builder = req_builder.query(&[("content", &param_value.to_string())]);
}
req_builder = req_builder.query(&[("anyType", &params.any_type.to_string())]);
req_builder = req_builder.query(&[("anyType", &params.query_any_type.to_string())]);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
match params.dummy_required_nullable_param {
match params.header_dummy_required_nullable_param {
Some(param_value) => { req_builder = req_builder.header("dummy_required_nullable_param", param_value.to_string()); },
None => { req_builder = req_builder.header("dummy_required_nullable_param", ""); },
}
if let Some(param_value) = params.uppercase {
if let Some(param_value) = params.header_uppercase {
req_builder = req_builder.header("UPPERCASE", param_value.to_string());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub async fn add_pet(configuration: &configuration::Configuration, params: AddPe
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&params.pet);
req_builder = req_builder.json(&params.body_pet);

let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
Expand All @@ -290,13 +290,13 @@ pub async fn add_pet(configuration: &configuration::Configuration, params: AddPe
///
pub async fn delete_pet(configuration: &configuration::Configuration, params: DeletePetParams) -> Result<ResponseContent<DeletePetSuccess>, Error<DeletePetError>> {

let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=params.pet_id);
let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=params.path_pet_id);
let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(param_value) = params.api_key {
if let Some(param_value) = params.header_api_key {
req_builder = req_builder.header("api_key", param_value.to_string());
}
if let Some(ref token) = configuration.oauth_access_token {
Expand Down Expand Up @@ -326,10 +326,10 @@ pub async fn find_pets_by_status(configuration: &configuration::Configuration, p
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

req_builder = match "csv" {
"multi" => req_builder.query(&params.status.into_iter().map(|p| ("status".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("status", &params.status.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
"multi" => req_builder.query(&params.query_status.into_iter().map(|p| ("status".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("status", &params.query_status.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
if let Some(ref param_value) = params.r#type {
if let Some(ref param_value) = params.query_r#type {
req_builder = match "csv" {
"multi" => req_builder.query(&param_value.into_iter().map(|p| ("type".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("type", &param_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
Expand Down Expand Up @@ -365,8 +365,8 @@ pub async fn find_pets_by_tags(configuration: &configuration::Configuration, par
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

req_builder = match "csv" {
"multi" => req_builder.query(&params.tags.into_iter().map(|p| ("tags".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("tags", &params.tags.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
"multi" => req_builder.query(&params.query_tags.into_iter().map(|p| ("tags".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("tags", &params.query_tags.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
Expand Down Expand Up @@ -394,7 +394,7 @@ pub async fn find_pets_by_tags(configuration: &configuration::Configuration, par
/// Returns a single pet
pub async fn get_pet_by_id(configuration: &configuration::Configuration, params: GetPetByIdParams) -> Result<ResponseContent<GetPetByIdSuccess>, Error<GetPetByIdError>> {

let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=params.pet_id);
let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=params.path_pet_id);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
Expand Down Expand Up @@ -431,7 +431,7 @@ pub async fn pets_explode_post(configuration: &configuration::Configuration, par
let uri_str = format!("{}/pets/explode", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

if let Some(ref param_value) = params.page_explode {
if let Some(ref param_value) = params.query_page_explode {
req_builder = req_builder.query(&param_value);
}
if let Some(ref user_agent) = configuration.user_agent {
Expand Down Expand Up @@ -460,7 +460,7 @@ pub async fn pets_post(configuration: &configuration::Configuration, params: Pet
let uri_str = format!("{}/pets", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

if let Some(ref param_value) = params.page {
if let Some(ref param_value) = params.query_page {
req_builder = req_builder.query(&[("page", &serde_json::to_string(param_value)?)]);
}
if let Some(ref user_agent) = configuration.user_agent {
Expand Down Expand Up @@ -495,7 +495,7 @@ pub async fn update_pet(configuration: &configuration::Configuration, params: Up
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&params.pet);
req_builder = req_builder.json(&params.body_pet);

let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
Expand All @@ -516,7 +516,7 @@ pub async fn update_pet(configuration: &configuration::Configuration, params: Up
///
pub async fn update_pet_with_form(configuration: &configuration::Configuration, params: UpdatePetWithFormParams) -> Result<ResponseContent<UpdatePetWithFormSuccess>, Error<UpdatePetWithFormError>> {

let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=params.pet_id);
let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=params.path_pet_id);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
Expand All @@ -526,10 +526,10 @@ pub async fn update_pet_with_form(configuration: &configuration::Configuration,
req_builder = req_builder.bearer_auth(token.to_owned());
};
let mut multipart_form_params = std::collections::HashMap::new();
if let Some(param_value) = params.name {
if let Some(param_value) = params.form_name {
multipart_form_params.insert("name", param_value.to_string());
}
if let Some(param_value) = params.status {
if let Some(param_value) = params.form_status {
multipart_form_params.insert("status", param_value.to_string());
}
req_builder = req_builder.form(&multipart_form_params);
Expand All @@ -553,7 +553,7 @@ pub async fn update_pet_with_form(configuration: &configuration::Configuration,
///
pub async fn upload_file(configuration: &configuration::Configuration, params: UploadFileParams) -> Result<ResponseContent<UploadFileSuccess>, Error<UploadFileError>> {

let uri_str = format!("{}/pet/{petId}/uploadImage", configuration.base_path, petId=params.pet_id);
let uri_str = format!("{}/pet/{petId}/uploadImage", configuration.base_path, petId=params.path_pet_id);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
Expand All @@ -563,7 +563,7 @@ pub async fn upload_file(configuration: &configuration::Configuration, params: U
req_builder = req_builder.bearer_auth(token.to_owned());
};
let mut multipart_form = reqwest::multipart::Form::new();
if let Some(param_value) = params.additional_metadata {
if let Some(param_value) = params.form_additional_metadata {
multipart_form = multipart_form.text("additionalMetadata", param_value.to_string());
}
// TODO: support file upload for 'file' parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub enum PlaceOrderError {
/// For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
pub async fn delete_order(configuration: &configuration::Configuration, params: DeleteOrderParams) -> Result<ResponseContent<DeleteOrderSuccess>, Error<DeleteOrderError>> {

let uri_str = format!("{}/store/order/{orderId}", configuration.base_path, orderId=crate::apis::urlencode(params.order_id));
let uri_str = format!("{}/store/order/{orderId}", configuration.base_path, orderId=crate::apis::urlencode(params.path_order_id));
let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
Expand Down Expand Up @@ -164,7 +164,7 @@ pub async fn get_inventory(configuration: &configuration::Configuration) -> Resu
/// For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions
pub async fn get_order_by_id(configuration: &configuration::Configuration, params: GetOrderByIdParams) -> Result<ResponseContent<GetOrderByIdSuccess>, Error<GetOrderByIdError>> {

let uri_str = format!("{}/store/order/{orderId}", configuration.base_path, orderId=params.order_id);
let uri_str = format!("{}/store/order/{orderId}", configuration.base_path, orderId=params.path_order_id);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

if let Some(ref user_agent) = configuration.user_agent {
Expand Down Expand Up @@ -196,7 +196,7 @@ pub async fn place_order(configuration: &configuration::Configuration, params: P
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
req_builder = req_builder.json(&params.order);
req_builder = req_builder.json(&params.body_order);

let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
Expand Down
Loading