|
18 | 18 | package org.openapitools.codegen.languages; |
19 | 19 |
|
20 | 20 | import com.google.common.collect.ImmutableMap; |
| 21 | +import io.swagger.v3.oas.models.Operation; |
21 | 22 | import lombok.Getter; |
22 | 23 | import lombok.Setter; |
23 | 24 | import org.apache.commons.lang3.StringUtils; |
@@ -64,6 +65,7 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa |
64 | 65 | private Boolean metricsFeatureEnabled = true; |
65 | 66 | private boolean interfaceOnly = false; |
66 | 67 | private boolean useBeanValidation = false; |
| 68 | + private boolean useTags = false; |
67 | 69 | private boolean useCoroutines = false; |
68 | 70 | private boolean useMutiny = false; |
69 | 71 | private boolean returnResponse = false; |
@@ -97,6 +99,7 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa |
97 | 99 | )) |
98 | 100 | .put(Constants.JAXRS_SPEC, Arrays.asList( |
99 | 101 | USE_BEANVALIDATION, |
| 102 | + USE_TAGS, |
100 | 103 | Constants.USE_COROUTINES, |
101 | 104 | Constants.USE_MUTINY, |
102 | 105 | Constants.RETURN_RESPONSE, |
@@ -170,6 +173,7 @@ public KotlinServerCodegen() { |
170 | 173 | addSwitch(Constants.METRICS, Constants.METRICS_DESC, getMetricsFeatureEnabled()); |
171 | 174 | addSwitch(Constants.INTERFACE_ONLY, Constants.INTERFACE_ONLY_DESC, interfaceOnly); |
172 | 175 | addSwitch(USE_BEANVALIDATION, Constants.USE_BEANVALIDATION_DESC, useBeanValidation); |
| 176 | + addSwitch(USE_TAGS, USE_TAGS_DESC, useTags); |
173 | 177 | addSwitch(Constants.USE_COROUTINES, Constants.USE_COROUTINES_DESC, useCoroutines); |
174 | 178 | addSwitch(Constants.USE_MUTINY, Constants.USE_MUTINY_DESC, useMutiny); |
175 | 179 | addSwitch(Constants.RETURN_RESPONSE, Constants.RETURN_RESPONSE_DESC, returnResponse); |
@@ -241,6 +245,10 @@ public void processOpts() { |
241 | 245 | setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION)); |
242 | 246 | } |
243 | 247 |
|
| 248 | + if (additionalProperties.containsKey(USE_TAGS)) { |
| 249 | + useTags = Boolean.parseBoolean(additionalProperties.get(USE_TAGS).toString()); |
| 250 | + } |
| 251 | + |
244 | 252 | if (additionalProperties.containsKey(Constants.OMIT_GRADLE_WRAPPER)) { |
245 | 253 | setOmitGradleWrapper(Boolean.parseBoolean(additionalProperties.get(Constants.OMIT_GRADLE_WRAPPER).toString())); |
246 | 254 | } |
@@ -698,6 +706,23 @@ public void postProcess() { |
698 | 706 | @Override |
699 | 707 | public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) { |
700 | 708 | OperationMap operations = objs.getOperations(); |
| 709 | + // For JAXRS_SPEC library, compute commonPath similar to JavaJaxRS generators |
| 710 | + if (operations != null && Objects.equals(library, Constants.JAXRS_SPEC)) { |
| 711 | + String commonPath = null; |
| 712 | + List<CodegenOperation> ops = operations.getOperation(); |
| 713 | + for (CodegenOperation operation : ops) { |
| 714 | + if (commonPath == null) { |
| 715 | + commonPath = operation.path; |
| 716 | + } else { |
| 717 | + commonPath = getCommonPath(commonPath, operation.path); |
| 718 | + } |
| 719 | + } |
| 720 | + for (CodegenOperation co : ops) { |
| 721 | + co.path = StringUtils.removeStart(co.path, commonPath); |
| 722 | + co.subresourceOperation = co.path.length() > 1; |
| 723 | + } |
| 724 | + objs.put("commonPath", "/".equals(commonPath) ? StringUtils.EMPTY : commonPath); |
| 725 | + } |
701 | 726 | // The following processing breaks the JAX-RS spec, so we only do this for the other libs. |
702 | 727 | if (operations != null && !Objects.equals(library, Constants.JAXRS_SPEC)) { |
703 | 728 | List<CodegenOperation> ops = operations.getOperation(); |
@@ -758,6 +783,24 @@ public void setReturnContainer(final String returnContainer) { |
758 | 783 | return objs; |
759 | 784 | } |
760 | 785 |
|
| 786 | + @Override |
| 787 | + public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) { |
| 788 | + if (Objects.equals(library, Constants.JAXRS_SPEC) && additionalProperties.containsKey(USE_TAGS) && !useTags) { |
| 789 | + String basePath = StringUtils.substringBefore(StringUtils.removeStart(resourcePath, "/"), "/"); |
| 790 | + if (!StringUtils.isEmpty(basePath)) { |
| 791 | + co.subresourceOperation = !co.path.isEmpty(); |
| 792 | + } |
| 793 | + co.baseName = basePath; |
| 794 | + if (StringUtils.isEmpty(co.baseName) || StringUtils.containsAny(co.baseName, "{", "}")) { |
| 795 | + co.baseName = "default"; |
| 796 | + } |
| 797 | + final List<CodegenOperation> opList = operations.computeIfAbsent(co.baseName, k -> new ArrayList<>()); |
| 798 | + opList.add(co); |
| 799 | + } else { |
| 800 | + super.addOperationToGroup(tag, resourcePath, operation, co, operations); |
| 801 | + } |
| 802 | + } |
| 803 | + |
761 | 804 | private boolean isJavalin() { |
762 | 805 | return Constants.JAVALIN5.equals(library) || Constants.JAVALIN6.equals(library); |
763 | 806 | } |
@@ -788,4 +831,17 @@ private boolean isKtor() { |
788 | 831 | private boolean isKtor2() { |
789 | 832 | return Constants.KTOR2.equals(library); |
790 | 833 | } |
| 834 | + |
| 835 | + private static String getCommonPath(String path1, String path2) { |
| 836 | + final String[] parts1 = StringUtils.split(path1, "/"); |
| 837 | + final String[] parts2 = StringUtils.split(path2, "/"); |
| 838 | + StringBuilder builder = new StringBuilder(); |
| 839 | + for (int i = 0; i < Math.min(parts1.length, parts2.length); i++) { |
| 840 | + if (!parts1[i].equals(parts2[i])) { |
| 841 | + break; |
| 842 | + } |
| 843 | + builder.append("/").append(parts1[i]); |
| 844 | + } |
| 845 | + return builder.toString(); |
| 846 | + } |
791 | 847 | } |
0 commit comments