Skip to content

Commit

Permalink
Better Rust client imports (OpenAPITools#3332)
Browse files Browse the repository at this point in the history
* Better Rust client imports.

* No more "unused_imports" in models for serde_json::Value.
* No more compilation problem for apis requiring serde_json::Value.
* "crate::" prefix for models and apis imports, for compatibility with Rust 2018 edition.

* Rust samples regeneration using new code.
  • Loading branch information
bcourtine authored and bjgill committed Jul 18, 2019
1 parent 0fec25e commit 26b8d1f
Show file tree
Hide file tree
Showing 36 changed files with 126 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ public RustClientCodegen() {
// I tried to map as "std::io::File", but Reqwest multipart file requires a "AsRef<Path>" param.
// Getting a file from a Path is simple, but the opposite is difficult. So I map as "std::path::Path".
typeMapping.put("file", "std::path::PathBuf");
typeMapping.put("binary", "::models::File");
typeMapping.put("binary", "crate::models::File");
typeMapping.put("ByteArray", "String");
typeMapping.put("object", "Value");
typeMapping.put("object", "serde_json::Value");

// no need for rust
//importMapping = new HashMap<String, String>();
Expand Down Expand Up @@ -352,8 +352,8 @@ public String getTypeDeclaration(Schema p) {
}

// return fully-qualified model name
// ::models::{{classnameFile}}::{{classname}}
return "::models::" + toModelName(schemaType);
// crate::models::{{classnameFile}}::{{classname}}
return "crate::models::" + toModelName(schemaType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<C: hyper::client::Connect> {{{classname}}}Client<C> {
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}}) -> Box<Future<Item = {{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error = Error<serde_json::Value>>>;
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}crate::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Box<Future<Item = {{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error = Error<serde_json::Value>>>;
{{/operation}}
{{/operations}}
}
Expand All @@ -33,7 +33,7 @@ pub trait {{{classname}}} {
impl<C: hyper::client::Connect>{{{classname}}} for {{{classname}}}Client<C> {
{{#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}}) -> Box<Future<Item = {{^returnType}}(){{/returnType}}{{#returnType}}{{{.}}}{{/returnType}}, Error = Error<serde_json::Value>>> {
fn {{{operationId}}}(&self, {{#allParams}}{{{paramName}}}: {{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}crate::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Box<Future<Item = {{^returnType}}(){{/returnType}}{{#returnType}}{{{.}}}{{/returnType}}, Error = Error<serde_json::Value>>> {
__internal_request::Request::new(hyper::Method::{{{httpMethod}}}, "{{{path}}}".to_string())
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct APIClient<C: hyper::client::Connect> {
{{#operations}}
{{#operation}}
{{#-last}}
{{{classFilename}}}: Box<::apis::{{{classname}}}>,
{{{classFilename}}}: Box<crate::apis::{{{classname}}}>,
{{/-last}}
{{/operation}}
{{/operations}}
Expand All @@ -29,7 +29,7 @@ impl<C: hyper::client::Connect> APIClient<C> {
{{#operations}}
{{#operation}}
{{#-last}}
{{{classFilename}}}: Box::new(::apis::{{{classname}}}Client::new(rc.clone())),
{{{classFilename}}}: Box::new(crate::apis::{{{classname}}}Client::new(rc.clone())),
{{/-last}}
{{/operation}}
{{/operations}}
Expand All @@ -43,7 +43,7 @@ impl<C: hyper::client::Connect> APIClient<C> {
{{#operations}}
{{#operation}}
{{#-last}}
pub fn {{{classFilename}}}(&self) -> &::apis::{{{classname}}}{
pub fn {{{classFilename}}}(&self) -> &crate::apis::{{{classname}}}{
self.{{{classFilename}}}.as_ref()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
/// {{{classname}}} : {{{description}}}
{{/description}}

#[allow(unused_imports)]
use serde_json::Value;

{{!-- for enum schemas --}}
{{#isEnum}}
/// {{{description}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ 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}}}: {{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}crate::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Result<{{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error>;
{{/operation}}
{{/operations}}
}

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}}}: {{#isString}}&str{{/isString}}{{#isUuid}}&str{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}crate::models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Result<{{^returnType}}(){{/returnType}}{{#returnType}}{{{returnType}}}{{/returnType}}, Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct APIClient {
{{#operations}}
{{#operation}}
{{#-last}}
{{{classFilename}}}: Box<::apis::{{{classname}}}>,
{{{classFilename}}}: Box<crate::apis::{{{classname}}}>,
{{/-last}}
{{/operation}}
{{/operations}}
Expand All @@ -28,7 +28,7 @@ impl APIClient {
{{#operations}}
{{#operation}}
{{#-last}}
{{{classFilename}}}: Box::new(::apis::{{{classname}}}Client::new(rc.clone())),
{{{classFilename}}}: Box::new(crate::apis::{{{classname}}}Client::new(rc.clone())),
{{/-last}}
{{/operation}}
{{/operations}}
Expand All @@ -42,7 +42,7 @@ impl APIClient {
{{#operations}}
{{#operation}}
{{#-last}}
pub fn {{{classFilename}}}(&self) -> &::apis::{{{classname}}}{
pub fn {{{classFilename}}}(&self) -> &crate::apis::{{{classname}}}{
self.{{{classFilename}}}.as_ref()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.2-SNAPSHOT
4.1.0-SNAPSHOT
4 changes: 2 additions & 2 deletions samples/client/petstore/rust-reqwest/docs/Pet.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | **i64** | | [optional]
**category** | [***::models::Category**](Category.md) | | [optional]
**category** | [***crate::models::Category**](Category.md) | | [optional]
**name** | **String** | |
**photo_urls** | **Vec<String>** | |
**tags** | [**Vec<::models::Tag>**](Tag.md) | | [optional]
**tags** | [**Vec<crate::models::Tag>**](Tag.md) | | [optional]
**status** | **String** | pet status in the store | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/rust-reqwest/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Name | Type | Description | Notes

## find_pets_by_status

> Vec<::models::Pet> find_pets_by_status(ctx, status)
> Vec<crate::models::Pet> find_pets_by_status(ctx, status)
Finds Pets by status

Multiple status values can be provided with comma separated strings
Expand All @@ -100,7 +100,7 @@ Name | Type | Description | Notes

### Return type

[**Vec<::models::Pet>**](Pet.md)
[**Vec<crate::models::Pet>**](Pet.md)

### Authorization

Expand All @@ -116,7 +116,7 @@ Name | Type | Description | Notes

## find_pets_by_tags

> Vec<::models::Pet> find_pets_by_tags(ctx, tags)
> Vec<crate::models::Pet> find_pets_by_tags(ctx, tags)
Finds Pets by tags

Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
Expand All @@ -131,7 +131,7 @@ Name | Type | Description | Notes

### Return type

[**Vec<::models::Pet>**](Pet.md)
[**Vec<crate::models::Pet>**](Pet.md)

### Authorization

Expand All @@ -147,7 +147,7 @@ Name | Type | Description | Notes

## get_pet_by_id

> ::models::Pet get_pet_by_id(ctx, pet_id)
> crate::models::Pet get_pet_by_id(ctx, pet_id)
Find pet by ID

Returns a single pet
Expand All @@ -162,7 +162,7 @@ Name | Type | Description | Notes

### Return type

[**::models::Pet**](Pet.md)
[**crate::models::Pet**](Pet.md)

### Authorization

Expand Down Expand Up @@ -247,7 +247,7 @@ Name | Type | Description | Notes

## upload_file

> ::models::ApiResponse upload_file(ctx, pet_id, optional)
> crate::models::ApiResponse upload_file(ctx, pet_id, optional)
uploads an image

### Required Parameters
Expand All @@ -271,7 +271,7 @@ Name | Type | Description | Notes

### Return type

[**::models::ApiResponse**](ApiResponse.md)
[**crate::models::ApiResponse**](ApiResponse.md)

### Authorization

Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/rust-reqwest/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ This endpoint does not need any parameter.

## get_order_by_id

> ::models::Order get_order_by_id(order_id)
> crate::models::Order get_order_by_id(order_id)
Find purchase order by ID

For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
Expand All @@ -84,7 +84,7 @@ Name | Type | Description | Notes

### Return type

[**::models::Order**](Order.md)
[**crate::models::Order**](Order.md)

### Authorization

Expand All @@ -100,7 +100,7 @@ No authorization required

## place_order

> ::models::Order place_order(body)
> crate::models::Order place_order(body)
Place an order for a pet

### Required Parameters
Expand All @@ -112,7 +112,7 @@ Name | Type | Description | Notes

### Return type

[**::models::Order**](Order.md)
[**crate::models::Order**](Order.md)

### Authorization

Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/rust-reqwest/docs/UserApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Creates list of users with given input array

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | [**Vec<::models::User>**](User.md)| List of user object |
**body** | [**Vec<crate::models::User>**](User.md)| List of user object |

### Return type

Expand Down Expand Up @@ -83,7 +83,7 @@ Creates list of users with given input array

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | [**Vec<::models::User>**](User.md)| List of user object |
**body** | [**Vec<crate::models::User>**](User.md)| List of user object |

### Return type

Expand Down Expand Up @@ -133,7 +133,7 @@ No authorization required

## get_user_by_name

> ::models::User get_user_by_name(username)
> crate::models::User get_user_by_name(username)
Get user by user name

### Required Parameters
Expand All @@ -145,7 +145,7 @@ Name | Type | Description | Notes

### Return type

[**::models::User**](User.md)
[**crate::models::User**](User.md)

### Authorization

Expand Down
18 changes: 9 additions & 9 deletions samples/client/petstore/rust-reqwest/src/apis/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use super::configuration::Configuration;

pub struct APIClient {
configuration: Rc<Configuration>,
pet_api: Box<::apis::PetApi>,
store_api: Box<::apis::StoreApi>,
user_api: Box<::apis::UserApi>,
pet_api: Box<crate::apis::PetApi>,
store_api: Box<crate::apis::StoreApi>,
user_api: Box<crate::apis::UserApi>,
}

impl APIClient {
Expand All @@ -15,21 +15,21 @@ impl APIClient {

APIClient {
configuration: rc.clone(),
pet_api: Box::new(::apis::PetApiClient::new(rc.clone())),
store_api: Box::new(::apis::StoreApiClient::new(rc.clone())),
user_api: Box::new(::apis::UserApiClient::new(rc.clone())),
pet_api: Box::new(crate::apis::PetApiClient::new(rc.clone())),
store_api: Box::new(crate::apis::StoreApiClient::new(rc.clone())),
user_api: Box::new(crate::apis::UserApiClient::new(rc.clone())),
}
}

pub fn pet_api(&self) -> &::apis::PetApi{
pub fn pet_api(&self) -> &crate::apis::PetApi{
self.pet_api.as_ref()
}

pub fn store_api(&self) -> &::apis::StoreApi{
pub fn store_api(&self) -> &crate::apis::StoreApi{
self.store_api.as_ref()
}

pub fn user_api(&self) -> &::apis::UserApi{
pub fn user_api(&self) -> &crate::apis::UserApi{
self.user_api.as_ref()
}

Expand Down
24 changes: 12 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 @@ -28,18 +28,18 @@ impl PetApiClient {
}

pub trait PetApi {
fn add_pet(&self, body: ::models::Pet) -> Result<(), Error>;
fn add_pet(&self, body: crate::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 get_pet_by_id(&self, pet_id: i64) -> Result<::models::Pet, Error>;
fn update_pet(&self, body: ::models::Pet) -> Result<(), Error>;
fn find_pets_by_status(&self, status: Vec<String>) -> Result<Vec<crate::models::Pet>, Error>;
fn find_pets_by_tags(&self, tags: Vec<String>) -> Result<Vec<crate::models::Pet>, Error>;
fn get_pet_by_id(&self, pet_id: i64) -> Result<crate::models::Pet, Error>;
fn update_pet(&self, body: crate::models::Pet) -> Result<(), Error>;
fn update_pet_with_form(&self, pet_id: i64, name: &str, status: &str) -> Result<(), Error>;
fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: std::path::PathBuf) -> Result<::models::ApiResponse, Error>;
fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: std::path::PathBuf) -> Result<crate::models::ApiResponse, Error>;
}

impl PetApi for PetApiClient {
fn add_pet(&self, body: ::models::Pet) -> Result<(), Error> {
fn add_pet(&self, body: crate::models::Pet) -> Result<(), Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

Expand Down Expand Up @@ -83,7 +83,7 @@ impl PetApi for PetApiClient {
Ok(())
}

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

Expand All @@ -104,7 +104,7 @@ impl PetApi for PetApiClient {
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<String>) -> Result<Vec<crate::models::Pet>, Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

Expand All @@ -125,7 +125,7 @@ impl PetApi for PetApiClient {
Ok(client.execute(req)?.error_for_status()?.json()?)
}

fn get_pet_by_id(&self, pet_id: i64) -> Result<::models::Pet, Error> {
fn get_pet_by_id(&self, pet_id: i64) -> Result<crate::models::Pet, Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

Expand All @@ -150,7 +150,7 @@ impl PetApi for PetApiClient {
Ok(client.execute(req)?.error_for_status()?.json()?)
}

fn update_pet(&self, body: ::models::Pet) -> Result<(), Error> {
fn update_pet(&self, body: crate::models::Pet) -> Result<(), Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

Expand Down Expand Up @@ -197,7 +197,7 @@ impl PetApi for PetApiClient {
Ok(())
}

fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: std::path::PathBuf) -> Result<::models::ApiResponse, Error> {
fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: std::path::PathBuf) -> Result<crate::models::ApiResponse, Error> {
let configuration: &configuration::Configuration = self.configuration.borrow();
let client = &configuration.client;

Expand Down
Loading

0 comments on commit 26b8d1f

Please sign in to comment.