Skip to content

Commit 4bd0ec3

Browse files
Refactor some code to make it cleaner
1 parent 452e95b commit 4bd0ec3

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

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

+18-24
Original file line numberDiff line numberDiff line change
@@ -3575,30 +3575,24 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch
35753575
discriminator.getMappedModels().addAll(uniqueDescendants);
35763576

35773577
// check current schema, all parents and descendants to see if the discriminator property is an enum string
3578-
List<Schema> schemasToCheckForEnumDiscriminator = new ArrayList<>();
3579-
schemasToCheckForEnumDiscriminator.add(schema);
3580-
if (ModelUtils.isComposedSchema(schema)) {
3581-
ModelUtils.getAllParentsName(schema, openAPI.getComponents().getSchemas(), true).stream()
3582-
.map(n -> ModelUtils.getSchema(openAPI, n))
3583-
.forEach(schemasToCheckForEnumDiscriminator::add);
3584-
}
3585-
uniqueDescendants.stream().map(MappedModel::getModelName)
3586-
.map(n->ModelUtils.getSchema(openAPI, n))
3587-
.forEach(schemasToCheckForEnumDiscriminator::add);
3588-
for (Schema schemaToCheck : schemasToCheckForEnumDiscriminator) {
3589-
if (schemaToCheck == null) {
3590-
continue;
3591-
}
3592-
boolean hasDiscriminatorEnum = Optional.ofNullable(schemaToCheck.getProperties())
3593-
.map(p -> (Schema) p.get(discriminatorPropertyName))
3594-
.map(s -> ModelUtils.getReferencedSchema(openAPI, s))
3595-
.filter(s -> s instanceof StringSchema && s.getEnum() != null && !s.getEnum().isEmpty())
3596-
.isPresent();
3597-
if (hasDiscriminatorEnum) {
3598-
discriminator.setIsEnum(true);
3599-
break;
3600-
}
3601-
}
3578+
Stream<Schema> schemasToCheckForEnumDiscriminator = Stream.concat(
3579+
Stream.of(schema),
3580+
Stream.concat(
3581+
ModelUtils.isComposedSchema(schema)
3582+
? ModelUtils.getAllParentsName(schema, openAPI.getComponents().getSchemas(), true).stream()
3583+
: Stream.of(),
3584+
uniqueDescendants.stream().map(MappedModel::getModelName)
3585+
).flatMap(s -> Optional.ofNullable(ModelUtils.getSchema(openAPI, s)).stream())
3586+
);
3587+
3588+
boolean isEnum = schemasToCheckForEnumDiscriminator.anyMatch(s -> Optional
3589+
.ofNullable(s.getProperties())
3590+
.map(p -> (Schema) p.get(discriminatorPropertyName))
3591+
.map(s1 -> ModelUtils.getReferencedSchema(openAPI, s1))
3592+
.filter(s1 -> s1 instanceof StringSchema && s1.getEnum() != null && !s1.getEnum().isEmpty())
3593+
.isPresent()
3594+
);
3595+
discriminator.setIsEnum(isEnum);
36023596

36033597
return discriminator;
36043598
}

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -440,21 +440,22 @@ public CodegenModel fromModel(String name, Schema model) {
440440

441441
for (final CodegenProperty property : codegenModel.readWriteVars) {
442442
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-
}
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;
453454
}
454-
property.defaultValue = toEnumDefaultValue(property, enumValue);
455-
} else {
456-
property.defaultValue = "\"" + name + "\"";
457455
}
456+
property.defaultValue = toEnumDefaultValue(property, enumValue);
457+
} else {
458+
property.defaultValue = "\"" + name + "\"";
458459
}
459460
}
460461

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,8 @@ public static List<String> getAllParentsName(Schema composedSchema, Map<String,
15981598
return getAllParentsName(composedSchema, allSchemas, includeAncestors, new HashSet<>());
15991599
}
16001600

1601-
public static List<String> getAllParentsName(
1601+
// Use a set of seen names to avoid infinite recursion
1602+
private static List<String> getAllParentsName(
16021603
Schema composedSchema, Map<String, Schema> allSchemas, boolean includeAncestors, Set<String> seenNames) {
16031604
List<Schema> interfaces = getInterfaces(composedSchema);
16041605
List<String> names = new ArrayList<String>();

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -2287,15 +2287,16 @@ public void testAllOfWithSinglePrimitiveTypeRef() {
22872287
assertNull(files.get("pom.xml"));
22882288
}
22892289

2290-
@Test public void testEnumDiscriminatorDefaultValueIsNotString() throws IOException {
2290+
@Test
2291+
public void testEnumDiscriminatorDefaultValueIsNotString() {
22912292
final Path output = newTempFolder();
22922293
final OpenAPI openAPI = TestUtils.parseFlattenSpec(
2293-
"src/test/resources/3_0/enum_discriminator_inheritance.yaml");
2294+
"src/test/resources/3_0/enum_discriminator_inheritance.yaml");
22942295
JavaClientCodegen codegen = new JavaClientCodegen();
22952296
codegen.setOutputDir(output.toString());
22962297

22972298
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
2298-
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
2299+
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
22992300

23002301
List<String> entities = List.of(
23012302
"Cat",

0 commit comments

Comments
 (0)