Skip to content

Commit 5ae9f75

Browse files
committed
test(rust): Add test for anyOf support
This commit adds a test case to verify that anyOf schemas generate proper untagged enums instead of empty structs in the Rust client generator. The test includes: - A test OpenAPI spec with anyOf schemas - Unit test that verifies the generated code structure - Assertions to ensure enums are created instead of empty structs
1 parent ef3f19e commit 5ae9f75

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,37 @@ public void testMultipleArrayTypesEnum() throws IOException {
271271
TestUtils.assertFileExists(outputPath);
272272
TestUtils.assertFileContains(outputPath, enumSpec);
273273
}
274+
275+
@Test
276+
public void testAnyOfSupport() throws IOException {
277+
Path target = Files.createTempDirectory("test-anyof");
278+
final CodegenConfigurator configurator = new CodegenConfigurator()
279+
.setGeneratorName("rust")
280+
.setInputSpec("src/test/resources/3_0/rust/rust-anyof-test.yaml")
281+
.setSkipOverwrite(false)
282+
.setOutputDir(target.toAbsolutePath().toString().replace("\\", "/"));
283+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
284+
files.forEach(File::deleteOnExit);
285+
286+
// Test that ModelIdentifier generates an untagged enum, not an empty struct
287+
Path modelIdentifierPath = Path.of(target.toString(), "/src/models/model_identifier.rs");
288+
TestUtils.assertFileExists(modelIdentifierPath);
289+
290+
// Should generate an untagged enum
291+
String enumDeclaration = linearize("#[serde(untagged)] pub enum ModelIdentifier");
292+
TestUtils.assertFileContains(modelIdentifierPath, enumDeclaration);
293+
294+
// Should have String variant (for anyOf with string types)
295+
TestUtils.assertFileContains(modelIdentifierPath, "String(String)");
296+
297+
// Should NOT generate an empty struct
298+
TestUtils.assertFileNotContains(modelIdentifierPath, "pub struct ModelIdentifier {");
299+
TestUtils.assertFileNotContains(modelIdentifierPath, "pub fn new()");
300+
301+
// Test AnotherAnyOfTest with mixed types
302+
Path anotherTestPath = Path.of(target.toString(), "/src/models/another_any_of_test.rs");
303+
TestUtils.assertFileExists(anotherTestPath);
304+
TestUtils.assertFileContains(anotherTestPath, "#[serde(untagged)]");
305+
TestUtils.assertFileContains(anotherTestPath, "pub enum AnotherAnyOfTest");
306+
}
274307
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Rust anyOf Test
4+
version: 1.0.0
5+
paths:
6+
/model:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/TestResponse'
15+
components:
16+
schemas:
17+
TestResponse:
18+
type: object
19+
properties:
20+
model:
21+
$ref: '#/components/schemas/ModelIdentifier'
22+
status:
23+
type: string
24+
ModelIdentifier:
25+
description: Model identifier that can be a string or specific enum value
26+
anyOf:
27+
- type: string
28+
description: Any model name as string
29+
- type: string
30+
enum:
31+
- gpt-4
32+
- gpt-3.5-turbo
33+
- dall-e-3
34+
description: Known model enum values
35+
AnotherAnyOfTest:
36+
description: Another test case with different types
37+
anyOf:
38+
- type: string
39+
- type: integer
40+
- type: array
41+
items:
42+
type: string

0 commit comments

Comments
 (0)