forked from loafle/openapi-generator-original
[rust] Fix generation for optional and nullable fields (double option pattern) (#13177)
* Fix generation for optional and nullable fields (double option pattern) * Only import serde_with if necessary
This commit is contained in:
parent
53dc385fc6
commit
c1c9cb2192
@ -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<ModelMap> allModels) {
|
||||
OperationMap objectMap = objs.getOperations();
|
||||
|
@ -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"] }
|
||||
|
@ -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}}
|
||||
}
|
||||
|
||||
|
@ -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
|
@ -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
|
||||
|
@ -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"] }
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"] }
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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"] }
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"] }
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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::<OptionalTesting>(&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::<OptionalTesting>(&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::<OptionalTesting>(&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::<OptionalTesting>(&input)
|
||||
.unwrap()
|
||||
.optional_nullable,
|
||||
Some(Some(_))
|
||||
));
|
||||
}
|
@ -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");
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user