Skip to content

Commit 92af81d

Browse files
committed
Fix class names for useTags = false
1 parent 05dd7fb commit 92af81d

File tree

3 files changed

+86
-47
lines changed

3 files changed

+86
-47
lines changed

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

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,36 @@
1818
package org.openapitools.codegen.languages;
1919

2020
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;
2132
import lombok.Getter;
2233
import lombok.Setter;
2334
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;
2544
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;
2751
import org.openapitools.codegen.model.ModelMap;
2852
import org.openapitools.codegen.model.ModelsMap;
2953
import org.openapitools.codegen.model.OperationMap;
@@ -33,9 +57,6 @@
3357
import org.slf4j.Logger;
3458
import org.slf4j.LoggerFactory;
3559

36-
import java.io.File;
37-
import java.util.*;
38-
3960
public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanValidationFeatures {
4061

4162
public static final String DEFAULT_LIBRARY = Constants.KTOR;
@@ -702,32 +723,45 @@ public void postProcess() {
702723
System.out.println("################################################################################");
703724
}
704725

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+
705745
@Override
706746
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
707747
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
709749
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);
728758
}
729-
objs.put("commonPath", "/");
730759
}
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);
731765
}
732766
// The following processing breaks the JAX-RS spec, so we only do this for the other libs.
733767
if (operations != null && !Objects.equals(library, Constants.JAXRS_SPEC)) {
@@ -801,8 +835,8 @@ private boolean isJavalin() {
801835
*/
802836
private boolean usesJacksonSerialization() {
803837
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);
806840
}
807841

808842
private boolean isKtor2Or3() {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
@{{httpMethod}}
2-
@Path("{{{path}}}"){{#hasConsumes}}
1+
@{{httpMethod}}{{#subresourceOperation}}
2+
@Path("{{{path}}}"){{/subresourceOperation}}{{#hasConsumes}}
33
@Consumes({{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}){{/hasConsumes}}{{#hasProduces}}
44
@Produces({{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}){{/hasProduces}}
55
{{#useCoroutines}}suspend {{/useCoroutines}}fun {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}){{#returnResponse}}: {{#useMutiny}}io.smallrye.mutiny.Uni<{{/useMutiny}}Response{{#useMutiny}}>{{/useMutiny}}{{/returnResponse}}{{^returnResponse}}{{#returnType}}: {{#useMutiny}}io.smallrye.mutiny.Uni<{{/useMutiny}}{{{returnType}}}{{#useMutiny}}>{{/useMutiny}}{{/returnType}}{{/returnResponse}}{{^returnResponse}}{{^returnType}}{{#useMutiny}}: io.smallrye.mutiny.Uni<Void>{{/useMutiny}}{{/returnType}}{{/returnResponse}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
package org.openapitools.codegen.kotlin;
22

3-
import lombok.Getter;
4-
import org.antlr.v4.runtime.*;
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import org.antlr.v4.runtime.CharStreams;
11+
import org.antlr.v4.runtime.CommonTokenStream;
512
import org.antlr.v4.runtime.tree.ParseTree;
613
import org.antlr.v4.runtime.tree.ParseTreeWalker;
7-
import org.checkerframework.checker.units.qual.C;
814
import org.openapitools.codegen.ClientOptInput;
915
import org.openapitools.codegen.CodegenConstants;
1016
import org.openapitools.codegen.DefaultGenerator;
1117
import org.openapitools.codegen.TestUtils;
1218
import org.openapitools.codegen.antlr4.KotlinLexer;
1319
import org.openapitools.codegen.antlr4.KotlinParser;
14-
import org.openapitools.codegen.antlr4.KotlinParserBaseListener;
1520
import org.openapitools.codegen.languages.KotlinServerCodegen;
1621
import org.openapitools.codegen.languages.KotlinSpringServerCodegen;
1722
import org.testng.Assert;
1823
import org.testng.annotations.DataProvider;
1924
import org.testng.annotations.Test;
2025

21-
import java.io.File;
22-
import java.io.IOException;
23-
import java.nio.file.Files;
24-
import java.nio.file.Path;
25-
import java.nio.file.Paths;
26-
import java.util.HashMap;
27-
import java.util.Map;
28-
29-
import static org.openapitools.codegen.CodegenConstants.*;
26+
import static org.openapitools.codegen.CodegenConstants.API_PACKAGE;
27+
import static org.openapitools.codegen.CodegenConstants.LIBRARY;
28+
import static org.openapitools.codegen.CodegenConstants.MODEL_PACKAGE;
29+
import static org.openapitools.codegen.CodegenConstants.PACKAGE_NAME;
3030
import static org.openapitools.codegen.TestUtils.assertFileContains;
3131
import static org.openapitools.codegen.TestUtils.assertFileNotContains;
3232
import static org.openapitools.codegen.languages.AbstractKotlinCodegen.USE_JAKARTA_EE;
3333
import static org.openapitools.codegen.languages.AbstractKotlinCodegen.USE_TAGS;
34-
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.*;
34+
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.INTERFACE_ONLY;
35+
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.JAVALIN5;
36+
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.JAVALIN6;
37+
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.JAXRS_SPEC;
38+
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.RETURN_RESPONSE;
3539
import static org.openapitools.codegen.languages.features.BeanValidationFeatures.USE_BEANVALIDATION;
3640

3741
public class KotlinServerCodegenTest {
@@ -222,6 +226,7 @@ public void issue18177Arrays() throws IOException {
222226
codegen.additionalProperties().put(INTERFACE_ONLY, true);
223227
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
224228
codegen.additionalProperties().put(LIBRARY, JAXRS_SPEC);
229+
codegen.additionalProperties().put(USE_TAGS, true);
225230
new DefaultGenerator().opts(new ClientOptInput()
226231
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/issue18177-array.yaml"))
227232
.config(codegen))
@@ -559,9 +564,9 @@ public void useTags_false_classNameFromTagsAndRootPathForJaxrsSpecLibrary() thro
559564

560565
assertFileContains(petApi,
561566
"class PetApi",
562-
"@Path(\"/\")",
563567
"@Path(\"/pet\")",
564-
"@Path(\"/pet/{petId}\")"
568+
"@Path(\"/findByStatus\")",
569+
"@Path(\"/{petId}\")"
565570
);
566571
assertFileNotContains(petApi, "@Path(\"/pet\")".replace("/pet", "/store"));
567572
}
@@ -586,9 +591,9 @@ public void useTags_notSpecified_behavesLikeUseTagsFalseForJaxrsSpecLibrary() th
586591

587592
assertFileContains(petApi,
588593
"class PetApi",
589-
"@Path(\"/\")",
590594
"@Path(\"/pet\")",
591-
"@Path(\"/pet/{petId}\")"
595+
"@Path(\"/findByStatus\")",
596+
"@Path(\"/{petId}\")"
592597
);
593598
assertFileNotContains(petApi, "@Path(\"/store\")");
594599
}

0 commit comments

Comments
 (0)