Skip to content

Commit

Permalink
[Rust] Fixed rust keyword params in reqwest-trait library (#20333)
Browse files Browse the repository at this point in the history
* Fixed rust keywords in reqwest-trait lib

* Update tests and samples
  • Loading branch information
ranger-ross authored Dec 16, 2024
1 parent ea2ba0c commit 4789124
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.templating.mustache.ReplaceAllLambda;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Mustache.Lambda;
import com.samskivert.mustache.Template;

import io.swagger.v3.oas.models.media.Discriminator;
Expand Down Expand Up @@ -692,4 +695,14 @@ public String toDefaultValue(Schema p) {
return null;
}
}

@Override
protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
return super.addMustacheLambdas()
// Convert variable names to lifetime names.
// Generally they are the same, but `#` is not valid in lifetime names.
// Rust uses `r#` prefix for variables that are also keywords.
.put("lifetimeName", new ReplaceAllLambda("^r#", "r_"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait {{{classname}}}: Send + Sync {
async fn {{{operationId}}}(&self, {{#allParams}}{{#-first}} params: {{{operationIdCamelCase}}}Params {{/-first}}{{/allParams}}) -> Result<{{#supportMultipleResponses}}ResponseContent<{{{operationIdCamelCase}}}Success>{{/supportMultipleResponses}}{{^supportMultipleResponses}}{{^returnType}}(){{/returnType}}{{{returnType}}}{{/supportMultipleResponses}}, Error<{{{operationIdCamelCase}}}Error>>;
{{/vendorExtensions.x-group-parameters}}
{{^vendorExtensions.x-group-parameters}}
async fn {{{operationId}}}<{{#allParams}}'{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}}>(&self, {{#allParams}}{{{paramName}}}: {{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{#isString}}{{#isArray}}Vec<{{/isArray}}{{^isUuid}}&'{{{paramName}}} str{{/isUuid}}{{#isArray}}>{{/isArray}}{{/isString}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}&str{{#isArray}}>{{/isArray}}{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) -> Result<{{#supportMultipleResponses}}ResponseContent<{{{operationIdCamelCase}}}Success>{{/supportMultipleResponses}}{{^supportMultipleResponses}}{{^returnType}}(){{/returnType}}{{{returnType}}}{{/supportMultipleResponses}}, Error<{{{operationIdCamelCase}}}Error>>;
async fn {{{operationId}}}<{{#allParams}}'{{#lambda.lifetimeName}}{{{paramName}}}{{/lambda.lifetimeName}}{{^-last}}, {{/-last}}{{/allParams}}>(&self, {{#allParams}}{{{paramName}}}: {{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{#isString}}{{#isArray}}Vec<{{/isArray}}{{^isUuid}}&'{{#lambda.lifetimeName}}{{{paramName}}}{{/lambda.lifetimeName}} str{{/isUuid}}{{#isArray}}>{{/isArray}}{{/isString}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}&str{{#isArray}}>{{/isArray}}{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) -> Result<{{#supportMultipleResponses}}ResponseContent<{{{operationIdCamelCase}}}Success>{{/supportMultipleResponses}}{{^supportMultipleResponses}}{{^returnType}}(){{/returnType}}{{{returnType}}}{{/supportMultipleResponses}}, Error<{{{operationIdCamelCase}}}Error>>;
{{/vendorExtensions.x-group-parameters}}
{{/operation}}
{{/operations}}
Expand Down Expand Up @@ -86,7 +86,7 @@ impl {{classname}} for {{classname}}Client {

{{/vendorExtensions.x-group-parameters}}
{{^vendorExtensions.x-group-parameters}}
async fn {{{operationId}}}<{{#allParams}}'{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}}>(&self, {{#allParams}}{{{paramName}}}: {{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{#isString}}{{#isArray}}Vec<{{/isArray}}{{^isUuid}}&'{{{paramName}}} str{{/isUuid}}{{#isArray}}>{{/isArray}}{{/isString}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}&str{{#isArray}}>{{/isArray}}{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) -> Result<{{#supportMultipleResponses}}ResponseContent<{{{operationIdCamelCase}}}Success>{{/supportMultipleResponses}}{{^supportMultipleResponses}}{{^returnType}}(){{/returnType}}{{{returnType}}}{{/supportMultipleResponses}}, Error<{{{operationIdCamelCase}}}Error>> {
async fn {{{operationId}}}<{{#allParams}}'{{#lambda.lifetimeName}}{{{paramName}}}{{/lambda.lifetimeName}}{{^-last}}, {{/-last}}{{/allParams}}>(&self, {{#allParams}}{{{paramName}}}: {{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{#isString}}{{#isArray}}Vec<{{/isArray}}{{^isUuid}}&'{{#lambda.lifetimeName}}{{{paramName}}}{{/lambda.lifetimeName}} str{{/isUuid}}{{#isArray}}>{{/isArray}}{{/isString}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}&str{{#isArray}}>{{/isArray}}{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) -> Result<{{#supportMultipleResponses}}ResponseContent<{{{operationIdCamelCase}}}Success>{{/supportMultipleResponses}}{{^supportMultipleResponses}}{{^returnType}}(){{/returnType}}{{{returnType}}}{{/supportMultipleResponses}}, Error<{{{operationIdCamelCase}}}Error>> {
{{/vendorExtensions.x-group-parameters}}
let local_var_configuration = &self.configuration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ paths:
- pending
- sold
default: available
- name: type
in: query
description: Make sure that Rust keywords like type work as query params
required: false
style: form
explode: false
deprecated: true
schema:
type: array
items:
type: string
enum:
- available
- pending
- sold
default: available
responses:
'200':
description: successful operation
Expand Down
3 changes: 2 additions & 1 deletion samples/client/petstore/rust/hyper/petstore/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Name | Type | Description | Required | Notes

## find_pets_by_status

> Vec<models::Pet> find_pets_by_status(status)
> Vec<models::Pet> find_pets_by_status(status, r#type)
Finds Pets by status

Multiple status values can be provided with comma separated strings
Expand All @@ -89,6 +89,7 @@ Multiple status values can be provided with comma separated strings
Name | Type | Description | Required | Notes
------------- | ------------- | ------------- | ------------- | -------------
**status** | [**Vec<String>**](String.md) | Status values that need to be considered for filter | [required] |
**r#type** | Option<[**Vec<String>**](String.md)> | Make sure that Rust keywords like type work as query params | |

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<C: Connect> PetApiClient<C>
pub trait PetApi: Send + Sync {
fn add_pet(&self, pet: models::Pet) -> Pin<Box<dyn Future<Output = Result<models::Pet, Error>> + Send>>;
fn delete_pet(&self, pet_id: i64, api_key: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
fn find_pets_by_status(&self, status: Vec<String>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>> + Send>>;
fn find_pets_by_status(&self, status: Vec<String>, r#type: Option<Vec<String>>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>> + Send>>;
fn find_pets_by_tags(&self, tags: Vec<String>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>> + Send>>;
fn get_pet_by_id(&self, pet_id: i64) -> Pin<Box<dyn Future<Output = Result<models::Pet, Error>> + Send>>;
fn update_pet(&self, pet: models::Pet) -> Pin<Box<dyn Future<Output = Result<models::Pet, Error>> + Send>>;
Expand Down Expand Up @@ -74,11 +74,15 @@ impl<C: Connect>PetApi for PetApiClient<C>
}

#[allow(unused_mut)]
fn find_pets_by_status(&self, status: Vec<String>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>> + Send>> {
fn find_pets_by_status(&self, status: Vec<String>, r#type: Option<Vec<String>>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>> + Send>> {
let mut req = __internal_request::Request::new(hyper::Method::GET, "/pet/findByStatus".to_string())
.with_auth(__internal_request::Auth::Oauth)
;
req = req.with_query_param("status".to_string(), status.join(",").to_string());
if let Some(ref s) = r#type {
let query_value = s.iter().map(|s| s.to_string()).collect::<Vec<String>>().join(",");
req = req.with_query_param("type".to_string(), query_value);
}

req.execute(self.configuration.borrow())
}
Expand Down
3 changes: 2 additions & 1 deletion samples/client/petstore/rust/hyper0x/petstore/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Name | Type | Description | Required | Notes

## find_pets_by_status

> Vec<models::Pet> find_pets_by_status(status)
> Vec<models::Pet> find_pets_by_status(status, r#type)
Finds Pets by status

Multiple status values can be provided with comma separated strings
Expand All @@ -89,6 +89,7 @@ Multiple status values can be provided with comma separated strings
Name | Type | Description | Required | Notes
------------- | ------------- | ------------- | ------------- | -------------
**status** | [**Vec<String>**](String.md) | Status values that need to be considered for filter | [required] |
**r#type** | Option<[**Vec<String>**](String.md)> | Make sure that Rust keywords like type work as query params | |

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<C: hyper::client::connect::Connect> PetApiClient<C>
pub trait PetApi {
fn add_pet(&self, pet: models::Pet) -> Pin<Box<dyn Future<Output = Result<models::Pet, Error>>>>;
fn delete_pet(&self, pet_id: i64, api_key: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>>>>;
fn find_pets_by_status(&self, status: Vec<String>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>>>>;
fn find_pets_by_status(&self, status: Vec<String>, r#type: Option<Vec<String>>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>>>>;
fn find_pets_by_tags(&self, tags: Vec<String>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>>>>;
fn get_pet_by_id(&self, pet_id: i64) -> Pin<Box<dyn Future<Output = Result<models::Pet, Error>>>>;
fn update_pet(&self, pet: models::Pet) -> Pin<Box<dyn Future<Output = Result<models::Pet, Error>>>>;
Expand Down Expand Up @@ -73,11 +73,15 @@ impl<C: hyper::client::connect::Connect>PetApi for PetApiClient<C>
}

#[allow(unused_mut)]
fn find_pets_by_status(&self, status: Vec<String>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>>>> {
fn find_pets_by_status(&self, status: Vec<String>, r#type: Option<Vec<String>>) -> Pin<Box<dyn Future<Output = Result<Vec<models::Pet>, Error>>>> {
let mut req = __internal_request::Request::new(hyper::Method::GET, "/pet/findByStatus".to_string())
.with_auth(__internal_request::Auth::Oauth)
;
req = req.with_query_param("status".to_string(), status.join(",").to_string());
if let Some(ref s) = r#type {
let query_value = s.iter().map(|s| s.to_string()).collect::<Vec<String>>().join(",");
req = req.with_query_param("type".to_string(), query_value);
}

req.execute(self.configuration.borrow())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Name | Type | Description | Required | Notes

## find_pets_by_status

> Vec<models::Pet> find_pets_by_status(status)
> Vec<models::Pet> find_pets_by_status(status, r#type)
Finds Pets by status

Multiple status values can be provided with comma separated strings
Expand All @@ -89,6 +89,7 @@ Multiple status values can be provided with comma separated strings
Name | Type | Description | Required | Notes
------------- | ------------- | ------------- | ------------- | -------------
**status** | [**Vec<String>**](String.md) | Status values that need to be considered for filter | [required] |
**r#type** | Option<[**Vec<String>**](String.md)> | Make sure that Rust keywords like type work as query params | |

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use super::{Error, configuration};
pub trait PetApi: Send + Sync {
async fn add_pet<'pet>(&self, pet: models::Pet) -> Result<models::Pet, Error<AddPetError>>;
async fn delete_pet<'pet_id, 'api_key>(&self, pet_id: i64, api_key: Option<&'api_key str>) -> Result<(), Error<DeletePetError>>;
async fn find_pets_by_status<'status>(&self, status: Vec<String>) -> Result<Vec<models::Pet>, Error<FindPetsByStatusError>>;
async fn find_pets_by_status<'status, 'r_type>(&self, status: Vec<String>, r#type: Option<Vec<String>>) -> Result<Vec<models::Pet>, Error<FindPetsByStatusError>>;
async fn find_pets_by_tags<'tags>(&self, tags: Vec<String>) -> Result<Vec<models::Pet>, Error<FindPetsByTagsError>>;
async fn get_pet_by_id<'pet_id>(&self, pet_id: i64) -> Result<models::Pet, Error<GetPetByIdError>>;
async fn update_pet<'pet>(&self, pet: models::Pet) -> Result<models::Pet, Error<UpdatePetError>>;
Expand Down Expand Up @@ -112,7 +112,7 @@ impl PetApi for PetApiClient {
}

/// Multiple status values can be provided with comma separated strings
async fn find_pets_by_status<'status>(&self, status: Vec<String>) -> Result<Vec<models::Pet>, Error<FindPetsByStatusError>> {
async fn find_pets_by_status<'status, 'r_type>(&self, status: Vec<String>, r#type: Option<Vec<String>>) -> Result<Vec<models::Pet>, Error<FindPetsByStatusError>> {
let local_var_configuration = &self.configuration;

let local_var_client = &local_var_configuration.client;
Expand All @@ -124,6 +124,12 @@ impl PetApi for PetApiClient {
"multi" => local_var_req_builder.query(&status.into_iter().map(|p| ("status".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => local_var_req_builder.query(&[("status", &status.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
if let Some(ref local_var_str) = r#type {
local_var_req_builder = match "csv" {
"multi" => local_var_req_builder.query(&local_var_str.into_iter().map(|p| ("type".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => local_var_req_builder.query(&[("type", &local_var_str.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Name | Type | Description | Required | Notes

## find_pets_by_status

> Vec<models::Pet> find_pets_by_status(status)
> Vec<models::Pet> find_pets_by_status(status, r#type)
Finds Pets by status

Multiple status values can be provided with comma separated strings
Expand All @@ -89,6 +89,7 @@ Multiple status values can be provided with comma separated strings
Name | Type | Description | Required | Notes
------------- | ------------- | ------------- | ------------- | -------------
**status** | [**Vec<String>**](String.md) | Status values that need to be considered for filter | [required] |
**r#type** | Option<[**Vec<String>**](String.md)> | Make sure that Rust keywords like type work as query params | |

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ pub struct DeletePetParams {
#[derive(Clone, Debug)]
pub struct FindPetsByStatusParams {
/// Status values that need to be considered for filter
pub status: Vec<String>
pub status: Vec<String>,
/// Make sure that Rust keywords like type work as query params
pub r#type: Option<Vec<String>>
}

/// struct for passing parameters to the method [`find_pets_by_tags`]
Expand Down Expand Up @@ -296,6 +298,7 @@ pub async fn find_pets_by_status(configuration: &configuration::Configuration, p

// unbox the parameters
let status = params.status;
let r#type = params.r#type;


let local_var_client = &local_var_configuration.client;
Expand All @@ -307,6 +310,12 @@ pub async fn find_pets_by_status(configuration: &configuration::Configuration, p
"multi" => local_var_req_builder.query(&status.into_iter().map(|p| ("status".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => local_var_req_builder.query(&[("status", &status.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
if let Some(ref local_var_str) = r#type {
local_var_req_builder = match "csv" {
"multi" => local_var_req_builder.query(&local_var_str.into_iter().map(|p| ("type".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => local_var_req_builder.query(&[("type", &local_var_str.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Name | Type | Description | Required | Notes

## find_pets_by_status

> Vec<models::Pet> find_pets_by_status(status)
> Vec<models::Pet> find_pets_by_status(status, r#type)
Finds Pets by status

Multiple status values can be provided with comma separated strings
Expand All @@ -89,6 +89,7 @@ Multiple status values can be provided with comma separated strings
Name | Type | Description | Required | Notes
------------- | ------------- | ------------- | ------------- | -------------
**status** | [**Vec<String>**](String.md) | Status values that need to be considered for filter | [required] |
**r#type** | Option<[**Vec<String>**](String.md)> | Make sure that Rust keywords like type work as query params | |

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ pub struct DeletePetParams {
#[derive(Clone, Debug)]
pub struct FindPetsByStatusParams {
/// Status values that need to be considered for filter
pub status: Vec<String>
pub status: Vec<String>,
/// Make sure that Rust keywords like type work as query params
pub r#type: Option<Vec<String>>
}

/// struct for passing parameters to the method [`find_pets_by_tags`]
Expand Down Expand Up @@ -300,6 +302,7 @@ pub async fn find_pets_by_status(configuration: &configuration::Configuration, p

// unbox the parameters
let status = params.status;
let r#type = params.r#type;


let local_var_client = &local_var_configuration.client;
Expand All @@ -311,6 +314,12 @@ pub async fn find_pets_by_status(configuration: &configuration::Configuration, p
"multi" => local_var_req_builder.query(&status.into_iter().map(|p| ("status".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => local_var_req_builder.query(&[("status", &status.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
if let Some(ref local_var_str) = r#type {
local_var_req_builder = match "csv" {
"multi" => local_var_req_builder.query(&local_var_str.into_iter().map(|p| ("type".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => local_var_req_builder.query(&[("type", &local_var_str.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
Expand Down
Loading

0 comments on commit 4789124

Please sign in to comment.