Skip to content

Commit cfeea18

Browse files
committed
fix #22147 handle rust generating null
1 parent d81b052 commit cfeea18

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ public CodegenModel fromModel(String name, Schema model) {
308308
// In-placed type (primitive), because there is no mapping or ref for it.
309309
// use camelized `title` if present, otherwise use `type`
310310
String oneOfName = Optional.ofNullable(schema.getTitle()).orElseGet(schema::getType);
311+
if (oneOfName == null) {
312+
oneOfName = "Variant" + i;
313+
}
311314
oneOf.setName(toModelName(oneOfName));
312315
}
313316
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,22 @@ public void testMultipleArrayTypesEnum() throws IOException {
271271
TestUtils.assertFileExists(outputPath);
272272
TestUtils.assertFileContains(outputPath, enumSpec);
273273
}
274+
275+
@Test
276+
public void testAnonymousOneOfGeneratesRustModel() throws IOException {
277+
Path target = Files.createTempDirectory("test_anonymous_one_of");
278+
final CodegenConfigurator configurator = new CodegenConfigurator()
279+
.setGeneratorName("rust")
280+
.setInputSpec("src/test/resources/3_0/rust/rust-anonymous-oneof-model.yaml")
281+
.setOutputDir(target.toAbsolutePath().toString().replace("\\", "/"));
282+
283+
try {
284+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
285+
files.forEach(File::deleteOnExit);
286+
Path outputPath = Path.of(target.toString(), "/src/models/custom_fields_attributes.rs");
287+
TestUtils.assertFileExists(outputPath);
288+
} finally {
289+
target.toFile().deleteOnExit();
290+
}
291+
}
274292
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Rust Anonymous OneOf Model
4+
version: 1.0.0
5+
paths:
6+
/example:
7+
patch:
8+
requestBody:
9+
required: true
10+
content:
11+
application/json:
12+
schema:
13+
type: object
14+
properties:
15+
custom_fields_attributes:
16+
$ref: "#/components/schemas/custom_fields_attributes"
17+
responses:
18+
"200":
19+
description: Success
20+
content:
21+
application/json:
22+
schema:
23+
type: object
24+
components:
25+
schemas:
26+
custom_fields_attributes:
27+
oneOf:
28+
- type: object
29+
additionalProperties:
30+
type: object
31+
properties:
32+
id:
33+
type:
34+
- integer
35+
- string
36+
value:
37+
type: string
38+
required:
39+
- id
40+
- value
41+
- type: array
42+
items:
43+
type: object
44+
properties:
45+
id:
46+
type:
47+
- integer
48+
- string
49+
value:
50+
type: string

0 commit comments

Comments
 (0)