From 603dbfd7f924ee588464b2798c2bac73f76db876 Mon Sep 17 00:00:00 2001 From: Rens Groothuijsen Date: Thu, 7 Aug 2025 03:38:07 +0200 Subject: [PATCH] [rust] Extend oneOf array enum names with inner type (#21599) --- .../codegen/languages/RustClientCodegen.java | 10 ++++++- .../codegen/rust/RustClientCodegenTest.java | 28 +++++++++++++++++++ .../src/test/resources/3_1/issue_18527.yaml | 19 +++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/resources/3_1/issue_18527.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java index 6e1c952f2a4..5ce20d8fb8b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java @@ -288,7 +288,15 @@ public class RustClientCodegen extends AbstractRustCodegen implements CodegenCon oneOf.setName(modelName); oneOf.setBaseName(refName); } - } else { + } else if (oneOf.isArray) { + // If the type is an array, extend the name with the inner type to prevent name collisions + // in case multiple arrays with different types are defined. If the user has manually specified + // a name, use that name instead. + String collectionWithTypeName = toModelName(schema.getType()) + oneOf.containerTypeMapped + oneOf.items.dataType; + String oneOfName = Optional.ofNullable(schema.getTitle()).orElse(collectionWithTypeName); + oneOf.setName(oneOfName); + } + else { // In-placed type (primitive), because there is no mapping or ref for it. // use camelized `title` if present, otherwise use `type` String oneOfName = Optional.ofNullable(schema.getTitle()).orElseGet(schema::getType); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/rust/RustClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/rust/RustClientCodegenTest.java index 4513fae19ff..c092ad3d7ae 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/rust/RustClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/rust/RustClientCodegenTest.java @@ -19,11 +19,21 @@ package org.openapitools.codegen.rust; import io.swagger.v3.oas.models.media.IntegerSchema; import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.DefaultGenerator; +import org.openapitools.codegen.TestUtils; +import org.openapitools.codegen.config.CodegenConfigurator; import org.openapitools.codegen.languages.RustClientCodegen; import org.testng.Assert; import org.testng.annotations.Test; +import java.io.File; +import java.io.IOException; import java.math.BigDecimal; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.openapitools.codegen.TestUtils.linearize; public class RustClientCodegenTest { @@ -243,4 +253,22 @@ public class RustClientCodegenTest { Assert.assertEquals(codegen.getSchemaType(s), "i64"); } + @Test + public void testMultipleArrayTypesEnum() throws IOException { + Path target = Files.createTempDirectory("test"); + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("rust") + .setInputSpec("src/test/resources/3_1/issue_18527.yaml") + .setSkipOverwrite(false) + .setOutputDir(target.toAbsolutePath().toString().replace("\\", "/")); + List files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate(); + files.forEach(File::deleteOnExit); + Path outputPath = Path.of(target.toString(), "/src/models/option1_or_option2_options.rs"); + String enumSpec = linearize("pub enum Option1OrOption2Options { " + + "ArrayVecString(Vec), " + + "ArrayVeci32(Vec)," + + "}"); + TestUtils.assertFileExists(outputPath); + TestUtils.assertFileContains(outputPath, enumSpec); + } } diff --git a/modules/openapi-generator/src/test/resources/3_1/issue_18527.yaml b/modules/openapi-generator/src/test/resources/3_1/issue_18527.yaml new file mode 100644 index 00000000000..01a7c35fc12 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_1/issue_18527.yaml @@ -0,0 +1,19 @@ +openapi: 3.1.0 +info: + title: oneOf with arrays + description: Ensure different names for kinds in enum when using oneOf with arrays. + version: 1.0.0 +paths: {} +components: + schemas: + Option1OrOption2: + type: object + properties: + Options: + oneOf: + - type: array + items: + type: string + - type: array + items: + type: integer \ No newline at end of file