Skip to content

Commit

Permalink
[Rust] Support formParams and fix list-params. (OpenAPITools#1678)
Browse files Browse the repository at this point in the history
* [Rust] Support formParams and fix list-params.

Form params were previously unsupported, and list-params would produce code that didn't compile.

* update rust samples
  • Loading branch information
mfarrugi authored and wing328 committed Dec 21, 2018
1 parent 10ecc0e commit 4a494b4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl {{{classname}}}Client {
pub trait {{{classname}}} {
{{#operations}}
{{#operation}}
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Result<{{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error>;
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{#isListContainer}}Vec<{{/isListContainer}}{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{#isListContainer}}>{{/isListContainer}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Result<{{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error>;
{{/operation}}
{{/operations}}
}
Expand All @@ -30,16 +30,23 @@ pub trait {{{classname}}} {
impl {{{classname}}} for {{{classname}}}Client {
{{#operations}}
{{#operation}}
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Result<{{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error> {
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{#isListContainer}}Vec<{{/isListContainer}}{{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{#isListContainer}}>{{/isListContainer}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Result<{{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;
{{#hasFormParams}}
let form = [
{{#formParams}}
("{{{baseName}}}", &{{{paramName}}}{{#isListContainer}}.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(","){{/isListContainer}}.to_string()),
{{/formParams}}
];
{{/hasFormParams}}
let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());
{{#queryParams}}
{{#queryParams}}
query.append_pair("{{{baseName}}}", &{{{paramName}}}{{#isListContainer}}.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(","){{/isListContainer}}.to_string());
{{/queryParams}}
{{#hasAuthMethods}}{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}}
{{/queryParams}}
{{#hasAuthMethods}}{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let val = match apikey.prefix {
Expand All @@ -48,7 +55,7 @@ impl {{{classname}}} for {{{classname}}}Client {
};
query.append_pair("{{{keyParamName}}}".to_owned(), val);
}
{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}{{/hasAuthMethods}}
{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}{{/hasAuthMethods}}
query.finish()
};
let uri_str = format!("{}{{{path}}}?{}", configuration.base_path, query_string{{#pathParams}}, {{{baseName}}}={{{paramName}}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{/pathParams}});
Expand Down Expand Up @@ -96,6 +103,10 @@ impl {{{classname}}} for {{{classname}}}Client {
{{/bodyParams}}
{{/hasBodyParam}}

{{#hasFormParams}}
req_builder = req_builder.form(&form);
{{/hasFormParams}}

// send request
let req = req_builder.build()?;

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.4-SNAPSHOT
4.0.0-SNAPSHOT
42 changes: 30 additions & 12 deletions samples/client/petstore/rust-reqwest/src/apis/pet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl PetApiClient {
pub trait PetApi {
fn add_pet(&self, pet: ::models::Pet) -> Result<(), Error>;
fn delete_pet(&self, pet_id: i64, api_key: &str) -> Result<(), Error>;
fn find_pets_by_status(&self, status: Vec<String>) -> Result<Vec<::models::Pet>, Error>;
fn find_pets_by_tags(&self, tags: Vec<String>) -> Result<Vec<::models::Pet>, Error>;
fn find_pets_by_status(&self, status: Vec<Vec<String>>) -> Result<Vec<::models::Pet>, Error>;
fn find_pets_by_tags(&self, tags: Vec<Vec<String>>) -> Result<Vec<::models::Pet>, Error>;
fn get_pet_by_id(&self, pet_id: i64) -> Result<::models::Pet, Error>;
fn update_pet(&self, pet: ::models::Pet) -> Result<(), Error>;
fn update_pet_with_form(&self, pet_id: i64, name: &str, status: &str) -> Result<(), Error>;
Expand All @@ -46,7 +46,7 @@ impl PetApi for PetApiClient {

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/pet?{}", configuration.base_path, query_string);
Expand All @@ -65,6 +65,7 @@ impl PetApi for PetApiClient {

req_builder = req_builder.json(&pet);


// send request
let req = req_builder.build()?;

Expand All @@ -78,7 +79,7 @@ impl PetApi for PetApiClient {

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/pet/{petId}?{}", configuration.base_path, query_string, petId=pet_id);
Expand All @@ -97,21 +98,22 @@ impl PetApi for PetApiClient {
};



// send request
let req = req_builder.build()?;

client.execute(req)?.error_for_status()?;
Ok(())
}

fn find_pets_by_status(&self, status: Vec<String>) -> Result<Vec<::models::Pet>, Error> {
fn find_pets_by_status(&self, status: Vec<Vec<String>>) -> Result<Vec<::models::Pet>, Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());
query.append_pair("status", &status.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string());

query.finish()
};
let uri_str = format!("{}/pet/findByStatus?{}", configuration.base_path, query_string);
Expand All @@ -129,20 +131,21 @@ impl PetApi for PetApiClient {
};



// send request
let req = req_builder.build()?;

Ok(client.execute(req)?.error_for_status()?.json()?)
}

fn find_pets_by_tags(&self, tags: Vec<String>) -> Result<Vec<::models::Pet>, Error> {
fn find_pets_by_tags(&self, tags: Vec<Vec<String>>) -> Result<Vec<::models::Pet>, Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());
query.append_pair("tags", &tags.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string());

query.finish()
};
let uri_str = format!("{}/pet/findByTags?{}", configuration.base_path, query_string);
Expand All @@ -160,6 +163,7 @@ impl PetApi for PetApiClient {
};



// send request
let req = req_builder.build()?;

Expand All @@ -172,7 +176,7 @@ impl PetApi for PetApiClient {

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/pet/{petId}?{}", configuration.base_path, query_string, petId=pet_id);
Expand All @@ -196,6 +200,7 @@ impl PetApi for PetApiClient {




// send request
let req = req_builder.build()?;

Expand All @@ -208,7 +213,7 @@ impl PetApi for PetApiClient {

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/pet?{}", configuration.base_path, query_string);
Expand All @@ -227,6 +232,7 @@ impl PetApi for PetApiClient {

req_builder = req_builder.json(&pet);


// send request
let req = req_builder.build()?;

Expand All @@ -238,9 +244,13 @@ impl PetApi for PetApiClient {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

let form = [
("name", &name.to_string()),
("status", &status.to_string()),
];
let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/pet/{petId}?{}", configuration.base_path, query_string, petId=pet_id);
Expand All @@ -258,6 +268,8 @@ impl PetApi for PetApiClient {
};


req_builder = req_builder.form(&form);

// send request
let req = req_builder.build()?;

Expand All @@ -269,9 +281,13 @@ impl PetApi for PetApiClient {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

let form = [
("additionalMetadata", &additional_metadata.to_string()),
("file", &file.to_string()),
];
let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/pet/{petId}/uploadImage?{}", configuration.base_path, query_string, petId=pet_id);
Expand All @@ -289,6 +305,8 @@ impl PetApi for PetApiClient {
};


req_builder = req_builder.form(&form);

// send request
let req = req_builder.build()?;

Expand Down
12 changes: 8 additions & 4 deletions samples/client/petstore/rust-reqwest/src/apis/store_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl StoreApi for StoreApiClient {

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/store/order/{orderId}?{}", configuration.base_path, query_string, orderId=order_id);
Expand All @@ -56,6 +56,7 @@ impl StoreApi for StoreApiClient {




// send request
let req = req_builder.build()?;

Expand All @@ -69,7 +70,7 @@ impl StoreApi for StoreApiClient {

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/store/inventory?{}", configuration.base_path, query_string);
Expand All @@ -93,6 +94,7 @@ impl StoreApi for StoreApiClient {




// send request
let req = req_builder.build()?;

Expand All @@ -105,7 +107,7 @@ impl StoreApi for StoreApiClient {

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/store/order/{orderId}?{}", configuration.base_path, query_string, orderId=order_id);
Expand All @@ -119,6 +121,7 @@ impl StoreApi for StoreApiClient {




// send request
let req = req_builder.build()?;

Expand All @@ -131,7 +134,7 @@ impl StoreApi for StoreApiClient {

let query_string = {
let mut query = ::url::form_urlencoded::Serializer::new(String::new());

query.finish()
};
let uri_str = format!("{}/store/order?{}", configuration.base_path, query_string);
Expand All @@ -146,6 +149,7 @@ impl StoreApi for StoreApiClient {

req_builder = req_builder.json(&order);


// send request
let req = req_builder.build()?;

Expand Down
Loading

0 comments on commit 4a494b4

Please sign in to comment.