Skip to content

Commit 52f2396

Browse files
committed
Enable useTags for all kotlin-server generators
1 parent c3e559b commit 52f2396

3 files changed

Lines changed: 60 additions & 64 deletions

File tree

docs/generators/kotlin-server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4848
|useCoroutines|Whether to use the Coroutines. This option is currently supported only when using jaxrs-spec library.| |false|
4949
|useJakartaEe|whether to use Jakarta EE namespace instead of javax| |false|
5050
|useMutiny|Whether to use Mutiny (should not be used with useCoroutines). This option is currently supported only when using jaxrs-spec library.| |false|
51-
|useTags|use tags for creating interface and controller classnames. This option is currently supported only when using jaxrs-spec library.| |true|
51+
|useTags|use tags for creating interface and controller classnames.| |true|
5252

5353
## IMPORT MAPPING
5454

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

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717

1818
package org.openapitools.codegen.languages;
1919

20-
import com.google.common.collect.ImmutableMap;
2120
import io.swagger.v3.oas.models.Operation;
2221
import java.io.File;
2322
import java.util.ArrayList;
24-
import java.util.Arrays;
2523
import java.util.EnumSet;
2624
import java.util.HashMap;
2725
import java.util.HashSet;
@@ -97,38 +95,6 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
9795
@Setter
9896
private boolean fixJacksonJsonTypeInfoInheritance = true;
9997

100-
// This is here to potentially warn the user when an option is not supported by the target framework.
101-
private Map<String, List<String>> optionsSupportedPerFramework = new ImmutableMap.Builder<String, List<String>>()
102-
.put(Constants.KTOR, Arrays.asList(
103-
Constants.AUTOMATIC_HEAD_REQUESTS,
104-
Constants.CONDITIONAL_HEADERS,
105-
Constants.HSTS,
106-
Constants.CORS,
107-
Constants.COMPRESSION,
108-
Constants.RESOURCES,
109-
Constants.METRICS,
110-
Constants.OMIT_GRADLE_WRAPPER
111-
))
112-
.put(Constants.KTOR2, Arrays.asList(
113-
Constants.AUTOMATIC_HEAD_REQUESTS,
114-
Constants.CONDITIONAL_HEADERS,
115-
Constants.HSTS,
116-
Constants.CORS,
117-
Constants.COMPRESSION,
118-
Constants.RESOURCES,
119-
Constants.METRICS,
120-
Constants.OMIT_GRADLE_WRAPPER
121-
))
122-
.put(Constants.JAXRS_SPEC, Arrays.asList(
123-
USE_BEANVALIDATION,
124-
USE_TAGS,
125-
Constants.USE_COROUTINES,
126-
Constants.USE_MUTINY,
127-
Constants.RETURN_RESPONSE,
128-
Constants.INTERFACE_ONLY
129-
))
130-
.build();
131-
13298
/**
13399
* Constructs an instance of `KotlinServerCodegen`.
134100
*/
@@ -429,7 +395,7 @@ public static class Constants {
429395
public static final String FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE = "fixJacksonJsonTypeInfoInheritance";
430396
public static final String FIX_JACKSON_JSON_TYPE_INFO_INHERITANCE_DESC = "When true (default), ensures Jackson polymorphism works correctly by: (1) always setting visible=true on @JsonTypeInfo, and (2) adding the discriminator property to child models with appropriate default values. When false, visible is only set to true if all children already define the discriminator property.";
431397
public static final String USE_TAGS = "useTags";
432-
public static final String USE_TAGS_DESC = "use tags for creating interface and controller classnames. This option is currently supported only when using jaxrs-spec library.";
398+
public static final String USE_TAGS_DESC = "use tags for creating interface and controller classnames.";
433399
}
434400

435401
@Override
@@ -729,7 +695,7 @@ public void postProcess() {
729695

730696
@Override
731697
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
732-
if (!Objects.equals(library, Constants.JAXRS_SPEC) || useTags) {
698+
if (useTags) {
733699
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
734700
return;
735701
}

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

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.openapitools.codegen.kotlin;
22

3+
import io.swagger.v3.oas.models.Operation;
34
import java.io.File;
45
import java.io.IOException;
56
import java.nio.file.Files;
67
import java.nio.file.Path;
78
import java.nio.file.Paths;
89
import java.util.HashMap;
10+
import java.util.List;
911
import java.util.Map;
12+
import org.openapitools.codegen.CodegenOperation;
1013
import org.antlr.v4.runtime.CharStreams;
1114
import org.antlr.v4.runtime.CommonTokenStream;
1215
import org.antlr.v4.runtime.tree.ParseTree;
@@ -515,33 +518,7 @@ public void fixJacksonJsonTypeInfoInheritance_canBeDisabled() throws IOException
515518
);
516519
}
517520

