From 0d30faccc2355b7d04a18b1fe3bed588fb4c6c40 Mon Sep 17 00:00:00 2001 From: frantuma Date: Mon, 29 Jul 2024 11:37:11 +0200 Subject: [PATCH] implement config priorities --- .../main/java/io/swagger/codegen/Codegen.java | 17 +++++++++++++---- .../java/io/swagger/codegen/CodegenConfig.java | 12 ++++++++++++ .../io/swagger/codegen/CodegenConfigLoader.java | 17 ++++++++++++++--- .../java/io/swagger/codegen/MetaGenerator.java | 15 ++++++++++++--- pom.xml | 4 ++-- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/Codegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/Codegen.java index 839f0800e15..98377c0fa2f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/Codegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/Codegen.java @@ -122,11 +122,20 @@ public static void main(String[] args) { public static List getExtensions() { ServiceLoader loader = ServiceLoader.load(CodegenConfig.class); - List output = new ArrayList(); - for (CodegenConfig aLoader : loader) { - output.add(aLoader); + Map output = new HashMap<>(); + + for (CodegenConfig config : loader) { + if (output.get(config.getName()) == null) { + output.put(config.getName(), config); + } else if (config.isPrivileged() && !output.get(config.getName()).isPrivileged()) { + output.put(config.getName(), config); + } else if (output.get(config.getName()).isPrivileged() && !config.isPrivileged()) { + // skip + } else if (config.getPriority() > output.get(config.getName()).getPriority()) { + output.put(config.getName(), config); + } } - return output; + return new ArrayList<>(output.values()); } static void usage(Options options) { diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java index 9e07b956594..9f7e605771c 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java @@ -239,4 +239,16 @@ public interface CodegenConfig { boolean defaultIgnoreImportMappingOption(); boolean isUsingFlattenSpec(); + + default boolean isPrivileged() { + return false; + } + + default int getPriority() { + return Integer.MIN_VALUE; + } + + default String getCodeName() { + return getName(); + } } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfigLoader.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfigLoader.java index 45a57e7bb3a..54f5c7bee63 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfigLoader.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfigLoader.java @@ -15,14 +15,25 @@ public static CodegenConfig forName(String name) { ServiceLoader loader = load(CodegenConfig.class); StringBuilder availableConfigs = new StringBuilder(); - + CodegenConfig current = null; for (CodegenConfig config : loader) { + if (config.getName().equals(name)) { - return config; + if (current == null) { + current = config; + } else if (config.isPrivileged() && !current.isPrivileged()) { + current = config; + } else if (current.isPrivileged() && !config.isPrivileged()) { + // skip + } else if (config.getPriority() > current.getPriority()) { + current = config; + } } - availableConfigs.append(config.getName()).append("\n"); } + if (current != null) { + return current; + } // else try to load directly try { diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/MetaGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/MetaGenerator.java index ecc8b02e689..3f3d08908b8 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/MetaGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/MetaGenerator.java @@ -39,11 +39,20 @@ public static void main(String[] args) { public static List getExtensions() { ServiceLoader loader = ServiceLoader.load(CodegenConfig.class); - List output = new ArrayList(); + Map output = new HashMap<>(); + for (CodegenConfig config : loader) { - output.add(config); + if (output.get(config.getName()) == null) { + output.put(config.getName(), config); + } else if (config.isPrivileged() && !output.get(config.getName()).isPrivileged()) { + output.put(config.getName(), config); + } else if (output.get(config.getName()).isPrivileged() && !config.isPrivileged()) { + // skip + } else if (config.getPriority() > output.get(config.getName()).getPriority()) { + output.put(config.getName(), config); + } } - return output; + return new ArrayList<>(output.values()); } static void usage(Options options) { diff --git a/pom.xml b/pom.xml index cc1394a4d40..d8307d03be5 100644 --- a/pom.xml +++ b/pom.xml @@ -1058,7 +1058,7 @@ 8 - 1.0.70 + 1.0.71 2.11.1 3.3.0 1.6.14 @@ -1075,7 +1075,7 @@ Codegen support of Java 8 should be removed if updating to 1.16+ --> 1.15 - 7.10.1 + 7.10.2 3.0.0 1.49 5.11.0