|
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; |
| 22 | +import java.io.File; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.EnumSet; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.Set; |
21 | 32 | import lombok.Getter; |
22 | 33 | import lombok.Setter; |
23 | 34 | import org.apache.commons.lang3.StringUtils; |
24 | | -import org.openapitools.codegen.*; |
| 35 | +import org.openapitools.codegen.CodegenConstants; |
| 36 | +import org.openapitools.codegen.CodegenDiscriminator; |
| 37 | +import org.openapitools.codegen.CodegenModel; |
| 38 | +import org.openapitools.codegen.CodegenOperation; |
| 39 | +import org.openapitools.codegen.CodegenParameter; |
| 40 | +import org.openapitools.codegen.CodegenProperty; |
| 41 | +import org.openapitools.codegen.CodegenResponse; |
| 42 | +import org.openapitools.codegen.CodegenType; |
| 43 | +import org.openapitools.codegen.SupportingFile; |
25 | 44 | import org.openapitools.codegen.languages.features.BeanValidationFeatures; |
26 | | -import org.openapitools.codegen.meta.features.*; |
| 45 | +import org.openapitools.codegen.meta.features.DocumentationFeature; |
| 46 | +import org.openapitools.codegen.meta.features.GlobalFeature; |
| 47 | +import org.openapitools.codegen.meta.features.ParameterFeature; |
| 48 | +import org.openapitools.codegen.meta.features.SchemaSupportFeature; |
| 49 | +import org.openapitools.codegen.meta.features.SecurityFeature; |
| 50 | +import org.openapitools.codegen.meta.features.WireFormatFeature; |
27 | 51 | import org.openapitools.codegen.model.ModelMap; |
28 | 52 | import org.openapitools.codegen.model.ModelsMap; |
29 | 53 | import org.openapitools.codegen.model.OperationMap; |
|
33 | 57 | import org.slf4j.Logger; |
34 | 58 | import org.slf4j.LoggerFactory; |
35 | 59 |
|
36 | | -import java.io.File; |
37 | | -import java.util.*; |
38 | | - |
39 | 60 | public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanValidationFeatures { |
40 | 61 |
|
41 | 62 | public static final String DEFAULT_LIBRARY = Constants.KTOR; |
@@ -702,32 +723,45 @@ public void postProcess() { |
702 | 723 | System.out.println("################################################################################"); |
703 | 724 | } |
704 | 725 |
|
| 726 | + @Override |
| 727 | + public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) { |
| 728 | + if (!Objects.equals(library, Constants.JAXRS_SPEC) || useTags) { |
| 729 | + super.addOperationToGroup(tag, resourcePath, operation, co, operations); |
| 730 | + return; |
| 731 | + } |
| 732 | + |
| 733 | + final String basePath = StringUtils.substringBefore(resourcePath.startsWith("/") ? resourcePath.substring(1) : resourcePath, "/"); |
| 734 | + if (!StringUtils.isEmpty(basePath)) { |
| 735 | + co.subresourceOperation = !co.path.isEmpty(); |
| 736 | + } |
| 737 | + co.baseName = basePath; |
| 738 | + if (StringUtils.isEmpty(co.baseName) || co.baseName.chars().anyMatch(ch -> ch == '{' || ch == '}')) { |
| 739 | + co.baseName = "default"; |
| 740 | + } |
| 741 | + final List<CodegenOperation> opList = operations.computeIfAbsent(co.baseName, k -> new ArrayList<>()); |
| 742 | + opList.add(co); |
| 743 | + } |
| 744 | + |
705 | 745 | @Override |
706 | 746 | public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) { |
707 | 747 | OperationMap operations = objs.getOperations(); |
708 | | - // For JAXRS_SPEC library, compute commonPath when useTags=true, otherwise default to "/" |
| 748 | + // For JAXRS_SPEC library, compute commonPath for all library modes |
709 | 749 | if (operations != null && Objects.equals(library, Constants.JAXRS_SPEC)) { |
710 | | - if (useTags) { |
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 | | - } else { |
726 | | - for (CodegenOperation co : operations.getOperation()) { |
727 | | - co.subresourceOperation = !co.path.isEmpty(); |
| 750 | + List<CodegenOperation> ops = operations.getOperation(); |
| 751 | + // Compute commonPath from operations in this group (called once per API class) |
| 752 | + String commonPath = null; |
| 753 | + for (CodegenOperation operation : ops) { |
| 754 | + if (commonPath == null) { |
| 755 | + commonPath = operation.path; |
| 756 | + } else { |
| 757 | + commonPath = getCommonPath(commonPath, operation.path); |
728 | 758 | } |
729 | | - objs.put("commonPath", "/"); |
730 | 759 | } |
| 760 | + for (CodegenOperation co : ops) { |
| 761 | + co.path = StringUtils.removeStart(co.path, commonPath); |
| 762 | + co.subresourceOperation = co.path.length() > 1; |
| 763 | + } |
| 764 | + objs.put("commonPath", "/".equals(commonPath) ? StringUtils.EMPTY : commonPath); |
731 | 765 | } |
732 | 766 | // The following processing breaks the JAX-RS spec, so we only do this for the other libs. |
733 | 767 | if (operations != null && !Objects.equals(library, Constants.JAXRS_SPEC)) { |
@@ -801,8 +835,8 @@ private boolean isJavalin() { |
801 | 835 | */ |
802 | 836 | private boolean usesJacksonSerialization() { |
803 | 837 | return Constants.JAVALIN5.equals(library) || |
804 | | - Constants.JAVALIN6.equals(library) || |
805 | | - Constants.JAXRS_SPEC.equals(library); |
| 838 | + Constants.JAVALIN6.equals(library) || |
| 839 | + Constants.JAXRS_SPEC.equals(library); |
806 | 840 | } |
807 | 841 |
|
808 | 842 | private boolean isKtor2Or3() { |
|
0 commit comments