Skip to content

Commit db38f0f

Browse files
[rust-axum] Prevent multiple declarations of the same operation (#21396)
* [rust-axum] Prevent multiple declarations of the same operation * [rust-axum] Add missing "delete on exit" to duplicate declaration test
1 parent d5ab8f2 commit db38f0f

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
429429
CodegenOperation op = super.fromOperation(path, httpMethod, operation, servers);
430430

431431
String underscoredOperationId = underscore(op.operationId);
432+
ArrayList<MethodOperation> pathMethods = pathMethodOpMap.get(path);
433+
434+
// Prevent multiple declarations of the same operation
435+
if (pathMethods != null && pathMethods.stream().anyMatch(pathMethod ->
436+
pathMethod.operationID.equals(underscoredOperationId))) {
437+
return op;
438+
}
439+
432440
op.vendorExtensions.put("x-operation-id", underscoredOperationId);
433441
op.vendorExtensions.put("x-uppercase-operation-id", underscoredOperationId.toUpperCase(Locale.ROOT));
434442

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.openapitools.codegen.rust;
2+
3+
import org.openapitools.codegen.DefaultGenerator;
4+
import org.openapitools.codegen.TestUtils;
5+
import org.openapitools.codegen.config.CodegenConfigurator;
6+
import org.testng.annotations.Test;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.util.List;
13+
14+
import static org.openapitools.codegen.TestUtils.linearize;
15+
16+
public class RustAxumServerCodegenTest {
17+
@Test
18+
public void testPreventDuplicateOperationDeclaration() throws IOException {
19+
Path target = Files.createTempDirectory("test");
20+
final CodegenConfigurator configurator = new CodegenConfigurator()
21+
.setGeneratorName("rust-axum")
22+
.setInputSpec("src/test/resources/3_1/issue_21144.yaml")
23+
.setSkipOverwrite(false)
24+
.setOutputDir(target.toAbsolutePath().toString().replace("\\", "/"));
25+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
26+
files.forEach(File::deleteOnExit);
27+
Path outputPath = Path.of(target.toString(), "/src/server/mod.rs");
28+
String routerSpec = linearize("Router::new() " +
29+
".route(\"/api/test\", " +
30+
"delete(test_delete::<I, A, E, C>).post(test_post::<I, A, E, C>) ) " +
31+
".with_state(api_impl)");
32+
TestUtils.assertFileExists(outputPath);
33+
TestUtils.assertFileContains(outputPath, routerSpec);
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
title: "test"
4+
version: "0.1.1"
5+
description: "test"
6+
paths:
7+
/api/test:
8+
delete:
9+
operationId: "test_delete"
10+
description: "Delete method"
11+
responses:
12+
204:
13+
description: "delete"
14+
security:
15+
- apiKey: []
16+
post:
17+
tags:
18+
- firsttag
19+
- secondtag
20+
- thirdtag
21+
operationId: "test_post"
22+
description: "Post method"
23+
responses:
24+
201:
25+
description: "post"
26+
security:
27+
- apiKey: []
28+
components:
29+
securitySchemes:
30+
apiKey:
31+
type: apiKey
32+
name: X-API-Key
33+
in: header

0 commit comments

Comments
 (0)