Skip to content

Commit 02204d0

Browse files
fix: [csharp] JsonConverter anyof (#21137)
1 parent bbc0b4a commit 02204d0

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/JsonConverter.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
{{^vendorExtensions.x-duplicated-data-type}}
285285
{{#model.discriminator}}
286286
{{#model.hasDiscriminatorWithNonEmptyMapping}}
287+
{{^model.composedSchemas.anyOf}}
287288
{{#mappedModels}}
288289
if ({{#lambda.camelcase_sanitize_param}}{{model.classname}}{{/lambda.camelcase_sanitize_param}} != null)
289290
return new {{classname}}({{#lambda.joinWithComma}}{{#lambda.camelcase_sanitize_param}}{{model.classname}}{{/lambda.camelcase_sanitize_param}}{{#vendorExtensions.x-is-value-type}}{{^isNullable}}.Value{{/isNullable}}{{/vendorExtensions.x-is-value-type}} {{#model.composedSchemas.anyOf}}{{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}}{{#vendorExtensions.x-is-value-type}}{{^isNullable}}.Value{{/isNullable}}{{/vendorExtensions.x-is-value-type}} {{/model.composedSchemas.anyOf}}{{#allVars}}{{^isDiscriminator}}{{#lambda.camelcase_sanitize_param}}{{name}}{{/lambda.camelcase_sanitize_param}}{{#required}}.Value{{nrt!}}{{^isNullable}}{{#vendorExtensions.x-is-value-type}}.Value{{/vendorExtensions.x-is-value-type}}{{/isNullable}}{{/required}} {{/isDiscriminator}}{{/allVars}}{{/lambda.joinWithComma}});
@@ -292,6 +293,7 @@
292293
throw new JsonException();
293294
{{/-last}}
294295
{{/mappedModels}}
296+
{{/model.composedSchemas.anyOf}}
295297
{{/model.hasDiscriminatorWithNonEmptyMapping}}
296298
{{/model.discriminator}}
297299
{{^composedSchemas.oneOf}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/csharpnetcore/CSharpClientCodegenTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import static org.junit.jupiter.api.Assertions.assertNotNull;
3535
import static org.openapitools.codegen.TestUtils.assertFileContains;
36+
import static org.openapitools.codegen.TestUtils.assertFileNotContains;
3637

3738
public class CSharpClientCodegenTest {
3839

@@ -181,4 +182,33 @@ public void testEnumDiscriminatorDefaultValueIsNotString() throws IOException {
181182
assertFileContains(file.toPath(), expectedContent);
182183
}
183184
}
185+
186+
@Test
187+
public void testAnyOfDiscriminatorCreatesCompilableCode() throws IOException {
188+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
189+
output.deleteOnExit();
190+
final OpenAPI openAPI = TestUtils.parseFlattenSpec(
191+
"src/test/resources/3_0/anyOfDiscriminatorSimple.yaml");
192+
final DefaultGenerator defaultGenerator = new DefaultGenerator();
193+
final ClientOptInput clientOptInput = new ClientOptInput();
194+
clientOptInput.openAPI(openAPI);
195+
CSharpClientCodegen cSharpClientCodegen = new CSharpClientCodegen();
196+
cSharpClientCodegen.setLibrary("generichost");
197+
cSharpClientCodegen.setOutputDir(output.getAbsolutePath());
198+
cSharpClientCodegen.setAutosetConstants(true);
199+
clientOptInput.config(cSharpClientCodegen);
200+
defaultGenerator.opts(clientOptInput);
201+
202+
Map<String, File> files = defaultGenerator.generate().stream()
203+
.collect(Collectors.toMap(File::getPath, Function.identity()));
204+
205+
String modelName = "FruitAnyOfDisc";
206+
File file = files.get(Paths
207+
.get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Model", modelName + ".cs")
208+
.toString()
209+
);
210+
assertNotNull(file, "Could not find file for model: " + modelName);
211+
// Should not contain this as the constructor will have two parameters instead of one
212+
assertFileNotContains(file.toPath(), "return new FruitAnyOfDisc(appleAnyOfDisc);");
213+
}
184214
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
openapi: 3.0.1
2+
info:
3+
title: fruity
4+
version: 0.0.1
5+
paths:
6+
/:
7+
get:
8+
responses:
9+
'200':
10+
description: desc
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/FruitAnyOfDisc'
15+
components:
16+
schemas:
17+
FruitAnyOfDisc:
18+
anyOf:
19+
- $ref: '#/components/schemas/AppleAnyOfDisc'
20+
- $ref: '#/components/schemas/BananaAnyOfDisc'
21+
discriminator:
22+
propertyName: fruitType
23+
AppleAnyOfDisc:
24+
type: object
25+
required:
26+
- fruitType
27+
properties:
28+
fruitType:
29+
type: string
30+
BananaAnyOfDisc:
31+
type: object
32+
required:
33+
- fruitType
34+
properties:
35+
fruitType:
36+
type: string

0 commit comments

Comments
 (0)