Skip to content

Commit 88fa9ef

Browse files
authored
[rust] Fix declaration for arrays with object and array references (#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
1 parent 341a853 commit 88fa9ef

File tree

27 files changed

+282
-7
lines changed

27 files changed

+282
-7
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,27 +424,28 @@ public String modelDocFileFolder() {
424424

425425
@Override
426426
public String getTypeDeclaration(Schema p) {
427-
if (ModelUtils.isArraySchema(p)) {
428-
ArraySchema ap = (ArraySchema) p;
427+
Schema unaliasSchema = unaliasSchema(p);
428+
if (ModelUtils.isArraySchema(unaliasSchema)) {
429+
ArraySchema ap = (ArraySchema) unaliasSchema;
429430
Schema inner = ap.getItems();
430431
if (inner == null) {
431432
LOGGER.warn("{}(array property) does not have a proper inner type defined.Default to string",
432433
ap.getName());
433434
inner = new StringSchema().description("TODO default missing array inner type to string");
434435
}
435436
return "Vec<" + getTypeDeclaration(inner) + ">";
436-
} else if (ModelUtils.isMapSchema(p)) {
437-
Schema inner = getAdditionalProperties(p);
437+
} else if (ModelUtils.isMapSchema(unaliasSchema)) {
438+
Schema inner = getAdditionalProperties(unaliasSchema);
438439
if (inner == null) {
439-
LOGGER.warn("{}(map property) does not have a proper inner type defined. Default to string", p.getName());
440+
LOGGER.warn("{}(map property) does not have a proper inner type defined. Default to string", unaliasSchema.getName());
440441
inner = new StringSchema().description("TODO default missing map inner type to string");
441442
}
442443
return "::std::collections::HashMap<String, " + getTypeDeclaration(inner) + ">";
443444
}
444445

445446
// Not using the supertype invocation, because we want to UpperCamelize
446447
// the type.
447-
String schemaType = getSchemaType(p);
448+
String schemaType = getSchemaType(unaliasSchema);
448449
if (typeMapping.containsKey(schemaType)) {
449450
return typeMapping.get(schemaType);
450451
}

modules/openapi-generator/src/test/resources/3_0/rust/petstore.yaml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ components:
801801
properties:
802802
uuid:
803803
type: string
804-
format: uuid
804+
format: uuid
805805
ActionContainer:
806806
required:
807807
- action
@@ -889,3 +889,27 @@ components:
889889
items:
890890
type: string
891891
enum: ["A", "B", "C"]
892+
ArrayRefItem:
893+
description: Helper object for the array item ref test
894+
type: array
895+
items:
896+
type: string
897+
ObjectRefItem:
898+
description: Helper object for the array item ref test
899+
type: object
900+
additionalProperties: true
901+
ArrayItemRefTest:
902+
description: Test handling of object reference in arrays
903+
type: object
904+
required:
905+
- list_with_array_ref
906+
- list_with_object_ref
907+
properties:
908+
list_with_array_ref:
909+
type: array
910+
items:
911+
$ref: '#/components/schemas/ArrayRefItem'
912+
list_with_object_ref:
913+
type: array
914+
items:
915+
$ref: '#/components/schemas/ObjectRefItem'

samples/client/petstore/rust/hyper/petstore/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Cargo.toml
44
README.md
55
docs/ActionContainer.md
66
docs/ApiResponse.md
7+
docs/ArrayItemRefTest.md
78
docs/Baz.md
89
docs/Category.md
910
docs/EnumArrayTesting.md
@@ -33,6 +34,7 @@ src/apis/user_api.rs
3334
src/lib.rs
3435
src/models/action_container.rs
3536
src/models/api_response.rs
37+
src/models/array_item_ref_test.rs
3638
src/models/baz.rs
3739
src/models/category.rs
3840
src/models/enum_array_testing.rs

samples/client/petstore/rust/hyper/petstore/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Class | Method | HTTP request | Description
5454

5555
- [ActionContainer](docs/ActionContainer.md)
5656
- [ApiResponse](docs/ApiResponse.md)
57+
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
5758
- [Baz](docs/Baz.md)
5859
- [Category](docs/Category.md)
5960
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ArrayItemRefTest
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**list_with_array_ref** | [**Vec<Vec<String>>**](array.md) | |
8+
**list_with_object_ref** | [**Vec<::std::collections::HashMap<String, serde_json::Value>>**](map.md) | |
9+
10+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
11+
12+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
/// ArrayItemRefTest : Test handling of object reference in arrays
12+
13+
14+
15+
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
16+
pub struct ArrayItemRefTest {
17+
#[serde(rename = "list_with_array_ref")]
18+
pub list_with_array_ref: Vec<Vec<String>>,
19+
#[serde(rename = "list_with_object_ref")]
20+
pub list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>,
21+
}
22+
23+
impl ArrayItemRefTest {
24+
/// Test handling of object reference in arrays
25+
pub fn new(list_with_array_ref: Vec<Vec<String>>, list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>) -> ArrayItemRefTest {
26+
ArrayItemRefTest {
27+
list_with_array_ref,
28+
list_with_object_ref,
29+
}
30+
}
31+
}
32+
33+

samples/client/petstore/rust/hyper/petstore/src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ pub mod action_container;
22
pub use self::action_container::ActionContainer;
33
pub mod api_response;
44
pub use self::api_response::ApiResponse;
5+
pub mod array_item_ref_test;
6+
pub use self::array_item_ref_test::ArrayItemRefTest;
57
pub mod baz;
68
pub use self::baz::Baz;
79
pub mod category;

samples/client/petstore/rust/reqwest/petstore-async-middleware/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Cargo.toml
44
README.md
55
docs/ActionContainer.md
66
docs/ApiResponse.md
7+
docs/ArrayItemRefTest.md
78
docs/Baz.md
89
docs/Category.md
910
docs/EnumArrayTesting.md
@@ -31,6 +32,7 @@ src/apis/user_api.rs
3132
src/lib.rs
3233
src/models/action_container.rs
3334
src/models/api_response.rs
35+
src/models/array_item_ref_test.rs
3436
src/models/baz.rs
3537
src/models/category.rs
3638
src/models/enum_array_testing.rs

samples/client/petstore/rust/reqwest/petstore-async-middleware/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Class | Method | HTTP request | Description
5454

5555
- [ActionContainer](docs/ActionContainer.md)
5656
- [ApiResponse](docs/ApiResponse.md)
57+
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
5758
- [Baz](docs/Baz.md)
5859
- [Category](docs/Category.md)
5960
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ArrayItemRefTest
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**list_with_array_ref** | [**Vec<Vec<String>>**](array.md) | |
8+
**list_with_object_ref** | [**Vec<::std::collections::HashMap<String, serde_json::Value>>**](map.md) | |
9+
10+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
11+
12+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
/// ArrayItemRefTest : Test handling of object reference in arrays
12+
13+
14+
15+
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
16+
pub struct ArrayItemRefTest {
17+
#[serde(rename = "list_with_array_ref")]
18+
pub list_with_array_ref: Vec<Vec<String>>,
19+
#[serde(rename = "list_with_object_ref")]
20+
pub list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>,
21+
}
22+
23+
impl ArrayItemRefTest {
24+
/// Test handling of object reference in arrays
25+
pub fn new(list_with_array_ref: Vec<Vec<String>>, list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>) -> ArrayItemRefTest {
26+
ArrayItemRefTest {
27+
list_with_array_ref,
28+
list_with_object_ref,
29+
}
30+
}
31+
}
32+
33+

samples/client/petstore/rust/reqwest/petstore-async-middleware/src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ pub mod action_container;
22
pub use self::action_container::ActionContainer;
33
pub mod api_response;
44
pub use self::api_response::ApiResponse;
5+
pub mod array_item_ref_test;
6+
pub use self::array_item_ref_test::ArrayItemRefTest;
57
pub mod baz;
68
pub use self::baz::Baz;
79
pub mod category;

samples/client/petstore/rust/reqwest/petstore-async/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Cargo.toml
44
README.md
55
docs/ActionContainer.md
66
docs/ApiResponse.md
7+
docs/ArrayItemRefTest.md
78
docs/Baz.md
89
docs/Category.md
910
docs/EnumArrayTesting.md
@@ -31,6 +32,7 @@ src/apis/user_api.rs
3132
src/lib.rs
3233
src/models/action_container.rs
3334
src/models/api_response.rs
35+
src/models/array_item_ref_test.rs
3436
src/models/baz.rs
3537
src/models/category.rs
3638
src/models/enum_array_testing.rs

samples/client/petstore/rust/reqwest/petstore-async/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Class | Method | HTTP request | Description
5454

5555
- [ActionContainer](docs/ActionContainer.md)
5656
- [ApiResponse](docs/ApiResponse.md)
57+
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
5758
- [Baz](docs/Baz.md)
5859
- [Category](docs/Category.md)
5960
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ArrayItemRefTest
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**list_with_array_ref** | [**Vec<Vec<String>>**](array.md) | |
8+
**list_with_object_ref** | [**Vec<::std::collections::HashMap<String, serde_json::Value>>**](map.md) | |
9+
10+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
11+
12+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
/// ArrayItemRefTest : Test handling of object reference in arrays
12+
13+
14+
15+
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
16+
pub struct ArrayItemRefTest {
17+
#[serde(rename = "list_with_array_ref")]
18+
pub list_with_array_ref: Vec<Vec<String>>,
19+
#[serde(rename = "list_with_object_ref")]
20+
pub list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>,
21+
}
22+
23+
impl ArrayItemRefTest {
24+
/// Test handling of object reference in arrays
25+
pub fn new(list_with_array_ref: Vec<Vec<String>>, list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>) -> ArrayItemRefTest {
26+
ArrayItemRefTest {
27+
list_with_array_ref,
28+
list_with_object_ref,
29+
}
30+
}
31+
}
32+
33+

samples/client/petstore/rust/reqwest/petstore-async/src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ pub mod action_container;
22
pub use self::action_container::ActionContainer;
33
pub mod api_response;
44
pub use self::api_response::ApiResponse;
5+
pub mod array_item_ref_test;
6+
pub use self::array_item_ref_test::ArrayItemRefTest;
57
pub mod baz;
68
pub use self::baz::Baz;
79
pub mod category;

samples/client/petstore/rust/reqwest/petstore-awsv4signature/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Cargo.toml
44
README.md
55
docs/ActionContainer.md
66
docs/ApiResponse.md
7+
docs/ArrayItemRefTest.md
78
docs/Baz.md
89
docs/Category.md
910
docs/EnumArrayTesting.md
@@ -31,6 +32,7 @@ src/apis/user_api.rs
3132
src/lib.rs
3233
src/models/action_container.rs
3334
src/models/api_response.rs
35+
src/models/array_item_ref_test.rs
3436
src/models/baz.rs
3537
src/models/category.rs
3638
src/models/enum_array_testing.rs

samples/client/petstore/rust/reqwest/petstore-awsv4signature/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Class | Method | HTTP request | Description
5454

5555
- [ActionContainer](docs/ActionContainer.md)
5656
- [ApiResponse](docs/ApiResponse.md)
57+
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
5758
- [Baz](docs/Baz.md)
5859
- [Category](docs/Category.md)
5960
- [EnumArrayTesting](docs/EnumArrayTesting.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ArrayItemRefTest
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**list_with_array_ref** | [**Vec<Vec<String>>**](array.md) | |
8+
**list_with_object_ref** | [**Vec<::std::collections::HashMap<String, serde_json::Value>>**](map.md) | |
9+
10+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
11+
12+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
* Generated by: https://openapi-generator.tech
9+
*/
10+
11+
/// ArrayItemRefTest : Test handling of object reference in arrays
12+
13+
14+
15+
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
16+
pub struct ArrayItemRefTest {
17+
#[serde(rename = "list_with_array_ref")]
18+
pub list_with_array_ref: Vec<Vec<String>>,
19+
#[serde(rename = "list_with_object_ref")]
20+
pub list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>,
21+
}
22+
23+
impl ArrayItemRefTest {
24+
/// Test handling of object reference in arrays
25+
pub fn new(list_with_array_ref: Vec<Vec<String>>, list_with_object_ref: Vec<::std::collections::HashMap<String, serde_json::Value>>) -> ArrayItemRefTest {
26+
ArrayItemRefTest {
27+
list_with_array_ref,
28+
list_with_object_ref,
29+
}
30+
}
31+
}
32+
33+

samples/client/petstore/rust/reqwest/petstore-awsv4signature/src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ pub mod action_container;
22
pub use self::action_container::ActionContainer;
33
pub mod api_response;
44
pub use self::api_response::ApiResponse;
5+
pub mod array_item_ref_test;
6+
pub use self::array_item_ref_test::ArrayItemRefTest;
57
pub mod baz;
68
pub use self::baz::Baz;
79
pub mod category;

0 commit comments

Comments
 (0)