518-
@Test
519-
public void useTags_commonPathIsComputedForJaxrsSpecLibrary() throws IOException {
520-
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
521-
output.deleteOnExit();
522-
523-
KotlinServerCodegen codegen = new KotlinServerCodegen();
524-
codegen.setOutputDir(output.getAbsolutePath());
525-
codegen.additionalProperties().put(LIBRARY, JAXRS_SPEC);
526-
codegen.additionalProperties().put(USE_TAGS, true);
527-
528-
new DefaultGenerator().opts(new ClientOptInput()
529-
.openAPI(TestUtils.parseSpec("src/test/resources/2_0/petstore.yaml"))
530-
.config(codegen))
531-
.generate();
532-
533-
String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
534-
Path petApi = Paths.get(outputPath + "/apis/PetApi.kt");
535-
536-
assertFileContains(
537-
petApi,
538-
"@Path(\"/pet\")"
539-
);
540-
assertFileNotContains(
541-
petApi,
542-
"@Path(\"/\")"
543-
);
544-
}
521+
// ==================== useTags for JAXRS-SPEC ====================
545522

546523
@Test
547524
public void useTags_false_classNameFromTagsAndRootPathForJaxrsSpecLibrary() throws IOException {
@@ -597,4 +574,57 @@ public void useTags_notSpecified_behavesLikeUseTagsTrueForJaxrsSpecLibrary() thr
597574
assertFileNotContains(petApi, "@Path(\"/\")");
598575
assertFileNotContains(petApi, "@Path(\"/store\")");
599576
}
577+
578+
// ==================== useTags for all libraries ====================
579+
580+
@Test
581+
public void useTags_false_groupsByFirstPathSegment() {
582+
KotlinServerCodegen codegen = new KotlinServerCodegen();
583+
codegen.additionalProperties().put(LIBRARY, JAVALIN6);
584+
codegen.additionalProperties().put(USE_TAGS, false);
585+
codegen.processOpts();
586+
587+
CodegenOperation co = new CodegenOperation();
588+
co.operationId = "findByStatus";
589+
Map<String, List<CodegenOperation>> groups = new HashMap<>();
590+
591+
codegen.addOperationToGroup("Pet", "/pet/findByStatus", new Operation(), co, groups);
592+
593+
Assert.assertTrue(groups.containsKey("pet"));
594+
Assert.assertEquals(co.baseName, "pet");
595+
}
596+
597+
@Test
598+
public void useTags_false_rootPath_groupsAsDefault() {
599+
KotlinServerCodegen codegen = new KotlinServerCodegen();
600+
codegen.additionalProperties().put(LIBRARY, JAVALIN6);
601+
codegen.additionalProperties().put(USE_TAGS, false);
602+
codegen.processOpts();
603+
604+
CodegenOperation co = new CodegenOperation();
605+
co.operationId = "getRoot";
606+
Map<String, List<CodegenOperation>> groups = new HashMap<>();
607+
608+
codegen.addOperationToGroup("Root", "/", new Operation(), co, groups);
609+
610+
Assert.assertTrue(groups.containsKey("default"));
611+
Assert.assertEquals(co.baseName, "default");
612+
}
613+
614+
@Test
615+
public void useTags_false_pathParamOnly_groupsAsDefault() {
616+
KotlinServerCodegen codegen = new KotlinServerCodegen();
617+
codegen.additionalProperties().put(LIBRARY, JAVALIN6);
618+
codegen.additionalProperties().put(USE_TAGS, false);
619+
codegen.processOpts();
620+
621+
CodegenOperation co = new CodegenOperation();
622+
co.operationId = "getById";
623+
Map<String, List<CodegenOperation>> groups = new HashMap<>();
624+
625+
codegen.addOperationToGroup("Resource", "/{uuid}", new Operation(), co, groups);
626+
627+
Assert.assertTrue(groups.containsKey("default"));
628+
Assert.assertEquals(co.baseName, "default");
629+
}
600630
}

0 commit comments

Comments
 (0)