Skip to content

Commit 603dbfd

Browse files
[rust] Extend oneOf array enum names with inner type (#21599)
1 parent e2652f1 commit 603dbfd

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,15 @@ public CodegenModel fromModel(String name, Schema model) {
288288
oneOf.setName(modelName);
289289
oneOf.setBaseName(refName);
290290
}
291-
} else {
291+
} else if (oneOf.isArray) {
292+
// If the type is an array, extend the name with the inner type to prevent name collisions
293+
// in case multiple arrays with different types are defined. If the user has manually specified
294+
// a name, use that name instead.
295+
String collectionWithTypeName = toModelName(schema.getType()) + oneOf.containerTypeMapped + oneOf.items.dataType;
296+
String oneOfName = Optional.ofNullable(schema.getTitle()).orElse(collectionWithTypeName);
297+
oneOf.setName(oneOfName);
298+
}
299+
else {
292300
// In-placed type (primitive), because there is no mapping or ref for it.
293301
// use camelized `title` if present, otherwise use `type`
294302
String oneOfName = Optional.ofNullable(schema.getTitle()).orElseGet(schema::getType);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@
1919

2020
import io.swagger.v3.oas.models.media.IntegerSchema;
2121
import org.openapitools.codegen.CodegenConstants;
22+
import org.openapitools.codegen.DefaultGenerator;
23+
import org.openapitools.codegen.TestUtils;
24+
import org.openapitools.codegen.config.CodegenConfigurator;
2225
import org.openapitools.codegen.languages.RustClientCodegen;
2326
import org.testng.Assert;
2427
import org.testng.annotations.Test;
2528

29+
import java.io.File;
30+
import java.io.IOException;
2631
import java.math.BigDecimal;
32+
import java.nio.file.Files;
33+
import java.nio.file.Path;
34+
import java.util.List;
35+
36+
import static org.openapitools.codegen.TestUtils.linearize;
2737

2838
public class RustClientCodegenTest {
2939

@@ -243,4 +253,22 @@ public void testWithIntegerFittingAndPreferUnsigned() {
243253
Assert.assertEquals(codegen.getSchemaType(s), "i64");
244254
}
245255

256+
@Test
257+
public void testMultipleArrayTypesEnum() throws IOException {
258+
Path target = Files.createTempDirectory("test");
259+
final CodegenConfigurator configurator = new CodegenConfigurator()
260+
.setGeneratorName("rust")
261+
.setInputSpec("src/test/resources/3_1/issue_18527.yaml")
262+
.setSkipOverwrite(false)
263+
.setOutputDir(target.toAbsolutePath().toString().replace("\\", "/"));
264+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
265+
files.forEach(File::deleteOnExit);
266+
Path outputPath = Path.of(target.toString(), "/src/models/option1_or_option2_options.rs");
267+
String enumSpec = linearize("pub enum Option1OrOption2Options { " +
268+
"ArrayVecString(Vec<String>), " +
269+
"ArrayVeci32(Vec<i32>)," +
270+
"}");
271+
TestUtils.assertFileExists(outputPath);
272+
TestUtils.assertFileContains(outputPath, enumSpec);
273+
}
246274
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
openapi: 3.1.0
2+
info:
3+
title: oneOf with arrays
4+
description: Ensure different names for kinds in enum when using oneOf with arrays.
5+
version: 1.0.0
6+
paths: {}
7+
components:
8+
schemas:
9+
Option1OrOption2:
10+
type: object
11+
properties:
12+
Options:
13+
oneOf:
14+
- type: array
15+
items:
16+
type: string
17+
- type: array
18+
items:
19+
type: integer

0 commit comments

Comments
 (0)