Skip to content

Commit

Permalink
[rust] Fix declaration for arrays with object and array references (#…
Browse files Browse the repository at this point in the history
…14198)

* [rust] Fix declaration for arrays with object and array references

For arrays with an item defined by reference to an array or an object,
the generated type declaration was `Vec<core::models::Array>` or
`Vec<core::models::Map>` without defining a `Array` or `Map` so that the
code didn't compile.

* [rust] Fix trailing whitespace in petstore definition
  • Loading branch information
tvrg authored Dec 28, 2022
1 parent 341a853 commit 88fa9ef
Show file tree
Hide file tree
Showing 27 changed files with 282 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,27 +424,28 @@ public String modelDocFileFolder() {

@Override
public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) {
ArraySchema ap = (ArraySchema) p;
Schema unaliasSchema = unaliasSchema(p);
if (ModelUtils.isArraySchema(unaliasSchema)) {
ArraySchema ap = (ArraySchema) unaliasSchema;
Schema inner = ap.getItems();
if (inner == null) {
LOGGER.warn("{}(array property) does not have a proper inner type defined.Default to string",
ap.getName());
inner = new StringSchema().description("TODO default missing array inner type to string");
}
return "Vec<" + getTypeDeclaration(inner) + ">";
} else if (ModelUtils.isMapSchema(p)) {
Schema inner = getAdditionalProperties(p);
} else if (ModelUtils.isMapSchema(unaliasSchema)) {
Schema inner = getAdditionalProperties(unaliasSchema);
if (inner == null) {
LOGGER.warn("{}(map property) does not have a proper inner type defined. Default to string", p.getName());
LOGGER.warn("{}(map property) does not have a proper inner type defined. Default to string", unaliasSchema.getName());
inner = new StringSchema().description("TODO default missing map inner type to string");
}
return "::std::collections::HashMap<String, " + getTypeDeclaration(inner) + ">";
}

// Not using the supertype invocation, because we want to UpperCamelize
// the type.
String schemaType = getSchemaType(p);
String schemaType = getSchemaType(unaliasSchema);
if (typeMapping.containsKey(schemaType)) {
return typeMapping.get(schemaType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ components:
properties:
uuid:
type: string
format: uuid
format: uuid
ActionContainer:
required:
- action
Expand Down Expand Up @@ -889,3 +889,27 @@ components:
items:
type: string
enum: ["A", "B", "C"]
ArrayRefItem:
description: Helper object for the array item ref test
type: array
items:
type: string
ObjectRefItem:
description: Helper object for the array item ref test
type: object
additionalProperties: true
ArrayItemRefTest:
description: Test handling of object reference in arrays
type: object
required:
- list_with_array_ref
- list_with_object_ref
properties:
list_with_array_ref:
type: array
items:
$ref: '#/components/schemas/ArrayRefItem'
list_with_object_ref:
type: array
items:
$ref: '#/components/schemas/ObjectRefItem'
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Cargo.toml
README.md
docs/ActionContainer.md
docs/ApiResponse.md
docs/ArrayItemRefTest.md
docs/Baz.md
docs/Category.md
docs/EnumArrayTesting.md
Expand Down Expand Up @@ -33,6 +34,7 @@ src/apis/user_api.rs
src/lib.rs
src/models/action_container.rs
src/models/api_response.rs
src/models/array_item_ref_test.rs
src/models/baz.rs
src/models/category.rs
src/models/enum_array_testing.rs
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 @@ -54,6 +54,7 @@ Class | Method | HTTP request | Description

- [ActionContainer](docs/ActionContainer.md)
- [ApiResponse](docs/ApiResponse.md)
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ArrayItemRefTest

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**list_with_array_ref** | [**Vec<Vec<String>>**](array.md) | |
**list_with_object_ref** | [**Vec<::std::collections::HashMap<String, serde_json::Value>>**](map.md) | |

[[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
@@ -0,0 +1,33 @@
/*
* 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
*/

/// ArrayItemRefTest : Test handling of object reference in arrays


#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct ArrayItemRefTest {
#[serde(rename = "list_with_array_ref")]
pub list_with_array_ref: Vec<Vec<String>>,
#[serde(rename = "list_with_object_ref")]
pub list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>,
}

impl ArrayItemRefTest {
/// Test handling of object reference in arrays
pub fn new(list_with_array_ref: Vec<Vec<String>>, list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>) -> ArrayItemRefTest {
ArrayItemRefTest {
list_with_array_ref,
list_with_object_ref,
}
}
}


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 @@ -2,6 +2,8 @@ pub mod action_container;
pub use self::action_container::ActionContainer;
pub mod api_response;
pub use self::api_response::ApiResponse;
pub mod array_item_ref_test;
pub use self::array_item_ref_test::ArrayItemRefTest;
pub mod baz;
pub use self::baz::Baz;
pub mod category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Cargo.toml
README.md
docs/ActionContainer.md
docs/ApiResponse.md
docs/ArrayItemRefTest.md
docs/Baz.md
docs/Category.md
docs/EnumArrayTesting.md
Expand Down Expand Up @@ -31,6 +32,7 @@ src/apis/user_api.rs
src/lib.rs
src/models/action_container.rs
src/models/api_response.rs
src/models/array_item_ref_test.rs
src/models/baz.rs
src/models/category.rs
src/models/enum_array_testing.rs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Class | Method | HTTP request | Description

- [ActionContainer](docs/ActionContainer.md)
- [ApiResponse](docs/ApiResponse.md)
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ArrayItemRefTest

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**list_with_array_ref** | [**Vec<Vec<String>>**](array.md) | |
**list_with_object_ref** | [**Vec<::std::collections::HashMap<String, serde_json::Value>>**](map.md) | |

[[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
@@ -0,0 +1,33 @@
/*
* 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
*/

/// ArrayItemRefTest : Test handling of object reference in arrays


#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct ArrayItemRefTest {
#[serde(rename = "list_with_array_ref")]
pub list_with_array_ref: Vec<Vec<String>>,
#[serde(rename = "list_with_object_ref")]
pub list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>,
}

impl ArrayItemRefTest {
/// Test handling of object reference in arrays
pub fn new(list_with_array_ref: Vec<Vec<String>>, list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>) -> ArrayItemRefTest {
ArrayItemRefTest {
list_with_array_ref,
list_with_object_ref,
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod action_container;
pub use self::action_container::ActionContainer;
pub mod api_response;
pub use self::api_response::ApiResponse;
pub mod array_item_ref_test;
pub use self::array_item_ref_test::ArrayItemRefTest;
pub mod baz;
pub use self::baz::Baz;
pub mod category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Cargo.toml
README.md
docs/ActionContainer.md
docs/ApiResponse.md
docs/ArrayItemRefTest.md
docs/Baz.md
docs/Category.md
docs/EnumArrayTesting.md
Expand Down Expand Up @@ -31,6 +32,7 @@ src/apis/user_api.rs
src/lib.rs
src/models/action_container.rs
src/models/api_response.rs
src/models/array_item_ref_test.rs
src/models/baz.rs
src/models/category.rs
src/models/enum_array_testing.rs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Class | Method | HTTP request | Description

- [ActionContainer](docs/ActionContainer.md)
- [ApiResponse](docs/ApiResponse.md)
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ArrayItemRefTest

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**list_with_array_ref** | [**Vec<Vec<String>>**](array.md) | |
**list_with_object_ref** | [**Vec<::std::collections::HashMap<String, serde_json::Value>>**](map.md) | |

[[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
@@ -0,0 +1,33 @@
/*
* 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
*/

/// ArrayItemRefTest : Test handling of object reference in arrays


#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct ArrayItemRefTest {
#[serde(rename = "list_with_array_ref")]
pub list_with_array_ref: Vec<Vec<String>>,
#[serde(rename = "list_with_object_ref")]
pub list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>,
}

impl ArrayItemRefTest {
/// Test handling of object reference in arrays
pub fn new(list_with_array_ref: Vec<Vec<String>>, list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>) -> ArrayItemRefTest {
ArrayItemRefTest {
list_with_array_ref,
list_with_object_ref,
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod action_container;
pub use self::action_container::ActionContainer;
pub mod api_response;
pub use self::api_response::ApiResponse;
pub mod array_item_ref_test;
pub use self::array_item_ref_test::ArrayItemRefTest;
pub mod baz;
pub use self::baz::Baz;
pub mod category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Cargo.toml
README.md
docs/ActionContainer.md
docs/ApiResponse.md
docs/ArrayItemRefTest.md
docs/Baz.md
docs/Category.md
docs/EnumArrayTesting.md
Expand Down Expand Up @@ -31,6 +32,7 @@ src/apis/user_api.rs
src/lib.rs
src/models/action_container.rs
src/models/api_response.rs
src/models/array_item_ref_test.rs
src/models/baz.rs
src/models/category.rs
src/models/enum_array_testing.rs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Class | Method | HTTP request | Description

- [ActionContainer](docs/ActionContainer.md)
- [ApiResponse](docs/ApiResponse.md)
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
- [Baz](docs/Baz.md)
- [Category](docs/Category.md)
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ArrayItemRefTest

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**list_with_array_ref** | [**Vec<Vec<String>>**](array.md) | |
**list_with_object_ref** | [**Vec<::std::collections::HashMap<String, serde_json::Value>>**](map.md) | |

[[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
@@ -0,0 +1,33 @@
/*
* 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
*/

/// ArrayItemRefTest : Test handling of object reference in arrays


#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct ArrayItemRefTest {
#[serde(rename = "list_with_array_ref")]
pub list_with_array_ref: Vec<Vec<String>>,
#[serde(rename = "list_with_object_ref")]
pub list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>,
}

impl ArrayItemRefTest {
/// Test handling of object reference in arrays
pub fn new(list_with_array_ref: Vec<Vec<String>>, list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>) -> ArrayItemRefTest {
ArrayItemRefTest {
list_with_array_ref,
list_with_object_ref,
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod action_container;
pub use self::action_container::ActionContainer;
pub mod api_response;
pub use self::api_response::ApiResponse;
pub mod array_item_ref_test;
pub use self::array_item_ref_test::ArrayItemRefTest;
pub mod baz;
pub use self::baz::Baz;
pub mod category;
Expand Down
Loading

0 comments on commit 88fa9ef

Please sign in to comment.