Skip to content

Commit 40e5b79

Browse files
ranger-rosskgeilmann
authored andcommitted
[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 d5e6a41 commit 40e5b79

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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)
Lines changed: 11 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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()); },
Lines changed: 27 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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!");

0 commit comments

Comments
 (0)