Skip to content

Commit 9a6a0b5

Browse files
code review feedback; move containsEnums to ModelUtils
1 parent c01a85b commit 9a6a0b5

3 files changed

Lines changed: 17 additions & 31 deletions

File tree

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.openapitools.codegen.model.ModelsMap;
3535
import org.openapitools.codegen.model.OperationMap;
3636
import org.openapitools.codegen.model.OperationsMap;
37+
import org.openapitools.codegen.utils.ModelUtils;
3738
import org.openapitools.codegen.utils.URLPathUtils;
3839
import org.slf4j.Logger;
3940
import org.slf4j.LoggerFactory;
@@ -769,7 +770,7 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
769770
public void preprocessOpenAPI(OpenAPI openAPI) {
770771
super.preprocessOpenAPI(openAPI);
771772

772-
if (SPRING_BOOT.equals(library) && containsEnums()) {
773+
if (SPRING_BOOT.equals(library) && ModelUtils.containsEnums(this.openAPI)) {
773774
supportingFiles.add(new SupportingFile("converter.mustache",
774775
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "EnumConverterConfiguration.kt"));
775776
}
@@ -803,20 +804,6 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
803804
// TODO: Handle tags
804805
}
805806

806-
private boolean containsEnums() {
807-
if (openAPI == null) {
808-
return false;
809-
}
810-
811-
Components components = this.openAPI.getComponents();
812-
if (components == null || components.getSchemas() == null) {
813-
return false;
814-
}
815-
816-
return components.getSchemas().values().stream()
817-
.anyMatch(it -> it.getEnum() != null && !it.getEnum().isEmpty());
818-
}
819-
820807
@Override
821808
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
822809
super.postProcessModelProperty(model, property);

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.openapitools.codegen.languages;
1919

2020
import com.samskivert.mustache.Mustache;
21-
import io.swagger.v3.oas.models.Components;
2221
import io.swagger.v3.oas.models.OpenAPI;
2322
import io.swagger.v3.oas.models.Operation;
2423
import io.swagger.v3.oas.models.PathItem;
@@ -40,6 +39,7 @@
4039
import org.openapitools.codegen.templating.mustache.SplitStringLambda;
4140
import org.openapitools.codegen.templating.mustache.SpringHttpStatusLambda;
4241
import org.openapitools.codegen.templating.mustache.TrimWhitespaceLambda;
42+
import org.openapitools.codegen.utils.ModelUtils;
4343
import org.openapitools.codegen.utils.ProcessUtils;
4444
import org.openapitools.codegen.utils.URLPathUtils;
4545
import org.slf4j.Logger;
@@ -648,20 +648,6 @@ public void processOpts() {
648648
supportsAdditionalPropertiesWithComposedSchema = true;
649649
}
650650

651-
private boolean containsEnums() {
652-
if (openAPI == null) {
653-
return false;
654-
}
655-
656-
Components components = this.openAPI.getComponents();
657-
if (components == null || components.getSchemas() == null) {
658-
return false;
659-
}
660-
661-
return components.getSchemas().values().stream()
662-
.anyMatch(it -> it.getEnum() != null && !it.getEnum().isEmpty());
663-
}
664-
665651
private boolean supportLibraryUseTags() {
666652
return SPRING_BOOT.equals(library) || SPRING_CLOUD_LIBRARY.equals(library);
667653
}
@@ -696,7 +682,7 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
696682
public void preprocessOpenAPI(OpenAPI openAPI) {
697683
super.preprocessOpenAPI(openAPI);
698684

699-
if (SPRING_BOOT.equals(library) && containsEnums()) {
685+
if (SPRING_BOOT.equals(library) && ModelUtils.containsEnums(this.openAPI)) {
700686
supportingFiles.add(new SupportingFile("converter.mustache",
701687
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "EnumConverterConfiguration.java"));
702688
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,6 +2435,19 @@ public static boolean isMetadataOnlySchema(Schema schema) {
24352435
schema.getContentSchema() != null);
24362436
}
24372437

2438+
/**
2439+
* Returns true if the OpenAPI specification contains any schemas which are enums.
2440+
* @param openAPI OpenAPI specification
2441+
* @return true if the OpenAPI specification contains any schemas which are enums.
2442+
*/
2443+
public static boolean containsEnums(OpenAPI openAPI) {
2444+
Map<String, Schema> schemaMap = getSchemas(openAPI);
2445+
if (schemaMap.isEmpty()) {
2446+
return false;
2447+
}
2448+
2449+
return schemaMap.values().stream().anyMatch(ModelUtils::isEnumSchema);
2450+
}
24382451

24392452
@FunctionalInterface
24402453
private interface OpenAPISchemaVisitor {

0 commit comments

Comments
 (0)