[rust] Extend oneOf array enum names with inner type (#21599)

This commit is contained in:
Rens Groothuijsen 2025-08-07 03:38:07 +02:00 committed by GitHub
parent e2652f1c62
commit 603dbfd7f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 56 additions and 1 deletions

View File

@ -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);

View File

@ -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<File> 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<String>), " +
"ArrayVeci32(Vec<i32>)," +
"}");
TestUtils.assertFileExists(outputPath);
TestUtils.assertFileContains(outputPath, enumSpec);
}
}

View File

@ -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