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

+3-3
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

+1-1
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

+1-1
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

+18
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

+4-4
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

+1-1
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

+3-3
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

+2-2
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

+1-1
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

+1-1
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());

samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,26 @@ paths:
360360
responses:
361361
"200":
362362
description: Success
363+
/repos/{repoId}:
364+
get:
365+
operationId: GetRepoInfo
366+
parameters:
367+
- explode: false
368+
in: path
369+
name: repoId
370+
required: true
371+
schema:
372+
type: string
373+
style: simple
374+
responses:
375+
"200":
376+
content:
377+
application/json:
378+
schema:
379+
$ref: '#/components/schemas/StringObject'
380+
description: OK
381+
tags:
382+
- Repo
363383
components:
364384
schemas:
365385
EnumWithStarObject:

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

+31-23
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ use openapi_v3::{Api, ApiNoContext, Client, ContextWrapperExt,
4949
XmlOtherPostResponse,
5050
XmlOtherPutResponse,
5151
XmlPostResponse,
52-
XmlPutResponse
52+
XmlPutResponse,
53+
GetRepoInfoResponse
5354
};
5455
use clap::{App, Arg};
5556

@@ -86,6 +87,7 @@ fn main() {
8687
"XmlOtherPut",
8788
"XmlPost",
8889
"XmlPut",
90+
"GetRepoInfo",
8991
])
9092
.required(true)
9193
.index(1))
@@ -100,7 +102,7 @@ fn main() {
100102
.arg(Arg::with_name("port")
101103
.long("port")
102104
.takes_value(true)
103-
.default_value("80")
105+
.default_value("8080")
104106
.help("Port to contact"))
105107
.get_matches();
106108

@@ -136,123 +138,129 @@ fn main() {
136138
let result = rt.block_on(client.callback_with_header_post(
137139
"url_example".to_string()
138140
));
139-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
141+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
140142
},
141143
Some("ComplexQueryParamGet") => {
142144
let result = rt.block_on(client.complex_query_param_get(
143145
Some(&Vec::new())
144146
));
145-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
147+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
146148
},
147149
/* Disabled because there's no example.
148150
Some("EnumInPathPathParamGet") => {
149151
let result = rt.block_on(client.enum_in_path_path_param_get(
150152
???
151153
));
152-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
154+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
153155
},
154156
*/
155157
Some("MandatoryRequestHeaderGet") => {
156158
let result = rt.block_on(client.mandatory_request_header_get(
157159
"x_header_example".to_string()
158160
));
159-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
161+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
160162
},
161163
Some("MergePatchJsonGet") => {
162164
let result = rt.block_on(client.merge_patch_json_get(
163165
));
164-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
166+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
165167
},
166168
Some("MultigetGet") => {
167169
let result = rt.block_on(client.multiget_get(
168170
));
169-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
171+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
170172
},
171173
Some("MultipleAuthSchemeGet") => {
172174
let result = rt.block_on(client.multiple_auth_scheme_get(
173175
));
174-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
176+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
175177
},
176178
Some("OverrideServerGet") => {
177179
let result = rt.block_on(client.override_server_get(
178180
));
179-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
181+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
180182
},
181183
Some("ParamgetGet") => {
182184
let result = rt.block_on(client.paramget_get(
183185
Some(serde_json::from_str::<uuid::Uuid>("38400000-8cf0-11bd-b23e-10b96e4ef00d").expect("Failed to parse JSON example")),
184186
None,
185187
None
186188
));
187-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
189+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
188190
},
189191
Some("ReadonlyAuthSchemeGet") => {
190192
let result = rt.block_on(client.readonly_auth_scheme_get(
191193
));
192-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
194+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
193195
},
194196
Some("RegisterCallbackPost") => {
195197
let result = rt.block_on(client.register_callback_post(
196198
"url_example".to_string()
197199
));
198-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
200+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
199201
},
200202
Some("RequiredOctetStreamPut") => {
201203
let result = rt.block_on(client.required_octet_stream_put(
202204
swagger::ByteArray(Vec::from("BYTE_ARRAY_DATA_HERE"))
203205
));
204-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
206+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
205207
},
206208
Some("ResponsesWithHeadersGet") => {
207209
let result = rt.block_on(client.responses_with_headers_get(
208210
));
209-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
211+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
210212
},
211213
Some("Rfc7807Get") => {
212214
let result = rt.block_on(client.rfc7807_get(
213215
));
214-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
216+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
215217
},
216218
Some("UntypedPropertyGet") => {
217219
let result = rt.block_on(client.untyped_property_get(
218220
None
219221
));
220-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
222+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
221223
},
222224
Some("UuidGet") => {
223225
let result = rt.block_on(client.uuid_get(
224226
));
225-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
227+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
226228
},
227229
Some("XmlExtraPost") => {
228230
let result = rt.block_on(client.xml_extra_post(
229231
None
230232
));
231-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
233+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
232234
},
233235
Some("XmlOtherPost") => {
234236
let result = rt.block_on(client.xml_other_post(
235237
None
236238
));
237-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
239+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
238240
},
239241
Some("XmlOtherPut") => {
240242
let result = rt.block_on(client.xml_other_put(
241243
None
242244
));
243-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
245+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
244246
},
245247
Some("XmlPost") => {
246248
let result = rt.block_on(client.xml_post(
247249
None
248250
));
249-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
251+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
250252
},
251253
Some("XmlPut") => {
252254
let result = rt.block_on(client.xml_put(
253255
None
254256
));
255-
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
257+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
258+
},
259+
Some("GetRepoInfo") => {
260+
let result = rt.block_on(client.get_repo_info(
261+
"repo_id_example".to_string()
262+
));
263+
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &dyn Has<XSpanIdString>).get().clone());
256264
},
257265
_ => {
258266
panic!("Invalid operation provided")

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<C> CallbackApi<C> for Server<C> where C: Has<XSpanIdString>{
111111
&self,
112112
callback_request_query_url: String,
113113
information: Option<String>,
114-
context: &C) -> Box<Future<Item=CallbackCallbackWithHeaderPostResponse, Error=ApiError> + Send>
114+
context: &C) -> Box<dyn Future<Item=CallbackCallbackWithHeaderPostResponse, Error=ApiError> + Send>
115115
{
116116
let context = context.clone();
117117
info!("callback_callback_with_header_post({:?}) - X-Span-ID: {:?}", information, context.get().0.clone());
@@ -121,7 +121,7 @@ impl<C> CallbackApi<C> for Server<C> where C: Has<XSpanIdString>{
121121
fn callback_callback_post(
122122
&self,
123123
callback_request_query_url: String,
124-
context: &C) -> Box<Future<Item=CallbackCallbackPostResponse, Error=ApiError> + Send>
124+
context: &C) -> Box<dyn Future<Item=CallbackCallbackPostResponse, Error=ApiError> + Send>
125125
{
126126
let context = context.clone();
127127
info!("callback_callback_post() - X-Span-ID: {:?}", context.get().0.clone());

0 commit comments

Comments
 (0)