Skip to content

Commit

Permalink
Fix generation for optional and nullable fields (double option pattern)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-pro committed Oct 9, 2022
1 parent 0291682 commit 4a77804
Show file tree
Hide file tree
Showing 30 changed files with 328 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
serde = "^1.0"
serde_derive = "^1.0"
serde_with = "^2.0"
serde_json = "^1.0"
url = "^2.2"
uuid = { version = "^1.0", features = ["serde"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub struct {{{classname}}} {
{{#description}}
/// {{{.}}}
{{/description}}
#[serde(rename = "{{{baseName}}}"{{^required}}, skip_serializing_if = "Option::is_none"{{/required}})]
pub {{{name}}}: {{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{^required}}Option<{{/required}}{{#isEnum}}{{#isArray}}{{#uniqueItems}}std::collections::HashSet<{{/uniqueItems}}{{^uniqueItems}}Vec<{{/uniqueItems}}{{/isArray}}{{{enumName}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{#isModel}}Box<{{{dataType}}}>{{/isModel}}{{^isModel}}{{{dataType}}}{{/isModel}}{{/isEnum}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^required}}>{{/required}},
#[serde(rename = "{{{baseName}}}"{{^required}}{{#isNullable}}, default, with = "::serde_with::rust::double_option"{{/isNullable}}{{/required}}{{^required}}, skip_serializing_if = "Option::is_none"{{/required}}{{#required}}{{#isNullable}}, deserialize_with = "Option::deserialize"{{/isNullable}}{{/required}})]
pub {{{name}}}: {{#isNullable}}Option<{{/isNullable}}{{^required}}Option<{{/required}}{{#isEnum}}{{#isArray}}{{#uniqueItems}}std::collections::HashSet<{{/uniqueItems}}{{^uniqueItems}}Vec<{{/uniqueItems}}{{/isArray}}{{{enumName}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{#isModel}}Box<{{{dataType}}}>{{/isModel}}{{^isModel}}{{{dataType}}}{{/isModel}}{{/isEnum}}{{#isNullable}}>{{/isNullable}}{{^required}}>{{/required}},
{{/vars}}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,4 +858,23 @@ components:
async:
type: boolean
super:
type: boolean
type: boolean
OptionalTesting:
description: Test handling of optional and nullable fields
type: object
required:
- required_nonnull
- required_nullable
properties:
optional_nonnull:
type: string
nullable: false
required_nonnull:
type: string
nullable: false
optional_nullable:
type: string
nullable: true
required_nullable:
type: string
nullable: true
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ docs/ApiResponse.md
docs/Baz.md
docs/Category.md
docs/FakeApi.md
docs/OptionalTesting.md
docs/Order.md
docs/Pet.md
docs/PetApi.md
Expand Down Expand Up @@ -35,6 +36,7 @@ src/models/baz.rs
src/models/category.rs
src/models/mod.rs
src/models/model_return.rs
src/models/optional_testing.rs
src/models/order.rs
src/models/pet.rs
src/models/property_test.rs
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/rust/hyper/petstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
serde = "^1.0"
serde_derive = "^1.0"
serde_with = "^2.0"
serde_json = "^1.0"
url = "^2.2"
uuid = { version = "^1.0", features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/rust/hyper/petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Class | Method | HTTP request | Description
- [ApiResponse](docs/ApiResponse.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [OptionalTesting](docs/OptionalTesting.md)
- [Order](docs/Order.md)
- [Pet](docs/Pet.md)
- [PropertyTest](docs/PropertyTest.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# OptionalTesting

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**optional_nonnull** | Option<**String**> | | [optional]
**required_nonnull** | **String** | |
**optional_nullable** | Option<**String**> | | [optional]
**required_nullable** | Option<**String**> | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


2 changes: 2 additions & 0 deletions samples/client/petstore/rust/hyper/petstore/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod baz;
pub use self::baz::Baz;
pub mod category;
pub use self::category::Category;
pub mod optional_testing;
pub use self::optional_testing::OptionalTesting;
pub mod order;
pub use self::order::Order;
pub mod pet;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/

/// OptionalTesting : Test handling of optional and nullable fields


#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct OptionalTesting {
#[serde(rename = "optional_nonnull", skip_serializing_if = "Option::is_none")]
pub optional_nonnull: Option<String>,
#[serde(rename = "required_nonnull")]
pub required_nonnull: String,
#[serde(rename = "optional_nullable", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub optional_nullable: Option<Option<String>>,
#[serde(rename = "required_nullable", deserialize_with = "Option::deserialize")]
pub required_nullable: Option<String>,
}

impl OptionalTesting {
/// Test handling of optional and nullable fields
pub fn new(required_nonnull: String, required_nullable: Option<String>) -> OptionalTesting {
OptionalTesting {
optional_nonnull: None,
required_nonnull,
optional_nullable: None,
required_nullable,
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ docs/ApiResponse.md
docs/Baz.md
docs/Category.md
docs/FakeApi.md
docs/OptionalTesting.md
docs/Order.md
docs/Pet.md
docs/PetApi.md
Expand All @@ -33,6 +34,7 @@ src/models/baz.rs
src/models/category.rs
src/models/mod.rs
src/models/model_return.rs
src/models/optional_testing.rs
src/models/order.rs
src/models/pet.rs
src/models/property_test.rs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
serde = "^1.0"
serde_derive = "^1.0"
serde_with = "^2.0"
serde_json = "^1.0"
url = "^2.2"
uuid = { version = "^1.0", features = ["serde"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Class | Method | HTTP request | Description
- [ApiResponse](docs/ApiResponse.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [OptionalTesting](docs/OptionalTesting.md)
- [Order](docs/Order.md)
- [Pet](docs/Pet.md)
- [PropertyTest](docs/PropertyTest.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# OptionalTesting

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**optional_nonnull** | Option<**String**> | | [optional]
**required_nonnull** | **String** | |
**optional_nullable** | Option<**String**> | | [optional]
**required_nullable** | Option<**String**> | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod baz;
pub use self::baz::Baz;
pub mod category;
pub use self::category::Category;
pub mod optional_testing;
pub use self::optional_testing::OptionalTesting;
pub mod order;
pub use self::order::Order;
pub mod pet;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/

/// OptionalTesting : Test handling of optional and nullable fields


#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct OptionalTesting {
#[serde(rename = "optional_nonnull", skip_serializing_if = "Option::is_none")]
pub optional_nonnull: Option<String>,
#[serde(rename = "required_nonnull")]
pub required_nonnull: String,
#[serde(rename = "optional_nullable", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub optional_nullable: Option<Option<String>>,
#[serde(rename = "required_nullable", deserialize_with = "Option::deserialize")]
pub required_nullable: Option<String>,
}

impl OptionalTesting {
/// Test handling of optional and nullable fields
pub fn new(required_nonnull: String, required_nullable: Option<String>) -> OptionalTesting {
OptionalTesting {
optional_nonnull: None,
required_nonnull,
optional_nullable: None,
required_nullable,
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate petstore_reqwest_async;
//use petstore_reqwest_async::apis::PetApiClient;
use petstore_reqwest_async::apis::configuration;
//use petstore_reqwest::apis::PetApiUpdatePetWithFormParams;
use petstore_reqwest_async::models::{Pet};
use petstore_reqwest_async::models::Pet;
use std::option::Option;

#[test]
Expand All @@ -16,16 +16,12 @@ fn test_pet() {
let mut new_pet = Pet::new("Rust Pet".to_string(), photo_urls);
new_pet.id = Option::Some(8787);

let new_pet_params = petstore_reqwest_async::apis::pet_api::AddPetParams {
pet: new_pet,
};
let new_pet_params = petstore_reqwest_async::apis::pet_api::AddPetParams { pet: new_pet };

// add pet
let _add_result = petstore_reqwest_async::apis::pet_api::add_pet(&config, new_pet_params);

let get_pet_params = petstore_reqwest_async::apis::pet_api::GetPetByIdParams {
pet_id: 8787,
};
let get_pet_params = petstore_reqwest_async::apis::pet_api::GetPetByIdParams { pet_id: 8787 };

// get pet
let _pet_result = petstore_reqwest_async::apis::pet_api::get_pet_by_id(&config, get_pet_params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ docs/ApiResponse.md
docs/Baz.md
docs/Category.md
docs/FakeApi.md
docs/OptionalTesting.md
docs/Order.md
docs/Pet.md
docs/PetApi.md
Expand All @@ -33,6 +34,7 @@ src/models/baz.rs
src/models/category.rs
src/models/mod.rs
src/models/model_return.rs
src/models/optional_testing.rs
src/models/order.rs
src/models/pet.rs
src/models/property_test.rs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
serde = "^1.0"
serde_derive = "^1.0"
serde_with = "^2.0"
serde_json = "^1.0"
url = "^2.2"
uuid = { version = "^1.0", features = ["serde"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Class | Method | HTTP request | Description
- [ApiResponse](docs/ApiResponse.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [OptionalTesting](docs/OptionalTesting.md)
- [Order](docs/Order.md)
- [Pet](docs/Pet.md)
- [PropertyTest](docs/PropertyTest.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# OptionalTesting

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**optional_nonnull** | Option<**String**> | | [optional]
**required_nonnull** | **String** | |
**optional_nullable** | Option<**String**> | | [optional]
**required_nullable** | Option<**String**> | |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod baz;
pub use self::baz::Baz;
pub mod category;
pub use self::category::Category;
pub mod optional_testing;
pub use self::optional_testing::OptionalTesting;
pub mod order;
pub use self::order::Order;
pub mod pet;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* The version of the OpenAPI document: 1.0.0
*
* Generated by: https://openapi-generator.tech
*/

/// OptionalTesting : Test handling of optional and nullable fields


#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct OptionalTesting {
#[serde(rename = "optional_nonnull", skip_serializing_if = "Option::is_none")]
pub optional_nonnull: Option<String>,
#[serde(rename = "required_nonnull")]
pub required_nonnull: String,
#[serde(rename = "optional_nullable", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub optional_nullable: Option<Option<String>>,
#[serde(rename = "required_nullable", deserialize_with = "Option::deserialize")]
pub required_nullable: Option<String>,
}

impl OptionalTesting {
/// Test handling of optional and nullable fields
pub fn new(required_nonnull: String, required_nullable: Option<String>) -> OptionalTesting {
OptionalTesting {
optional_nonnull: None,
required_nonnull,
optional_nullable: None,
required_nullable,
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ docs/ApiResponse.md
docs/Baz.md
docs/Category.md
docs/FakeApi.md
docs/OptionalTesting.md
docs/Order.md
docs/Pet.md
docs/PetApi.md
Expand All @@ -33,6 +34,7 @@ src/models/baz.rs
src/models/category.rs
src/models/mod.rs
src/models/model_return.rs
src/models/optional_testing.rs
src/models/order.rs
src/models/pet.rs
src/models/property_test.rs
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/rust/reqwest/petstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
serde = "^1.0"
serde_derive = "^1.0"
serde_with = "^2.0"
serde_json = "^1.0"
url = "^2.2"
uuid = { version = "^1.0", features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/rust/reqwest/petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Class | Method | HTTP request | Description
- [ApiResponse](docs/ApiResponse.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [OptionalTesting](docs/OptionalTesting.md)
- [Order](docs/Order.md)
- [Pet](docs/Pet.md)
- [PropertyTest](docs/PropertyTest.md)
Expand Down
Loading

0 comments on commit 4a77804

Please sign in to comment.