From fdb13c3e2e5fa96523102d7c92ae2a6b409c47d4 Mon Sep 17 00:00:00 2001 From: bgong-mdsol Date: Thu, 16 Sep 2021 21:38:48 -0400 Subject: [PATCH] add the correct import of enum (#10412) --- .../languages/ScalaSttpClientCodegen.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java index 1f7128b4c9f0..4ad5505871ca 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java @@ -219,6 +219,103 @@ public String escapeReservedWord(String name) { return "`" + name + "`"; } + @Override + public Map postProcessModels(Map objs) { + return objs; + } + + /** + * Invoked by {@link DefaultGenerator} after all models have been post-processed, + * allowing for a last pass of codegen-specific model cleanup. + * + * @param objs Current state of codegen object model. + * @return An in-place modified state of the codegen object model. + */ + @Override + public Map postProcessAllModels(Map objs) { + final Map processed = super.postProcessAllModels(objs); + postProcessUpdateImports(processed); + return processed; + } + + /** + * Update/clean up model imports + * + * append '._" if the import is a Enum class, otherwise + * remove model imports to avoid warnings for importing class in the same package in Scala + * + * @param models processed models to be further processed + */ + @SuppressWarnings({"unchecked"}) + private void postProcessUpdateImports(final Map models) { + final String prefix = modelPackage() + "."; + Map enumRefs = new HashMap(); + for (Map.Entry entry : models.entrySet()) { + CodegenModel model = ModelUtils.getModelByName(entry.getKey(), models); + if (model.isEnum) { + Map objs = (Map)models.get(entry.getKey()); + enumRefs.put(entry.getKey(), objs); + } + } + + for (Map.Entry entry : models.entrySet()) { + String openAPIName = entry.getKey(); + CodegenModel model = ModelUtils.getModelByName(openAPIName, models); + if (model == null) { + LOGGER.warn("Expected to retrieve model %s by name, but no model was found. Check your -Dmodels inclusions.", openAPIName); + continue; + } + Map objs = (Map)models.get(openAPIName); + List> imports = (List>) objs.get("imports"); + if (imports == null || imports.isEmpty()) { + continue; + } + List> newImports = new ArrayList<>(); + Iterator> iterator = imports.iterator(); + while (iterator.hasNext()) { + String importPath = iterator.next().get("import"); + if (importPath.startsWith(prefix)) { + if (isEnumClass(importPath, (Map)enumRefs)) { + Map item = new HashMap<>(); + item.put("import", importPath.concat("._")); + newImports.add(item); + } + } + else { + Map item = new HashMap<>(); + item.put("import", importPath); + newImports.add(item); + } + + } + // reset imports + objs.put("imports", newImports); + } + } + + @SuppressWarnings("unchecked") + private boolean isEnumClass(final String importPath, final Map enumModels) { + if (enumModels == null || enumModels.isEmpty()) { + return false; + } + for (Map.Entry entry : enumModels.entrySet()) { + String name = entry.getKey(); + Map objs = (Map)enumModels.get(name); + List> modles = (List>) objs.get("models"); + if (modles == null || modles.isEmpty()) { + continue; + } + Iterator> iterator = modles.iterator(); + while (iterator.hasNext()) { + String enumImportPath = (String)iterator.next().get("importPath"); + if (enumImportPath != null && enumImportPath.equals(importPath)) { + return true; + } + } + } + return false; + } + @Override public Map postProcessOperationsWithModels(Map objs, List allModels) { if (registerNonStandardStatusCodes) {