Skip to content

Commit dda8e0e

Browse files
authored
[Rust] Use named lifetimes for UUID params in reqwest-trait (fix mockall build) (#24930)
The reqwest-trait template declares a lifetime for every parameter and uses it for string parameters, but UUID parameters were rendered as a bare `&str`. mockall's #[automock] cannot elide a reference nested in another type, so an optional UUID parameter (`Option<&str>`) failed to compile with E0106/E0637 when the mockall feature was enabled. Render UUID parameters as `&'<param> str` in both the trait declaration and the impl, matching the string branch.
1 parent c83886d commit dda8e0e

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎modules/openapi-generator/src/main/resources/rust/reqwest-trait/api.mustache‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub trait {{{classname}}}: Send + Sync {
4747
### &str and Vec<&str>
4848
}}{{#isString}}{{#isArray}}Vec<{{/isArray}}{{^isUuid}}&'{{#lambda.lifetimeName}}{{{paramName}}}{{/lambda.lifetimeName}} str{{/isUuid}}{{#isArray}}>{{/isArray}}{{/isString}}{{!
4949
### UUIDs
50-
}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}&str{{#isArray}}>{{/isArray}}{{/isUuid}}{{!
50+
}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}&'{{#lambda.lifetimeName}}{{{paramName}}}{{/lambda.lifetimeName}} str{{#isArray}}>{{/isArray}}{{/isUuid}}{{!
5151
### Models and primative types
5252
}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{!
5353
### Option End
@@ -155,7 +155,7 @@ impl {{classname}} for {{classname}}Client {
155155
### &str and Vec<&str>
156156
}}{{#isString}}{{#isArray}}Vec<{{/isArray}}{{^isUuid}}&'{{#lambda.lifetimeName}}{{{paramName}}}{{/lambda.lifetimeName}} str{{/isUuid}}{{#isArray}}>{{/isArray}}{{/isString}}{{!
157157
### UUIDs
158-
}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}&str{{#isArray}}>{{/isArray}}{{/isUuid}}{{!
158+
}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}&'{{#lambda.lifetimeName}}{{{paramName}}}{{/lambda.lifetimeName}} str{{#isArray}}>{{/isArray}}{{/isUuid}}{{!
159159
### Models and primative types
160160
}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{!
161161
### Option End

‎modules/openapi-generator/src/test/java/org/openapitools/codegen/rust/RustClientCodegenTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,24 @@ public void testArrayWithObjectEnumValues() throws IOException {
337337
TestUtils.assertFileExists(outputPath);
338338
TestUtils.assertFileContains(outputPath, enumSpec);
339339
}
340+
341+
@Test
342+
public void testReqwestTraitUuidParamsUseNamedLifetimes() throws IOException {
343+
Path target = Files.createTempDirectory("test");
344+
target.toFile().deleteOnExit();
345+
final CodegenConfigurator configurator = new CodegenConfigurator()
346+
.setGeneratorName("rust")
347+
.setLibrary("reqwest-trait")
348+
.addAdditionalProperty("mockall", true)
349+
.setInputSpec("src/test/resources/3_0/rust/reqwest-trait-uuid-params.yaml")
350+
.setSkipOverwrite(false)
351+
.setOutputDir(target.toAbsolutePath().toString().replace("\\", "/"));
352+
new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
353+
Path outputPath = Path.of(target.toString(), "/src/apis/widget_api.rs");
354+
TestUtils.assertFileExists(outputPath);
355+
// mockall's #[automock] cannot elide the lifetime of a reference nested in Option<..>
356+
TestUtils.assertFileContains(outputPath,
357+
"async fn list_widget_items<'id, 'run_id>(&self, id: &'id str, run_id: Option<&'run_id str>)");
358+
TestUtils.assertFileNotContains(outputPath, "Option<&str>");
359+
}
340360
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
openapi: 3.0.3
2+
info:
3+
title: UUID parameters with the reqwest-trait library
4+
description: >-
5+
Regression test for #24912. UUID parameters must use the named lifetime
6+
declared on the trait method (e.g. Option<&'run_id str>), otherwise
7+
mockall's #[automock] fails to compile.
8+
version: 1.0.0
9+
paths:
10+
/widgets/{id}/items:
11+
parameters:
12+
- name: id
13+
in: path
14+
required: true
15+
schema:
16+
type: string
17+
format: uuid
18+
get:
19+
operationId: listWidgetItems
20+
tags:
21+
- widget
22+
parameters:
23+
- name: run_id
24+
in: query
25+
required: false
26+
schema:
27+
type: string
28+
format: uuid
29+
responses:
30+
"204":
31+
description: No content

0 commit comments

Comments
 (0)