Skip to content

Commit 8fe6bc0

Browse files
Handle different use cases of mappings
1 parent 5079f3a commit 8fe6bc0

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,19 @@ public CodegenModel fromModel(String name, Schema model) {
439439
}
440440

441441
for (final CodegenProperty property : codegenModel.readWriteVars) {
442-
if (property.defaultValue == null && parentCodegenModel.discriminator != null && property.name.equals(parentCodegenModel.discriminator.getPropertyName())) {
443-
if (parentCodegenModel.discriminator.getIsEnum()) {
444-
property.defaultValue = toEnumDefaultValue(property, name);
442+
CodegenDiscriminator discriminator = parentCodegenModel.discriminator;
443+
if (property.defaultValue == null && discriminator != null && property.name.equals(discriminator.getPropertyName())) {
444+
if (discriminator.getIsEnum()) {
445+
String enumValue = name;
446+
Map<String, String> mapping = Optional.ofNullable(discriminator.getMapping()).orElseGet(Collections::emptyMap);
447+
for (Map.Entry<String, String> e : mapping.entrySet()) {
448+
String schemaName = e.getValue().indexOf('/') < 0 ? e.getValue() : ModelUtils.getSimpleRef(e.getValue());
449+
if (name.equals(schemaName)) {
450+
enumValue = e.getKey();
451+
break;
452+
}
453+
}
454+
property.defaultValue = toEnumDefaultValue(property, enumValue);
445455
} else {
446456
property.defaultValue = "\"" + name + "\"";
447457
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void testEnumDiscriminatorDefaultValueIsNotString() throws IOException {
146146
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
147147
output.deleteOnExit();
148148
final OpenAPI openAPI = TestUtils.parseFlattenSpec(
149-
"src/test/resources/3_0/enum_discriminator_inheritance.yaml");
149+
"src/test/resources/3_0/enum_discriminator_inheritance.yaml");
150150
final DefaultGenerator defaultGenerator = new DefaultGenerator();
151151
final ClientOptInput clientOptInput = new ClientOptInput();
152152
clientOptInput.openAPI(openAPI);
@@ -156,14 +156,23 @@ public void testEnumDiscriminatorDefaultValueIsNotString() throws IOException {
156156
clientOptInput.config(cSharpClientCodegen);
157157
defaultGenerator.opts(clientOptInput);
158158

159-
Map<String, File> files = defaultGenerator.generate().stream()
160-
.collect(Collectors.toMap(File::getPath, Function.identity()));
159+
Map<String, File> files = defaultGenerator.generate().stream()
160+
.collect(Collectors.toMap(File::getPath, Function.identity()));
161161

162-
File modelFile = files.get(Paths
163-
.get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Model", "Cat.cs")
164-
.toString()
162+
Map<String, String> expectedMapping = Map.of(
163+
"Catty", "Cat",
164+
"Lizzy", "Lizard",
165+
"Dog", "Dog"
165166
);
166-
assertNotNull(modelFile);
167-
assertFileContains(modelFile.toPath(), "AnimalType petType = AnimalType.Cat");
167+
for (Map.Entry<String, String> e : expectedMapping.entrySet()) {
168+
String modelName = e.getValue();
169+
String enumValue = e.getKey();
170+
File file = files.get(Paths
171+
.get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Model", modelName + ".cs")
172+
.toString()
173+
);
174+
assertNotNull(file, "Could not find file for model: " + modelName);
175+
assertFileContains(file.toPath(), "AnimalType petType = AnimalType." + enumValue);
176+
}
168177
}
169178
}

modules/openapi-generator/src/test/resources/3_0/enum_discriminator_inheritance.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ components:
2121
$ref: '#/components/schemas/AnimalType'
2222
discriminator:
2323
propertyName: petType
24+
# Mapping with implicit (Dog), explicit ref (Catty -> Cat), and explicit schema name (Lizzy -> Lizard)
25+
mapping:
26+
Catty: '#/components/schemas/Cat'
27+
Lizzy: 'Lizard'
2428
required:
2529
- petType
2630
AnimalType:
2731
type: string
2832
enum:
2933
- Dog
30-
- Cat
34+
- Catty
3135
Cat:
3236
type: object
3337
allOf:
@@ -42,3 +46,10 @@ components:
4246
properties:
4347
bark:
4448
type: string
49+
Lizard:
50+
type: object
51+
allOf:
52+
- $ref: '#/components/schemas/Animal'
53+
properties:
54+
lovesRocks:
55+
type: string

0 commit comments

Comments
 (0)