forked from loafle/openapi-generator-original
fix: [csharp] JsonConverter anyof (#21137)
This commit is contained in:
parent
bbc0b4a79f
commit
02204d0e4b
@ -284,6 +284,7 @@
|
||||
{{^vendorExtensions.x-duplicated-data-type}}
|
||||
{{#model.discriminator}}
|
||||
{{#model.hasDiscriminatorWithNonEmptyMapping}}
|
||||
{{^model.composedSchemas.anyOf}}
|
||||
{{#mappedModels}}
|
||||
if ({{#lambda.camelcase_sanitize_param}}{{model.classname}}{{/lambda.camelcase_sanitize_param}} != null)
|
||||
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 @@
|
||||
throw new JsonException();
|
||||
{{/-last}}
|
||||
{{/mappedModels}}
|
||||
{{/model.composedSchemas.anyOf}}
|
||||
{{/model.hasDiscriminatorWithNonEmptyMapping}}
|
||||
{{/model.discriminator}}
|
||||
{{^composedSchemas.oneOf}}
|
||||
|
@ -33,6 +33,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.openapitools.codegen.TestUtils.assertFileContains;
|
||||
import static org.openapitools.codegen.TestUtils.assertFileNotContains;
|
||||
|
||||
public class CSharpClientCodegenTest {
|
||||
|
||||
@ -181,4 +182,33 @@ public class CSharpClientCodegenTest {
|
||||
assertFileContains(file.toPath(), expectedContent);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnyOfDiscriminatorCreatesCompilableCode() throws IOException {
|
||||
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
|
||||
output.deleteOnExit();
|
||||
final OpenAPI openAPI = TestUtils.parseFlattenSpec(
|
||||
"src/test/resources/3_0/anyOfDiscriminatorSimple.yaml");
|
||||
final DefaultGenerator defaultGenerator = new DefaultGenerator();
|
||||
final ClientOptInput clientOptInput = new ClientOptInput();
|
||||
clientOptInput.openAPI(openAPI);
|
||||
CSharpClientCodegen cSharpClientCodegen = new CSharpClientCodegen();
|
||||
cSharpClientCodegen.setLibrary("generichost");
|
||||
cSharpClientCodegen.setOutputDir(output.getAbsolutePath());
|
||||
cSharpClientCodegen.setAutosetConstants(true);
|
||||
clientOptInput.config(cSharpClientCodegen);
|
||||
defaultGenerator.opts(clientOptInput);
|
||||
|
||||
Map<String, File> files = defaultGenerator.generate().stream()
|
||||
.collect(Collectors.toMap(File::getPath, Function.identity()));
|
||||
|
||||
String modelName = "FruitAnyOfDisc";
|
||||
File file = files.get(Paths
|
||||
.get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Model", modelName + ".cs")
|
||||
.toString()
|
||||
);
|
||||
assertNotNull(file, "Could not find file for model: " + modelName);
|
||||
// Should not contain this as the constructor will have two parameters instead of one
|
||||
assertFileNotContains(file.toPath(), "return new FruitAnyOfDisc(appleAnyOfDisc);");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
openapi: 3.0.1
|
||||
info:
|
||||
title: fruity
|
||||
version: 0.0.1
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
responses:
|
||||
'200':
|
||||
description: desc
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/FruitAnyOfDisc'
|
||||
components:
|
||||
schemas:
|
||||
FruitAnyOfDisc:
|
||||
anyOf:
|
||||
- $ref: '#/components/schemas/AppleAnyOfDisc'
|
||||
- $ref: '#/components/schemas/BananaAnyOfDisc'
|
||||
discriminator:
|
||||
propertyName: fruitType
|
||||
AppleAnyOfDisc:
|
||||
type: object
|
||||
required:
|
||||
- fruitType
|
||||
properties:
|
||||
fruitType:
|
||||
type: string
|
||||
BananaAnyOfDisc:
|
||||
type: object
|
||||
required:
|
||||
- fruitType
|
||||
properties:
|
||||
fruitType:
|
||||
type: string
|
Loading…
x
Reference in New Issue
Block a user