Skip to content

Commit c3837ca

Browse files
authored
[Rust Server] Fix #5906 (yaml with path parameter error) (#5871)
* [Rust Server] Fix Rust 1.39+ "Box<Future" and "as &Has" compile issue * [Rust Server] Fix Rust server side pathRegEx and baseName not match issue * [Rust Server] Add test case yaml with path parameter.
1 parent 95105ce commit c3837ca

File tree

27 files changed

+492
-246
lines changed

27 files changed

+492
-246
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
336336

337337
URL url = URLPathUtils.getServerURL(openAPI, serverVariableOverrides());
338338
additionalProperties.put("serverHost", url.getHost());
339-
additionalProperties.put("serverPort", URLPathUtils.getPort(url, 80));
339+
additionalProperties.put("serverPort", URLPathUtils.getPort(url, serverPort));
340340

341341
if (packageVersion == null || "".equals(packageVersion)) {
342342
List<String> versionComponents = new ArrayList<>(Arrays.asList(info.getVersion().split("[.]")));
@@ -706,9 +706,9 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
706706
// Don't prefix with '^' so that the templates can put the
707707
// basePath on the front.
708708
for (CodegenParameter param : op.pathParams) {
709-
// Replace {paramName} with (?P<paramName>[^/?#]*) for regex
709+
// Replace {baseName} with (?P<baseName>[^/?#]*) for regex
710710
String paramSearch = "{" + param.baseName + "}";
711-
String paramReplace = "(?P<" + param.paramName + ">[^/?#]*)";
711+
String paramReplace = "(?P<" + param.baseName + ">[^/?#]*)";
712712

713713
regex = regex.replace(paramSearch, paramReplace);
714714
}

modules/openapi-generator/src/main/resources/rust-server/example-client-main.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn main() {
126126
{{{vendorExtensions.example}}}{{^-last}},{{/-last}}
127127
{{/allParams}}
128128
));
129-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
129+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
130130
},
131131
{{#vendorExtensions}}
132132
{{#noClientExample}}

modules/openapi-generator/src/main/resources/rust-server/example-server-operation.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{{#allParams}}
1212
{{{paramName}}}: {{^required}}Option<{{/required}}{{#isListContainer}}&{{/isListContainer}}{{{dataType}}}{{^required}}>{{/required}},
1313
{{/allParams}}
14-
context: &C) -> Box<Future<Item={{{operationId}}}Response, Error=ApiError> + Send>
14+
context: &C) -> Box<dyn Future<Item={{{operationId}}}Response, Error=ApiError> + Send>
1515
{
1616
let context = context.clone();
1717
info!("{{#vendorExtensions}}{{{operation_id}}}{{/vendorExtensions}}({{#allParams}}{{#vendorExtensions}}{{{formatString}}}{{/vendorExtensions}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) - X-Span-ID: {:?}"{{#allParams}}, {{{paramName}}}{{/allParams}}, context.get().0.clone());

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,24 @@ paths:
359359
'200':
360360
description: Success
361361

362+
/repos/{repoId}:
363+
parameters:
364+
- in: path
365+
name: repoId
366+
schema:
367+
type: string
368+
required: true
369+
get:
370+
tags: [Repo]
371+
operationId: GetRepoInfo
372+
responses:
373+
"200":
374+
description: OK
375+
content:
376+
application/json:
377+
schema:
378+
$ref: "#/components/schemas/StringObject"
379+
362380
components:
363381
securitySchemes:
364382
authScheme:

samples/server/petstore/rust-server/output/multipart-v3/examples/client/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn main() {
5353
.arg(Arg::with_name("port")
5454
.long("port")
5555
.takes_value(true)
56-
.default_value("80")
56+
.default_value("8080")
5757
.help("Port to contact"))
5858
.get_matches();
5959

@@ -88,7 +88,7 @@ fn main() {
8888
None,
8989
Some(swagger::ByteArray(Vec::from("BINARY_DATA_HERE")))
9090
));
91-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
91+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
9292
},
9393
Some("MultipartRequestPost") => {
9494
let result = rt.block_on(client.multipart_request_post(
@@ -97,14 +97,14 @@ fn main() {
9797
Some("optional_string_field_example".to_string()),
9898
None
9999
));
100-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
100+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
101101
},
102102
Some("MultipleIdenticalMimeTypesPost") => {
103103
let result = rt.block_on(client.multiple_identical_mime_types_post(
104104
Some(swagger::ByteArray(Vec::from("BINARY_DATA_HERE"))),
105105
Some(swagger::ByteArray(Vec::from("BINARY_DATA_HERE")))
106106
));
107-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
107+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
108108
},
109109
_ => {
110110
panic!("Invalid operation provided")

samples/server/petstore/rust-server/output/multipart-v3/examples/server/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn main() {
4242
.help("Whether to use HTTPS or not"))
4343
.get_matches();
4444

45-
let addr = "127.0.0.1:80";
45+
let addr = "127.0.0.1:8080";
4646

4747
hyper::rt::run(server::create(addr, matches.is_present("https")));
4848
}

samples/server/petstore/rust-server/output/multipart-v3/examples/server/server.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<C> Api<C> for Server<C> where C: Has<XSpanIdString>{
117117
required_binary_field: swagger::ByteArray,
118118
object_field: Option<models::MultipartRequestObjectField>,
119119
optional_binary_field: Option<swagger::ByteArray>,
120-
context: &C) -> Box<Future<Item=MultipartRelatedRequestPostResponse, Error=ApiError> + Send>
120+
context: &C) -> Box<dyn Future<Item=MultipartRelatedRequestPostResponse, Error=ApiError> + Send>
121121
{
122122
let context = context.clone();
123123
info!("multipart_related_request_post({:?}, {:?}, {:?}) - X-Span-ID: {:?}", required_binary_field, object_field, optional_binary_field, context.get().0.clone());
@@ -130,7 +130,7 @@ impl<C> Api<C> for Server<C> where C: Has<XSpanIdString>{
130130
binary_field: swagger::ByteArray,
131131
optional_string_field: Option<String>,
132132
object_field: Option<models::MultipartRequestObjectField>,
133-
context: &C) -> Box<Future<Item=MultipartRequestPostResponse, Error=ApiError> + Send>
133+
context: &C) -> Box<dyn Future<Item=MultipartRequestPostResponse, Error=ApiError> + Send>
134134
{
135135
let context = context.clone();
136136
info!("multipart_request_post(\"{}\", {:?}, {:?}, {:?}) - X-Span-ID: {:?}", string_field, binary_field, optional_string_field, object_field, context.get().0.clone());
@@ -141,7 +141,7 @@ impl<C> Api<C> for Server<C> where C: Has<XSpanIdString>{
141141
&self,
142142
binary1: Option<swagger::ByteArray>,
143143
binary2: Option<swagger::ByteArray>,
144-
context: &C) -> Box<Future<Item=MultipleIdenticalMimeTypesPostResponse, Error=ApiError> + Send>
144+
context: &C) -> Box<dyn Future<Item=MultipleIdenticalMimeTypesPostResponse, Error=ApiError> + Send>
145145
{
146146
let context = context.clone();
147147
info!("multiple_identical_mime_types_post({:?}, {:?}) - X-Span-ID: {:?}", binary1, binary2, context.get().0.clone());

samples/server/petstore/rust-server/output/no-example-v3/examples/client/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn main() {
4848
.arg(Arg::with_name("port")
4949
.long("port")
5050
.takes_value(true)
51-
.default_value("80")
51+
.default_value("8080")
5252
.help("Port to contact"))
5353
.get_matches();
5454

@@ -82,7 +82,7 @@ fn main() {
8282
let result = rt.block_on(client.op_get(
8383
???
8484
));
85-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
85+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
8686
},
8787
*/
8888
_ => {

samples/server/petstore/rust-server/output/no-example-v3/examples/server/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn main() {
4242
.help("Whether to use HTTPS or not"))
4343
.get_matches();
4444

45-
let addr = "127.0.0.1:80";
45+
let addr = "127.0.0.1:8080";
4646

4747
hyper::rt::run(server::create(addr, matches.is_present("https")));
4848
}

samples/server/petstore/rust-server/output/no-example-v3/examples/server/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<C> Api<C> for Server<C> where C: Has<XSpanIdString>{
113113
fn op_get(
114114
&self,
115115
inline_object: models::InlineObject,
116-
context: &C) -> Box<Future<Item=OpGetResponse, Error=ApiError> + Send>
116+
context: &C) -> Box<dyn Future<Item=OpGetResponse, Error=ApiError> + Send>
117117
{
118118
let context = context.clone();
119119
info!("op_get({:?}) - X-Span-ID: {:?}", inline_object, context.get().0.clone());

0 commit comments

Comments
 (0)