diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java index 73559171943..bbfa8f6afbc 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java @@ -450,6 +450,7 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon String schemaType = super.getSchemaType(p); String type = typeMapping.getOrDefault(schemaType, schemaType); + // Implement integer type fitting (when property is enabled) if (Objects.equals(p.getType(), "integer")) { boolean bestFit = convertPropertyToBoolean(BEST_FIT_INT); boolean preferUnsigned = convertPropertyToBoolean(PREFER_UNSIGNED_INT); @@ -483,6 +484,18 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon return type; } + @Override + public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { + super.postProcessModelProperty(model, property); + + // If a property is both nullable and non-required then we represent this using a double Option + // which requires the `serde_with` extension crate for deserialization. + // See: https://docs.rs/serde_with/latest/serde_with/rust/double_option/index.html + if (property.isNullable && !property.required) { + additionalProperties.put("serdeWith", true); + } + } + @Override public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List allModels) { OperationMap objectMap = objs.getOperations(); diff --git a/modules/openapi-generator/src/main/resources/rust/Cargo.mustache b/modules/openapi-generator/src/main/resources/rust/Cargo.mustache index 9ca82500e3a..d1fd21e0a77 100644 --- a/modules/openapi-generator/src/main/resources/rust/Cargo.mustache +++ b/modules/openapi-generator/src/main/resources/rust/Cargo.mustache @@ -7,6 +7,9 @@ edition = "2018" [dependencies] serde = "^1.0" serde_derive = "^1.0" +{{#serdeWith}} +serde_with = "^2.0" +{{/serdeWith}} serde_json = "^1.0" url = "^2.2" uuid = { version = "^1.0", features = ["serde"] } diff --git a/modules/openapi-generator/src/main/resources/rust/model.mustache b/modules/openapi-generator/src/main/resources/rust/model.mustache index 8e4dac90ce8..2fcad1fbf36 100644 --- a/modules/openapi-generator/src/main/resources/rust/model.mustache +++ b/modules/openapi-generator/src/main/resources/rust/model.mustache @@ -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}} } diff --git a/modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml b/modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml index deaf6dd0860..0c09dee9414 100644 --- a/modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml @@ -858,4 +858,23 @@ components: async: type: boolean super: - type: boolean \ No newline at end of file + 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 \ No newline at end of file diff --git a/samples/client/petstore/rust/hyper/petstore/.openapi-generator/FILES b/samples/client/petstore/rust/hyper/petstore/.openapi-generator/FILES index 6ebb4efdd0e..eeea90f1b1c 100644 --- a/samples/client/petstore/rust/hyper/petstore/.openapi-generator/FILES +++ b/samples/client/petstore/rust/hyper/petstore/.openapi-generator/FILES @@ -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 @@ -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 diff --git a/samples/client/petstore/rust/hyper/petstore/Cargo.toml b/samples/client/petstore/rust/hyper/petstore/Cargo.toml index daaad97bf86..17926e9a056 100644 --- a/samples/client/petstore/rust/hyper/petstore/Cargo.toml +++ b/samples/client/petstore/rust/hyper/petstore/Cargo.toml @@ -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"] } diff --git a/samples/client/petstore/rust/hyper/petstore/README.md b/samples/client/petstore/rust/hyper/petstore/README.md index 29c034e6f69..f92feb9d9f6 100644 --- a/samples/client/petstore/rust/hyper/petstore/README.md +++ b/samples/client/petstore/rust/hyper/petstore/README.md @@ -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) diff --git a/samples/client/petstore/rust/hyper/petstore/docs/OptionalTesting.md b/samples/client/petstore/rust/hyper/petstore/docs/OptionalTesting.md new file mode 100644 index 00000000000..029e38eb977 --- /dev/null +++ b/samples/client/petstore/rust/hyper/petstore/docs/OptionalTesting.md @@ -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) + + diff --git a/samples/client/petstore/rust/hyper/petstore/src/models/mod.rs b/samples/client/petstore/rust/hyper/petstore/src/models/mod.rs index af4630c6a26..c528fd499bd 100644 --- a/samples/client/petstore/rust/hyper/petstore/src/models/mod.rs +++ b/samples/client/petstore/rust/hyper/petstore/src/models/mod.rs @@ -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; diff --git a/samples/client/petstore/rust/hyper/petstore/src/models/optional_testing.rs b/samples/client/petstore/rust/hyper/petstore/src/models/optional_testing.rs new file mode 100644 index 00000000000..f966470fbce --- /dev/null +++ b/samples/client/petstore/rust/hyper/petstore/src/models/optional_testing.rs @@ -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, + #[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>, + #[serde(rename = "required_nullable", deserialize_with = "Option::deserialize")] + pub required_nullable: Option, +} + +impl OptionalTesting { + /// Test handling of optional and nullable fields + pub fn new(required_nonnull: String, required_nullable: Option) -> OptionalTesting { + OptionalTesting { + optional_nonnull: None, + required_nonnull, + optional_nullable: None, + required_nullable, + } + } +} + + diff --git a/samples/client/petstore/rust/reqwest/petstore-async/.openapi-generator/FILES b/samples/client/petstore/rust/reqwest/petstore-async/.openapi-generator/FILES index 6a0f473de45..4b2347f5e64 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/.openapi-generator/FILES +++ b/samples/client/petstore/rust/reqwest/petstore-async/.openapi-generator/FILES @@ -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 @@ -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 diff --git a/samples/client/petstore/rust/reqwest/petstore-async/Cargo.toml b/samples/client/petstore/rust/reqwest/petstore-async/Cargo.toml index 5902f2f8900..950452873da 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/Cargo.toml +++ b/samples/client/petstore/rust/reqwest/petstore-async/Cargo.toml @@ -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"] } diff --git a/samples/client/petstore/rust/reqwest/petstore-async/README.md b/samples/client/petstore/rust/reqwest/petstore-async/README.md index e17c4105c17..0d2259c258c 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/README.md +++ b/samples/client/petstore/rust/reqwest/petstore-async/README.md @@ -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) diff --git a/samples/client/petstore/rust/reqwest/petstore-async/docs/OptionalTesting.md b/samples/client/petstore/rust/reqwest/petstore-async/docs/OptionalTesting.md new file mode 100644 index 00000000000..029e38eb977 --- /dev/null +++ b/samples/client/petstore/rust/reqwest/petstore-async/docs/OptionalTesting.md @@ -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) + + diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/models/mod.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/models/mod.rs index af4630c6a26..c528fd499bd 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/src/models/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/models/mod.rs @@ -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; diff --git a/samples/client/petstore/rust/reqwest/petstore-async/src/models/optional_testing.rs b/samples/client/petstore/rust/reqwest/petstore-async/src/models/optional_testing.rs new file mode 100644 index 00000000000..f966470fbce --- /dev/null +++ b/samples/client/petstore/rust/reqwest/petstore-async/src/models/optional_testing.rs @@ -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, + #[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>, + #[serde(rename = "required_nullable", deserialize_with = "Option::deserialize")] + pub required_nullable: Option, +} + +impl OptionalTesting { + /// Test handling of optional and nullable fields + pub fn new(required_nonnull: String, required_nullable: Option) -> OptionalTesting { + OptionalTesting { + optional_nonnull: None, + required_nonnull, + optional_nullable: None, + required_nullable, + } + } +} + + diff --git a/samples/client/petstore/rust/reqwest/petstore-async/tests/pet_tests.rs b/samples/client/petstore/rust/reqwest/petstore-async/tests/pet_tests.rs index 8d9d2576f92..8ab1927deaf 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async/tests/pet_tests.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async/tests/pet_tests.rs @@ -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] @@ -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); diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/.openapi-generator/FILES b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/.openapi-generator/FILES index 6a0f473de45..4b2347f5e64 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/.openapi-generator/FILES +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/.openapi-generator/FILES @@ -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 @@ -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 diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/Cargo.toml b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/Cargo.toml index 112dcce90da..7ac0a2940a1 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/Cargo.toml +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/Cargo.toml @@ -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"] } diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/README.md b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/README.md index 8520ca9d803..6aa08b5fdf6 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/README.md +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/README.md @@ -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) diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/docs/OptionalTesting.md b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/docs/OptionalTesting.md new file mode 100644 index 00000000000..029e38eb977 --- /dev/null +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/docs/OptionalTesting.md @@ -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) + + diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/mod.rs b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/mod.rs index af4630c6a26..c528fd499bd 100644 --- a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/mod.rs @@ -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; diff --git a/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/optional_testing.rs b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/optional_testing.rs new file mode 100644 index 00000000000..f966470fbce --- /dev/null +++ b/samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/optional_testing.rs @@ -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, + #[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>, + #[serde(rename = "required_nullable", deserialize_with = "Option::deserialize")] + pub required_nullable: Option, +} + +impl OptionalTesting { + /// Test handling of optional and nullable fields + pub fn new(required_nonnull: String, required_nullable: Option) -> OptionalTesting { + OptionalTesting { + optional_nonnull: None, + required_nonnull, + optional_nullable: None, + required_nullable, + } + } +} + + diff --git a/samples/client/petstore/rust/reqwest/petstore/.openapi-generator/FILES b/samples/client/petstore/rust/reqwest/petstore/.openapi-generator/FILES index 6a0f473de45..4b2347f5e64 100644 --- a/samples/client/petstore/rust/reqwest/petstore/.openapi-generator/FILES +++ b/samples/client/petstore/rust/reqwest/petstore/.openapi-generator/FILES @@ -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 @@ -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 diff --git a/samples/client/petstore/rust/reqwest/petstore/Cargo.toml b/samples/client/petstore/rust/reqwest/petstore/Cargo.toml index a815e8f2f2f..b008ffc37b2 100644 --- a/samples/client/petstore/rust/reqwest/petstore/Cargo.toml +++ b/samples/client/petstore/rust/reqwest/petstore/Cargo.toml @@ -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"] } diff --git a/samples/client/petstore/rust/reqwest/petstore/README.md b/samples/client/petstore/rust/reqwest/petstore/README.md index 962cac9c971..8f11cb347dc 100644 --- a/samples/client/petstore/rust/reqwest/petstore/README.md +++ b/samples/client/petstore/rust/reqwest/petstore/README.md @@ -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) diff --git a/samples/client/petstore/rust/reqwest/petstore/docs/OptionalTesting.md b/samples/client/petstore/rust/reqwest/petstore/docs/OptionalTesting.md new file mode 100644 index 00000000000..029e38eb977 --- /dev/null +++ b/samples/client/petstore/rust/reqwest/petstore/docs/OptionalTesting.md @@ -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) + + diff --git a/samples/client/petstore/rust/reqwest/petstore/src/models/mod.rs b/samples/client/petstore/rust/reqwest/petstore/src/models/mod.rs index af4630c6a26..c528fd499bd 100644 --- a/samples/client/petstore/rust/reqwest/petstore/src/models/mod.rs +++ b/samples/client/petstore/rust/reqwest/petstore/src/models/mod.rs @@ -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; diff --git a/samples/client/petstore/rust/reqwest/petstore/src/models/optional_testing.rs b/samples/client/petstore/rust/reqwest/petstore/src/models/optional_testing.rs new file mode 100644 index 00000000000..f966470fbce --- /dev/null +++ b/samples/client/petstore/rust/reqwest/petstore/src/models/optional_testing.rs @@ -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, + #[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>, + #[serde(rename = "required_nullable", deserialize_with = "Option::deserialize")] + pub required_nullable: Option, +} + +impl OptionalTesting { + /// Test handling of optional and nullable fields + pub fn new(required_nonnull: String, required_nullable: Option) -> OptionalTesting { + OptionalTesting { + optional_nonnull: None, + required_nonnull, + optional_nullable: None, + required_nullable, + } + } +} + + diff --git a/samples/client/petstore/rust/reqwest/petstore/tests/optional_tests.rs b/samples/client/petstore/rust/reqwest/petstore/tests/optional_tests.rs new file mode 100644 index 00000000000..3ae39aef6b2 --- /dev/null +++ b/samples/client/petstore/rust/reqwest/petstore/tests/optional_tests.rs @@ -0,0 +1,58 @@ +extern crate petstore_reqwest; + +use petstore_reqwest::models::OptionalTesting; + +#[test] +fn test_serialization() { + let mut test = OptionalTesting { + optional_nonnull: None, + required_nonnull: "".to_string(), + optional_nullable: None, + required_nullable: None, + }; + // Only the required fields should show up in JSON + assert_eq!( + serde_json::to_string(&test).unwrap(), + "{\"required_nonnull\":\"\",\"required_nullable\":null}" + ); + // Setting the outer of `optional_nullable` it should be serialized as null + test.optional_nullable = Some(None); + assert_eq!( + serde_json::to_string(&test).unwrap(), + "{\"required_nonnull\":\"\",\"optional_nullable\":null,\"required_nullable\":null}" + ); +} + +#[test] +fn test_deserialization() { + // `required_nullable` is missing so should fail to deserialize + let input = "{\"required_nonnull\": \"\"}"; + assert!(serde_json::from_str::(&input).is_err()); + + // After adding `required_nullable` it should deserialize + // `optional_nullable` should be None because it is not present + let input = "{\"required_nonnull\": \"\", \"required_nullable\": null}"; + let out = serde_json::from_str::(&input).unwrap(); + assert!(out.required_nullable.is_none()); + assert!(out.optional_nullable.is_none()); + + // Setting `optional_nullable` to null should be Some(None) + let input = + "{\"required_nonnull\": \"\", \"required_nullable\": null, \"optional_nullable\": null}"; + assert!(matches!( + serde_json::from_str::(&input) + .unwrap() + .optional_nullable, + Some(None) + )); + + // Setting `optional_nullable` to a value + let input = + "{\"required_nonnull\": \"\", \"required_nullable\": null, \"optional_nullable\": \"abc\"}"; + assert!(matches!( + serde_json::from_str::(&input) + .unwrap() + .optional_nullable, + Some(Some(_)) + )); +} diff --git a/samples/client/petstore/rust/reqwest/petstore/tests/pet_tests.rs b/samples/client/petstore/rust/reqwest/petstore/tests/pet_tests.rs index 9a1e00a70a2..fd49b3d2d4f 100644 --- a/samples/client/petstore/rust/reqwest/petstore/tests/pet_tests.rs +++ b/samples/client/petstore/rust/reqwest/petstore/tests/pet_tests.rs @@ -1,8 +1,8 @@ extern crate petstore_reqwest; -use petstore_reqwest::apis::pet_api::{add_pet, get_pet_by_id}; use petstore_reqwest::apis::configuration; -use petstore_reqwest::models::{Pet}; +use petstore_reqwest::apis::pet_api::{add_pet, get_pet_by_id}; +use petstore_reqwest::models::Pet; #[test] fn test_pet() { @@ -24,7 +24,10 @@ fn test_pet() { /* Test code when multiple returns option is not set. */ assert_eq!(resp.id, Option::Some(8787)); assert_eq!(resp.name, "Rust Pet"); - assert_eq!(resp.photo_urls, vec!["https://11".to_string(), "https://22".to_string()]); + assert_eq!( + resp.photo_urls, + vec!["https://11".to_string(), "https://22".to_string()] + ); /* Test code for multiple returns option. match resp.entity { Some(petstore_reqwest::apis::pet_api::GetPetByIdSuccess::Status200(pet)) => { @@ -37,10 +40,10 @@ fn test_pet() { }, }; */ - }, + } Err(error) => { println!("error: {:?}", error); panic!("Query should succeed"); - }, + } }; }