Skip to content

Commit 191dc1a

Browse files
authored
Override escape reserved word in abstract rust (#17440)
* override escape reserved word in abstract rust * add tests for ref, improve verion lambda * add files
1 parent b7ea139 commit 191dc1a

33 files changed

Lines changed: 285 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,12 @@ public String toApiDocFilename(String name) {
436436
public String addRegularExpressionDelimiter(String pattern) {
437437
return pattern;
438438
}
439+
440+
@Override
441+
public String escapeReservedWord(String name) {
442+
if (this.reservedWordsMappings().containsKey(name)) {
443+
return this.reservedWordsMappings().get(name);
444+
}
445+
return "r#"+ name;
446+
}
439447
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,13 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
392392
// remove v or V
393393
content = content.trim().replace("v", "");
394394
content = content.replace("V", "");
395+
396+
// convert 5.2 to 5.2.0 for example
397+
String[] contents = content.split("[.]");
398+
if (contents.length == 2) {
399+
content += ".0";
400+
}
401+
395402
writer.write(content);
396403
}
397404
});

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,3 +955,9 @@ components:
955955
nullable: true
956956
just_string:
957957
type: string
958+
ref:
959+
description: using reserved word as model name
960+
type: object
961+
properties:
962+
dummy:
963+
type: string

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ docs/Order.md
1515
docs/Pet.md
1616
docs/PetApi.md
1717
docs/PropertyTest.md
18+
docs/Ref.md
1819
docs/Return.md
1920
docs/StoreApi.md
2021
docs/Tag.md
@@ -41,6 +42,7 @@ src/models/baz.rs
4142
src/models/category.rs
4243
src/models/enum_array_testing.rs
4344
src/models/mod.rs
45+
src/models/model_ref.rs
4446
src/models/model_return.rs
4547
src/models/nullable_array.rs
4648
src/models/optional_testing.rs

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Class | Method | HTTP request | Description
6464
- [Order](docs/Order.md)
6565
- [Pet](docs/Pet.md)
6666
- [PropertyTest](docs/PropertyTest.md)
67+
- [Ref](docs/Ref.md)
6768
- [Return](docs/Return.md)
6869
- [Tag](docs/Tag.md)
6970
- [TypeTesting](docs/TypeTesting.md)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Ref
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**dummy** | Option<**String**> | | [optional]
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/src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub mod pet;
2020
pub use self::pet::Pet;
2121
pub mod property_test;
2222
pub use self::property_test::PropertyTest;
23+
pub mod model_ref;
24+
pub use self::model_ref::Ref;
2325
pub mod model_return;
2426
pub use self::model_return::Return;
2527
pub mod tag;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
13+
/// Ref : using reserved word as model name
14+
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
15+
pub struct Ref {
16+
#[serde(rename = "dummy", skip_serializing_if = "Option::is_none")]
17+
pub dummy: Option<String>,
18+
}
19+
20+
impl Ref {
21+
/// using reserved word as model name
22+
pub fn new() -> Ref {
23+
Ref {
24+
dummy: None,
25+
}
26+
}
27+
}
28+

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
@@ -15,6 +15,7 @@ docs/Order.md
1515
docs/Pet.md
1616
docs/PetApi.md
1717
docs/PropertyTest.md
18+
docs/Ref.md
1819
docs/Return.md
1920
docs/StoreApi.md
2021
docs/Tag.md
@@ -39,6 +40,7 @@ src/models/baz.rs
3940
src/models/category.rs
4041
src/models/enum_array_testing.rs
4142
src/models/mod.rs
43+
src/models/model_ref.rs
4244
src/models/model_return.rs
4345
src/models/nullable_array.rs
4446
src/models/optional_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
@@ -64,6 +64,7 @@ Class | Method | HTTP request | Description
6464
- [Order](docs/Order.md)
6565
- [Pet](docs/Pet.md)
6666
- [PropertyTest](docs/PropertyTest.md)
67+
- [Ref](docs/Ref.md)
6768
- [Return](docs/Return.md)
6869
- [Tag](docs/Tag.md)
6970
- [TypeTesting](docs/TypeTesting.md)

0 commit comments

Comments
 (0)