Skip to content

Commit 3f2a342

Browse files
Initialize discriminator enum field
1 parent 9c0a126 commit 3f2a342

File tree

6 files changed

+58
-28
lines changed

6 files changed

+58
-28
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,6 +3080,7 @@ public CodegenModel fromModel(String name, Schema schema) {
30803080
listOLists.add(m.requiredVars);
30813081
listOLists.add(m.vars);
30823082
listOLists.add(m.allVars);
3083+
listOLists.add(m.readWriteVars);
30833084
for (List<CodegenProperty> theseVars : listOLists) {
30843085
for (CodegenProperty requiredVar : theseVars) {
30853086
if (discPropName.equals(requiredVar.baseName)) {
@@ -3113,6 +3114,37 @@ public CodegenModel fromModel(String name, Schema schema) {
31133114
return m;
31143115
}
31153116

3117+
protected static void setEnumDiscriminatorDefaultValue(CodegenModel model) {
3118+
if (model.discriminator == null) {
3119+
return;
3120+
}
3121+
String discPropName = model.discriminator.getPropertyBaseName();
3122+
Stream.of(model.requiredVars, model.vars, model.allVars, model.readWriteVars)
3123+
.flatMap(List::stream)
3124+
.filter(v -> discPropName.equals(v.baseName))
3125+
.forEach(v -> v.defaultValue = getEnumValueForProperty(model.schemaName, model.discriminator, v));
3126+
}
3127+
3128+
protected static String getEnumValueForProperty(
3129+
String modelName, CodegenDiscriminator discriminator, CodegenProperty var) {
3130+
if (!discriminator.getIsEnum() && !var.isEnum) {
3131+
return null;
3132+
}
3133+
Map<String, String> mapping = Optional.ofNullable(discriminator.getMapping()).orElseGet(Collections::emptyMap);
3134+
for (Map.Entry<String, String> e : mapping.entrySet()) {
3135+
String schemaName = e.getValue().indexOf('/') < 0 ? e.getValue() : ModelUtils.getSimpleRef(e.getValue());
3136+
if (modelName.equals(schemaName)) {
3137+
return e.getKey();
3138+
}
3139+
}
3140+
Object values = var.allowableValues.get("values");
3141+
if (!(values instanceof List<?>)) {
3142+
return null;
3143+
}
3144+
List<?> valueList = (List<?>) values;
3145+
return valueList.stream().filter(o -> o.equals(modelName)).map(o -> (String) o).findAny().orElse(null);
3146+
}
3147+
31163148
protected void SortModelPropertiesByRequiredFlag(CodegenModel model) {
31173149
Comparator<CodegenProperty> comparator = new Comparator<CodegenProperty>() {
31183150
@Override

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,7 @@ public CodegenModel fromModel(String name, Schema model) {
17001700

17011701
// additional import for different cases
17021702
addAdditionalImports(codegenModel, codegenModel.getComposedSchemas());
1703+
setEnumDiscriminatorDefaultValue(codegenModel);
17031704
return codegenModel;
17041705
}
17051706

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ public String apiTestFileFolder() {
425425
public CodegenModel fromModel(String name, Schema model) {
426426
Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);
427427
CodegenModel codegenModel = super.fromModel(name, model);
428+
setEnumDiscriminatorDefaultValue(codegenModel);
428429
if (allDefinitions != null && codegenModel != null && codegenModel.parent != null) {
429430
final Schema<?> parentModel = allDefinitions.get(toModelName(codegenModel.parent));
430431
if (parentModel != null) {
@@ -439,22 +440,7 @@ public CodegenModel fromModel(String name, Schema model) {
439440
}
440441

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

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/pojo.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
8080
{{/parcelableModel}}
8181
{{/parent}}
8282
{{#discriminator}}
83+
{{#discriminator.isEnum}}
84+
{{#readWriteVars}}{{#isDiscriminator}}{{#defaultValue}}
85+
this.{{name}} = {{defaultValue}};
86+
{{/defaultValue}}{{/isDiscriminator}}{{/readWriteVars}}
87+
{{/discriminator.isEnum}}
8388
{{^discriminator.isEnum}}
8489
this.{{{discriminatorName}}} = this.getClass().getSimpleName();
8590
{{/discriminator.isEnum}}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,20 +2298,25 @@ public void testEnumDiscriminatorDefaultValueIsNotString() {
22982298
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
22992299
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
23002300

2301-
List<String> entities = List.of(
2302-
"Cat",
2303-
"Dog",
2304-
"Gecko",
2305-
"Chameleon",
2306-
"MiniVan",
2307-
"CargoVan",
2308-
"SUV",
2309-
"Truck",
2310-
"Sedan");
2311-
for (String entity : entities) {
2312-
File entityFile = files.get(entity + ".java");
2301+
Map<String, String> expectedContents = Map.of(
2302+
"Cat", "this.petType = PetTypeEnum.CATTY",
2303+
"Dog", "this.petType = PetTypeEnum.DOG",
2304+
"Gecko", "this.petType = PetTypeEnum.GECKO",
2305+
"Chameleon", "this.petType = PetTypeEnum.CAMO",
2306+
"MiniVan", "this.carType = CarType.MINI_VAN",
2307+
"CargoVan", "this.carType = CarType.CARGO_VAN",
2308+
"SUV", "this.carType = CarType.SUV",
2309+
"Truck", "this.carType = CarType.TRUCK",
2310+
"Sedan", "this.carType = CarType.SEDAN"
2311+
2312+
);
2313+
for (Map.Entry<String, String> e : expectedContents.entrySet()) {
2314+
String modelName = e.getKey();
2315+
String expectedContent = e.getValue();
2316+
File entityFile = files.get(modelName + ".java");
23132317
assertNotNull(entityFile);
23142318
assertThat(entityFile).content().doesNotContain("Type = this.getClass().getSimpleName();");
2319+
assertThat(entityFile).content().contains(expectedContent);
23152320
}
23162321
}
23172322

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/EnumStringDiscriminator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
107107
protected EnumStrTypeEnum enumStrType;
108108

109109
public EnumStringDiscriminator() {
110+
110111
}
111112

112113
public EnumStringDiscriminator enumStrType(EnumStrTypeEnum enumStrType) {

0 commit comments

Comments
 (0)