From 550890313452c78bc5e893f33034cb59a9188396 Mon Sep 17 00:00:00 2001 From: Richard Whitehouse Date: Fri, 9 Aug 2019 13:56:08 +0100 Subject: [PATCH 1/3] [Rust Server] Handle arrays of object in objects Use correct data type for arrays inside objects. --- .../src/main/resources/rust-server/models.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/rust-server/models.mustache b/modules/openapi-generator/src/main/resources/rust-server/models.mustache index 3edf610cbf55..d05c7f7d5efa 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/models.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/models.mustache @@ -183,7 +183,7 @@ pub struct {{{classname}}} { #[serde(deserialize_with = "swagger::nullable_format::deserialize_optional_nullable")] #[serde(default = "swagger::nullable_format::default_optional_nullable")]{{/isNullable}} #[serde(skip_serializing_if="Option::is_none")] - pub {{{name}}}: Option<{{#isNullable}}swagger::Nullable<{{/isNullable}}{{#isListContainer}}Vec<{{#items}}{{{dataType}}}{{/items}}>{{/isListContainer}}{{^isListContainer}}{{{dataType}}}{{/isListContainer}}{{#isNullable}}>{{/isNullable}}>, + pub {{{name}}}: Option<{{#isNullable}}swagger::Nullable<{{/isNullable}}{{{dataType}}}{{#isNullable}}>{{/isNullable}}>, {{/required}} {{/vars}} From cd123f3d0dc9c3af174f6d1e7361c214e3d63f0c Mon Sep 17 00:00:00 2001 From: Richard Whitehouse Date: Fri, 9 Aug 2019 13:55:27 +0100 Subject: [PATCH 2/3] [Rust Server] Add test for arrays of objects --- .../src/test/resources/3_0/rust-server/openapi-v3.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml b/modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml index 619a49d1c503..777e5a65189f 100644 --- a/modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml @@ -260,3 +260,12 @@ components: xml: name: snake_another_xml_object namespace: http://foo.bar + ObjectWithArrayOfObjects: + type: object + properties: + objectArray: + type: array + items: + $ref: '#/components/schemas/StringObject' + StringObject: + type: string From 37c9b9224861305ed817362e4de721faf77aa2a3 Mon Sep 17 00:00:00 2001 From: Richard Whitehouse Date: Sun, 19 Jan 2020 14:52:30 +0000 Subject: [PATCH 3/3] Update samples --- .../rust-server/output/openapi-v3/README.md | 2 + .../output/openapi-v3/api/openapi.yaml | 9 +++ .../docs/ObjectWithArrayOfObjects.md | 10 +++ .../output/openapi-v3/docs/StringObject.md | 9 +++ .../output/openapi-v3/src/models.rs | 72 +++++++++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 samples/server/petstore/rust-server/output/openapi-v3/docs/ObjectWithArrayOfObjects.md create mode 100644 samples/server/petstore/rust-server/output/openapi-v3/docs/StringObject.md diff --git a/samples/server/petstore/rust-server/output/openapi-v3/README.md b/samples/server/petstore/rust-server/output/openapi-v3/README.md index 248f4335fac4..86ed998313f7 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/README.md +++ b/samples/server/petstore/rust-server/output/openapi-v3/README.md @@ -125,6 +125,8 @@ Method | HTTP request | Description - [AnotherXmlObject](docs/AnotherXmlObject.md) - [DuplicateXmlObject](docs/DuplicateXmlObject.md) - [InlineResponse201](docs/InlineResponse201.md) + - [ObjectWithArrayOfObjects](docs/ObjectWithArrayOfObjects.md) + - [StringObject](docs/StringObject.md) - [UuidObject](docs/UuidObject.md) - [XmlArray](docs/XmlArray.md) - [XmlInner](docs/XmlInner.md) diff --git a/samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml b/samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml index 8181f26db03c..57d1ee85ba59 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml +++ b/samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml @@ -242,6 +242,15 @@ components: xml: name: snake_another_xml_object namespace: http://foo.bar + ObjectWithArrayOfObjects: + properties: + objectArray: + items: + $ref: '#/components/schemas/StringObject' + type: array + type: object + StringObject: + type: string inline_response_201: properties: foo: diff --git a/samples/server/petstore/rust-server/output/openapi-v3/docs/ObjectWithArrayOfObjects.md b/samples/server/petstore/rust-server/output/openapi-v3/docs/ObjectWithArrayOfObjects.md new file mode 100644 index 000000000000..6801b3966ec4 --- /dev/null +++ b/samples/server/petstore/rust-server/output/openapi-v3/docs/ObjectWithArrayOfObjects.md @@ -0,0 +1,10 @@ +# ObjectWithArrayOfObjects + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**object_array** | **Vec** | | [optional] [default to None] + +[[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/server/petstore/rust-server/output/openapi-v3/docs/StringObject.md b/samples/server/petstore/rust-server/output/openapi-v3/docs/StringObject.md new file mode 100644 index 000000000000..2b822551043c --- /dev/null +++ b/samples/server/petstore/rust-server/output/openapi-v3/docs/StringObject.md @@ -0,0 +1,9 @@ +# StringObject + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +[[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/server/petstore/rust-server/output/openapi-v3/src/models.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/models.rs index e51d31f2d51c..ab3b7f96711a 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/models.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/models.rs @@ -243,6 +243,78 @@ impl InlineResponse201 { } } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[cfg_attr(feature = "conversion", derive(LabelledGeneric))] +pub struct ObjectWithArrayOfObjects { + #[serde(rename = "objectArray")] + #[serde(skip_serializing_if="Option::is_none")] + pub object_array: Option>, + +} + +impl ObjectWithArrayOfObjects { + pub fn new() -> ObjectWithArrayOfObjects { + ObjectWithArrayOfObjects { + object_array: None, + } + } +} + +impl ObjectWithArrayOfObjects { + /// Helper function to allow us to convert this model to an XML string. + /// Will panic if serialisation fails. + #[allow(dead_code)] + pub(crate) fn to_xml(&self) -> String { + serde_xml_rs::to_string(&self).expect("impossible to fail to serialize") + } +} + +#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)] +#[cfg_attr(feature = "conversion", derive(LabelledGeneric))] +pub struct StringObject(String); + +impl ::std::convert::From for StringObject { + fn from(x: String) -> Self { + StringObject(x) + } +} + +impl std::str::FromStr for StringObject { + type Err = ParseError; + fn from_str(x: &str) -> Result { + Ok(StringObject(x.to_string())) + } +} + +impl ::std::convert::From for String { + fn from(x: StringObject) -> Self { + x.0 + } +} + +impl ::std::ops::Deref for StringObject { + type Target = String; + fn deref(&self) -> &String { + &self.0 + } +} + +impl ::std::ops::DerefMut for StringObject { + fn deref_mut(&mut self) -> &mut String { + &mut self.0 + } +} + + +impl StringObject { + /// Helper function to allow us to convert this model to an XML string. + /// Will panic if serialisation fails. + #[allow(dead_code)] + pub(crate) fn to_xml(&self) -> String { + serde_xml_rs::to_string(&self).expect("impossible to fail to serialize") + } +} + /// Test a model containing a UUID #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)] #[cfg_attr(feature = "conversion", derive(LabelledGeneric))]