Skip to content

Commit 9ed15a1

Browse files
authored
[Rust] Fixed Rust default isAnyType causing compiler issues. (#20631)
* Fixed Rust default `isAnyType` causing compiler issues. * Added tests for Rust isAnyType's * Fixed thread_test.rs
1 parent 21bf477 commit 9ed15a1

File tree

73 files changed

+500
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+500
-20
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,18 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
633633
List<CodegenOperation> operations = objectMap.getOperation();
634634
for (CodegenOperation operation : operations) {
635635
if (operation.pathParams != null && operation.pathParams.size() > 0) {
636+
637+
// For types with `isAnyType` we assume it's a `serde_json::Value` type.
638+
// However for path, query, and headers it's unlikely to be JSON so we default to `String`.
639+
// Note that we keep the default `serde_json::Value` for body parameters.
640+
for (var param : operation.allParams) {
641+
if (param.isAnyType && (param.isPathParam || param.isQueryParam || param.isHeaderParam)) {
642+
param.dataType = "String";
643+
param.isPrimitiveType = true;
644+
param.isString = true;
645+
}
646+
}
647+
636648
for (var pathParam : operation.pathParams) {
637649
if (!pathParam.baseName.contains("-")) {
638650
continue;

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

+11
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ paths:
615615
description: To test escaping of parameters in rust code works
616616
schema:
617617
type: string
618+
# For testing `isAnyType` types
619+
- name: anyType
620+
in: query
621+
required: true
622+
schema: {}
618623
responses:
619624
'200':
620625
description: successful operation
@@ -996,3 +1001,9 @@ components:
9961001
properties:
9971002
dummy:
9981003
type: string
1004+
AnyTypeTest:
1005+
type: object
1006+
properties:
1007+
foo: {}
1008+
required: ["foo"]
1009+

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

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Cargo.toml
44
README.md
55
docs/ActionContainer.md
6+
docs/AnyTypeTest.md
67
docs/ApiResponse.md
78
docs/ArrayItemRefTest.md
89
docs/Baz.md
@@ -37,6 +38,7 @@ src/apis/testing_api.rs
3738
src/apis/user_api.rs
3839
src/lib.rs
3940
src/models/action_container.rs
41+
src/models/any_type_test.rs
4042
src/models/api_response.rs
4143
src/models/array_item_ref_test.rs
4244
src/models/baz.rs

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

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Class | Method | HTTP request | Description
5454
## Documentation For Models
5555

5656
- [ActionContainer](docs/ActionContainer.md)
57+
- [AnyTypeTest](docs/AnyTypeTest.md)
5758
- [ApiResponse](docs/ApiResponse.md)
5859
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
5960
- [Baz](docs/Baz.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# AnyTypeTest
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**foo** | Option<[**serde_json::Value**](.md)> | |
8+
9+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
10+
11+

samples/client/petstore/rust/hyper/petstore/docs/FakeApi.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Method | HTTP request | Description
1010

1111
## test_nullable_required_param
1212

13-
> test_nullable_required_param(user_name, dummy_required_nullable_param, uppercase, content)
13+
> test_nullable_required_param(user_name, dummy_required_nullable_param, any_type, uppercase, content)
1414
To test nullable required parameters
1515

1616

@@ -22,6 +22,7 @@ Name | Type | Description | Required | Notes
2222
------------- | ------------- | ------------- | ------------- | -------------
2323
**user_name** | **String** | The name that needs to be fetched. Use user1 for testing. | [required] |
2424
**dummy_required_nullable_param** | Option<**String**> | To test nullable required parameters | [required] |
25+
**any_type** | **String** | | [required] |
2526
**uppercase** | Option<**String**> | To test parameter names in upper case | |
2627
**content** | Option<**String**> | To test escaping of parameters in rust code works | |
2728

samples/client/petstore/rust/hyper/petstore/src/apis/fake_api.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,20 @@ impl<C: Connect> FakeApiClient<C>
3737
}
3838

3939
pub trait FakeApi: Send + Sync {
40-
fn test_nullable_required_param(&self, user_name: &str, dummy_required_nullable_param: Option<&str>, uppercase: Option<&str>, content: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
40+
fn test_nullable_required_param(&self, user_name: &str, dummy_required_nullable_param: Option<&str>, any_type: &str, uppercase: Option<&str>, content: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
4141
}
4242

4343
impl<C: Connect>FakeApi for FakeApiClient<C>
4444
where C: Clone + std::marker::Send + Sync {
4545
#[allow(unused_mut)]
46-
fn test_nullable_required_param(&self, user_name: &str, dummy_required_nullable_param: Option<&str>, uppercase: Option<&str>, content: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> {
46+
fn test_nullable_required_param(&self, user_name: &str, dummy_required_nullable_param: Option<&str>, any_type: &str, uppercase: Option<&str>, content: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> {
4747
let mut req = __internal_request::Request::new(hyper::Method::GET, "/fake/user/{user_name}".to_string())
4848
;
4949
if let Some(ref s) = content {
5050
let query_value = s.to_string();
5151
req = req.with_query_param("content".to_string(), query_value);
5252
}
53+
req = req.with_query_param("anyType".to_string(), any_type.to_string());
5354
req = req.with_path_param("user_name".to_string(), user_name.to_string());
5455
match dummy_required_nullable_param {
5556
Some(param_value) => { req = req.with_header_param("dummy_required_nullable_param".to_string(), param_value.to_string()); },
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
use crate::models;
12+
use serde::{Deserialize, Serialize};
13+
14+
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
15+
pub struct AnyTypeTest {
16+
#[serde(rename = "foo", deserialize_with = "Option::deserialize")]
17+
pub foo: Option<serde_json::Value>,
18+
}
19+
20+
impl AnyTypeTest {
21+
pub fn new(foo: Option<serde_json::Value>) -> AnyTypeTest {
22+
AnyTypeTest {
23+
foo,
24+
}
25+
}
26+
}
27+

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub mod action_container;
22
pub use self::action_container::ActionContainer;
3+
pub mod any_type_test;
4+
pub use self::any_type_test::AnyTypeTest;
35
pub mod api_response;
46
pub use self::api_response::ApiResponse;
57
pub mod array_item_ref_test;

samples/client/petstore/rust/hyper/petstore/tests/thread_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod tests {
1111
let handle = thread::spawn(move || {
1212
let _ = client
1313
.fake_api()
14-
.test_nullable_required_param("username", None, None, None);
14+
.test_nullable_required_param("username", None, "any_type", None, None);
1515
});
1616

1717
handle.join().expect("Thread panicked!");

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

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Cargo.toml
44
README.md
55
docs/ActionContainer.md
6+
docs/AnyTypeTest.md
67
docs/ApiResponse.md
78
docs/ArrayItemRefTest.md
89
docs/Baz.md
@@ -35,6 +36,7 @@ src/apis/testing_api.rs
3536
src/apis/user_api.rs
3637
src/lib.rs
3738
src/models/action_container.rs
39+
src/models/any_type_test.rs
3840
src/models/api_response.rs
3941
src/models/array_item_ref_test.rs
4042
src/models/baz.rs

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

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Class | Method | HTTP request | Description
5454
## Documentation For Models
5555

5656
- [ActionContainer](docs/ActionContainer.md)
57+
- [AnyTypeTest](docs/AnyTypeTest.md)
5758
- [ApiResponse](docs/ApiResponse.md)
5859
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
5960
- [Baz](docs/Baz.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# AnyTypeTest
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**foo** | Option<[**serde_json::Value**](.md)> | |
8+
9+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
10+
11+

samples/client/petstore/rust/hyper0x/petstore/docs/FakeApi.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Method | HTTP request | Description
1010

1111
## test_nullable_required_param
1212

13-
> test_nullable_required_param(user_name, dummy_required_nullable_param, uppercase, content)
13+
> test_nullable_required_param(user_name, dummy_required_nullable_param, any_type, uppercase, content)
1414
To test nullable required parameters
1515

1616

@@ -22,6 +22,7 @@ Name | Type | Description | Required | Notes
2222
------------- | ------------- | ------------- | ------------- | -------------
2323
**user_name** | **String** | The name that needs to be fetched. Use user1 for testing. | [required] |
2424
**dummy_required_nullable_param** | Option<**String**> | To test nullable required parameters | [required] |
25+
**any_type** | **String** | | [required] |
2526
**uppercase** | Option<**String**> | To test parameter names in upper case | |
2627
**content** | Option<**String**> | To test escaping of parameters in rust code works | |
2728

samples/client/petstore/rust/hyper0x/petstore/src/apis/fake_api.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,20 @@ impl<C: hyper::client::connect::Connect> FakeApiClient<C>
3636
}
3737

3838
pub trait FakeApi {
39-
fn test_nullable_required_param(&self, user_name: &str, dummy_required_nullable_param: Option<&str>, uppercase: Option<&str>, content: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>>>>;
39+
fn test_nullable_required_param(&self, user_name: &str, dummy_required_nullable_param: Option<&str>, any_type: &str, uppercase: Option<&str>, content: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>>>>;
4040
}
4141

4242
impl<C: hyper::client::connect::Connect>FakeApi for FakeApiClient<C>
4343
where C: Clone + std::marker::Send + Sync {
4444
#[allow(unused_mut)]
45-
fn test_nullable_required_param(&self, user_name: &str, dummy_required_nullable_param: Option<&str>, uppercase: Option<&str>, content: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>>>> {
45+
fn test_nullable_required_param(&self, user_name: &str, dummy_required_nullable_param: Option<&str>, any_type: &str, uppercase: Option<&str>, content: Option<&str>) -> Pin<Box<dyn Future<Output = Result<(), Error>>>> {
4646
let mut req = __internal_request::Request::new(hyper::Method::GET, "/fake/user/{user_name}".to_string())
4747
;
4848
if let Some(ref s) = content {
4949
let query_value = s.to_string();
5050
req = req.with_query_param("content".to_string(), query_value);
5151
}
52+
req = req.with_query_param("anyType".to_string(), any_type.to_string());
5253
req = req.with_path_param("user_name".to_string(), user_name.to_string());
5354
match dummy_required_nullable_param {
5455
Some(param_value) => { req = req.with_header_param("dummy_required_nullable_param".to_string(), param_value.to_string()); },
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
use crate::models;
12+
use serde::{Deserialize, Serialize};
13+
14+
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
15+
pub struct AnyTypeTest {
16+
#[serde(rename = "foo", deserialize_with = "Option::deserialize")]
17+
pub foo: Option<serde_json::Value>,
18+
}
19+
20+
impl AnyTypeTest {
21+
pub fn new(foo: Option<serde_json::Value>) -> AnyTypeTest {
22+
AnyTypeTest {
23+
foo,
24+
}
25+
}
26+
}
27+

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub mod action_container;
22
pub use self::action_container::ActionContainer;
3+
pub mod any_type_test;
4+
pub use self::any_type_test::AnyTypeTest;
35
pub mod api_response;
46
pub use self::api_response::ApiResponse;
57
pub mod array_item_ref_test;

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

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Cargo.toml
44
README.md
55
docs/ActionContainer.md
6+
docs/AnyTypeTest.md
67
docs/ApiResponse.md
78
docs/ArrayItemRefTest.md
89
docs/Baz.md
@@ -35,6 +36,7 @@ src/apis/testing_api.rs
3536
src/apis/user_api.rs
3637
src/lib.rs
3738
src/models/action_container.rs
39+
src/models/any_type_test.rs
3840
src/models/api_response.rs
3941
src/models/array_item_ref_test.rs
4042
src/models/baz.rs

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

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Class | Method | HTTP request | Description
5454
## Documentation For Models
5555

5656
- [ActionContainer](docs/ActionContainer.md)
57+
- [AnyTypeTest](docs/AnyTypeTest.md)
5758
- [ApiResponse](docs/ApiResponse.md)
5859
- [ArrayItemRefTest](docs/ArrayItemRefTest.md)
5960
- [Baz](docs/Baz.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# AnyTypeTest
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**foo** | Option<[**serde_json::Value**](.md)> | |
8+
9+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
10+
11+

samples/client/petstore/rust/reqwest-trait/petstore/docs/FakeApi.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Method | HTTP request | Description
1010

1111
## test_nullable_required_param
1212

13-
> test_nullable_required_param(user_name, dummy_required_nullable_param, uppercase, content)
13+
> test_nullable_required_param(user_name, dummy_required_nullable_param, any_type, uppercase, content)
1414
To test nullable required parameters
1515

1616

@@ -22,6 +22,7 @@ Name | Type | Description | Required | Notes
2222
------------- | ------------- | ------------- | ------------- | -------------
2323
**user_name** | **String** | The name that needs to be fetched. Use user1 for testing. | [required] |
2424
**dummy_required_nullable_param** | Option<**String**> | To test nullable required parameters | [required] |
25+
**any_type** | **String** | | [required] |
2526
**uppercase** | Option<**String**> | To test parameter names in upper case | |
2627
**content** | Option<**String**> | To test escaping of parameters in rust code works | |
2728

samples/client/petstore/rust/reqwest-trait/petstore/src/apis/fake_api.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::{Error, configuration};
2323
pub trait FakeApi: Send + Sync {
2424

2525
/// GET /fake/user/{user_name}
26-
async fn test_nullable_required_param<'user_name, 'dummy_required_nullable_param, 'uppercase, 'content>(&self, user_name: &'user_name str, dummy_required_nullable_param: Option<&'dummy_required_nullable_param str>, uppercase: Option<&'uppercase str>, content: Option<&'content str>) -> Result<(), Error<TestNullableRequiredParamError>>;
26+
async fn test_nullable_required_param<'user_name, 'dummy_required_nullable_param, 'any_type, 'uppercase, 'content>(&self, user_name: &'user_name str, dummy_required_nullable_param: Option<&'dummy_required_nullable_param str>, any_type: &'any_type str, uppercase: Option<&'uppercase str>, content: Option<&'content str>) -> Result<(), Error<TestNullableRequiredParamError>>;
2727
}
2828

2929
pub struct FakeApiClient {
@@ -41,7 +41,7 @@ impl FakeApiClient {
4141
#[async_trait]
4242
impl FakeApi for FakeApiClient {
4343
///
44-
async fn test_nullable_required_param<'user_name, 'dummy_required_nullable_param, 'uppercase, 'content>(&self, user_name: &'user_name str, dummy_required_nullable_param: Option<&'dummy_required_nullable_param str>, uppercase: Option<&'uppercase str>, content: Option<&'content str>) -> Result<(), Error<TestNullableRequiredParamError>> {
44+
async fn test_nullable_required_param<'user_name, 'dummy_required_nullable_param, 'any_type, 'uppercase, 'content>(&self, user_name: &'user_name str, dummy_required_nullable_param: Option<&'dummy_required_nullable_param str>, any_type: &'any_type str, uppercase: Option<&'uppercase str>, content: Option<&'content str>) -> Result<(), Error<TestNullableRequiredParamError>> {
4545
let local_var_configuration = &self.configuration;
4646

4747
let local_var_client = &local_var_configuration.client;
@@ -52,6 +52,7 @@ impl FakeApi for FakeApiClient {
5252
if let Some(ref local_var_str) = content {
5353
local_var_req_builder = local_var_req_builder.query(&[("content", &local_var_str.to_string())]);
5454
}
55+
local_var_req_builder = local_var_req_builder.query(&[("anyType", &any_type.to_string())]);
5556
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
5657
local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
5758
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
use crate::models;
12+
use serde::{Deserialize, Serialize};
13+
14+
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
15+
pub struct AnyTypeTest {
16+
#[serde(rename = "foo", deserialize_with = "Option::deserialize")]
17+
pub foo: Option<serde_json::Value>,
18+
}
19+
20+
impl AnyTypeTest {
21+
pub fn new(foo: Option<serde_json::Value>) -> AnyTypeTest {
22+
AnyTypeTest {
23+
foo,
24+
}
25+
}
26+
}
27+

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub mod action_container;
22
pub use self::action_container::ActionContainer;
3+
pub mod any_type_test;
4+
pub use self::any_type_test::AnyTypeTest;
35
pub mod api_response;
46
pub use self::api_response::ApiResponse;
57
pub mod array_item_ref_test;

0 commit comments

Comments
 (0)