Skip to content

Commit

Permalink
Issue 5497: Support the use of tags in the delegated Spring Kotlin ge…
Browse files Browse the repository at this point in the history
…nerator. (#5499)

Co-authored-by: Jim Schubert <[email protected]>
  • Loading branch information
dumitru-petrusca and jimschubert authored Jun 7, 2020
1 parent 7baa72e commit 057c429
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/generators/kotlin-spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ sidebar_label: kotlin-spring
|swaggerAnnotations|generate swagger annotations to go alongside controllers and models| |false|
|title|server title name or client service name| |OpenAPI Kotlin Spring|
|useBeanValidation|Use BeanValidation API annotations to validate data types| |true|
|useTags|Whether to use tags for creating interface and controller class names| |false|

## IMPORT MAPPING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
public static final String REACTIVE = "reactive";
public static final String INTERFACE_ONLY = "interfaceOnly";
public static final String DELEGATE_PATTERN = "delegatePattern";
public static final String USE_TAGS = "useTags";

private String basePackage;
private String invokerPackage;
Expand All @@ -81,6 +82,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
private boolean reactive = false;
private boolean interfaceOnly = false;
private boolean delegatePattern = false;
protected boolean useTags = false;

public KotlinSpringServerCodegen() {
super();
Expand Down Expand Up @@ -152,6 +154,7 @@ public KotlinSpringServerCodegen() {
addSwitch(REACTIVE, "use coroutines for reactive behavior", reactive);
addSwitch(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files.", interfaceOnly);
addSwitch(DELEGATE_PATTERN, "Whether to generate the server files using the delegate pattern", delegatePattern);
addSwitch(USE_TAGS, "Whether to use tags for creating interface and controller class names", useTags);
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
setLibrary(SPRING_BOOT);

Expand Down Expand Up @@ -245,6 +248,10 @@ public void setDelegatePattern(boolean delegatePattern) {
this.delegatePattern = delegatePattern;
}

public void setUseTags(boolean useTags) {
this.useTags = useTags;
}

@Override
public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation;
Expand Down Expand Up @@ -369,6 +376,10 @@ public void processOpts() {
}
}

if (additionalProperties.containsKey(USE_TAGS)) {
this.setUseTags(Boolean.parseBoolean(additionalProperties.get(USE_TAGS).toString()));
}

modelTemplateFiles.put("model.mustache", ".kt");

if (!this.interfaceOnly && this.delegatePattern) {
Expand Down Expand Up @@ -440,7 +451,7 @@ protected Builder<String, Lambda> addMustacheLambdas() {

@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
if (library.equals(SPRING_BOOT) && this.delegatePattern) {
if (library.equals(SPRING_BOOT) && !useTags) {
String basePath = resourcePath;
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.codegen.kotlin.spring;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.testing.Helpers;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
Expand All @@ -21,6 +22,7 @@
import java.io.File;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;

public class KotlinSpringServerCodegenTest {

Expand Down Expand Up @@ -183,4 +185,28 @@ public void testDelegatePattern() {

Assert.assertTrue(codegen.supportingFiles().stream().anyMatch(supportingFile -> supportingFile.templateFile.equals("apiUtil.mustache")));
}

@Test(description = "test delegate with tags")
public void delegateWithTags() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); //may be move to /build
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(KotlinSpringServerCodegen.DELEGATE_PATTERN, true);
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_TAGS, true);

List<File> files = new DefaultGenerator()
.opts(
new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/issue5497-use-tags-kotlin.yaml"))
.config(codegen)
)
.generate();

Helpers.assertContainsAllOf(files,
new File(output, "src/main/kotlin/org/openapitools/api/TestV1ApiController.kt"),
new File(output, "src/main/kotlin/org/openapitools/api/TestV1ApiDelegate.kt"),
new File(output, "src/main/kotlin/org/openapitools/api/TestV2ApiController.kt"),
new File(output, "src/main/kotlin/org/openapitools/api/TestV2ApiDelegate.kt")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: "3.0.1"
info:
title: test
version: "1.0"

paths:

/api/v1/test:
get:
tags: [Test v1]
operationId: test1
responses:
200:
description: OK

/api/v2/test:
get:
tags: [Test v2]
operationId: test2
responses:
200:
description: OK
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.3.1-SNAPSHOT
4.3.1-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import kotlin.collections.Map

@RestController
@Validated
@Api(value = "Pet", description = "The Pet API")
@Api(value = "pet", description = "The pet API")
@RequestMapping("\${api.base-path:/v2}")
class PetApiController(@Autowired(required = true) val service: PetApiService) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlin.collections.Map

@RestController
@Validated
@Api(value = "Store", description = "The Store API")
@Api(value = "store", description = "The store API")
@RequestMapping("\${api.base-path:/v2}")
class StoreApiController(@Autowired(required = true) val service: StoreApiService) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlin.collections.Map

@RestController
@Validated
@Api(value = "User", description = "The User API")
@Api(value = "user", description = "The user API")
@RequestMapping("\${api.base-path:/v2}")
class UserApiController(@Autowired(required = true) val service: UserApiService) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import kotlin.collections.Map

@RestController
@Validated
@Api(value = "Pet", description = "The Pet API")
@Api(value = "pet", description = "The pet API")
@RequestMapping("\${api.base-path:/v2}")
class PetApiController(@Autowired(required = true) val service: PetApiService) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import kotlin.collections.Map

@RestController
@Validated
@Api(value = "Store", description = "The Store API")
@Api(value = "store", description = "The store API")
@RequestMapping("\${api.base-path:/v2}")
class StoreApiController(@Autowired(required = true) val service: StoreApiService) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import kotlin.collections.Map

@RestController
@Validated
@Api(value = "User", description = "The User API")
@Api(value = "user", description = "The user API")
@RequestMapping("\${api.base-path:/v2}")
class UserApiController(@Autowired(required = true) val service: UserApiService) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import kotlin.collections.Map

@RestController
@Validated
@Api(value = "Pet", description = "The Pet API")
@Api(value = "pet", description = "The pet API")
@RequestMapping("\${api.base-path:/v2}")
class PetApiController(@Autowired(required = true) val service: PetApiService) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlin.collections.Map

@RestController
@Validated
@Api(value = "Store", description = "The Store API")
@Api(value = "store", description = "The store API")
@RequestMapping("\${api.base-path:/v2}")
class StoreApiController(@Autowired(required = true) val service: StoreApiService) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlin.collections.Map

@RestController
@Validated
@Api(value = "User", description = "The User API")
@Api(value = "user", description = "The user API")
@RequestMapping("\${api.base-path:/v2}")
class UserApiController(@Autowired(required = true) val service: UserApiService) {

Expand Down

0 comments on commit 057c429

Please sign in to comment.