Skip to content

Commit

Permalink
[KOTLIN] [SPRING] Minor bug fixes to generator (#1001)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr4ke616 authored and jimschubert committed Sep 12, 2018
1 parent 2d99836 commit 1ae3403
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
public static final String SERVICE_IMPLEMENTATION = "serviceImplementation";

private String basePackage;
private String invokerPackage;
private String serverPort = "8080";
private String title = "OpenAPI Kotlin Spring";
private String resourceFolder = "src/main/resources";
Expand All @@ -79,12 +80,12 @@ public KotlinSpringServerCodegen() {
embeddedTemplateDir = templateDir = "kotlin-spring";

artifactId = "openapi-spring";
basePackage = "org.openapitools";
basePackage = invokerPackage = "org.openapitools";
apiPackage = "org.openapitools.api";
modelPackage = "org.openapitools.model";

addOption(TITLE, "server title name or client service name", title);
addOption(BASE_PACKAGE, "base package for generated code", basePackage);
addOption(BASE_PACKAGE, "base package (invokerPackage) for generated code", basePackage);
addOption(SERVER_PORT, "configuration the port in which the sever is to run on", serverPort);
addOption(CodegenConstants.MODEL_PACKAGE, "model package for generated code", modelPackage);
addOption(CodegenConstants.API_PACKAGE, "api package for generated code", apiPackage);
Expand Down Expand Up @@ -123,6 +124,14 @@ public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}

public String getInvokerPackage() {
return this.invokerPackage;
}

public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}

public String getServerPort() {
return this.serverPort;
}
Expand Down Expand Up @@ -225,13 +234,19 @@ public void processOpts() {
// used later in recursive import in postProcessingModels
importMapping.put("com.fasterxml.jackson.annotation.JsonProperty", "com.fasterxml.jackson.annotation.JsonCreator");

// TODO when adding invokerPackage
//importMapping.put("StringUtil", invokerPackage + ".StringUtil");

if (!additionalProperties.containsKey(CodegenConstants.LIBRARY)) {
additionalProperties.put(CodegenConstants.LIBRARY, library);
}

// Set basePackage from invokerPackage
if (!additionalProperties.containsKey(BASE_PACKAGE)
&& additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
this.setBasePackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
additionalProperties.put(BASE_PACKAGE, basePackage);
LOGGER.info("Set base package to invoker package (" + basePackage + ")");
}

if (additionalProperties.containsKey(BASE_PACKAGE)) {
this.setBasePackage((String) additionalProperties.get(BASE_PACKAGE));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import kotlin.collections.Map
@Api(value = "{{{baseName}}}", description = "The {{{baseName}}} API")
{{/swaggerAnnotations}}
{{=<% %>=}}
@RequestMapping("\${openapi.<%title%>.base-path:<%contextPath%>}")
@RequestMapping("\${api.base-path:<%contextPath%>}")
<%={{ }}=%>
{{#operations}}
class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) val service: {{classname}}Service{{/serviceInterface}}) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
spring.application.name={{title}}
server.port={{serverPort}}
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
spring:
application:
name: {{title}}

jackson:
serialization:
WRITE_DATES_AS_TIMESTAMPS: false

server:
port: {{serverPort}}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies {
{{/swaggerAnnotations}}
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
compile("com.fasterxml.jackson.module:jackson-module-kotlin")

testCompile("org.springframework.boot:spring-boot-starter-test") {
exclude(module = "junit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
{{#useBeanValidation}}
<!-- Bean Validation API support -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void testInitialConfigValues() throws Exception {
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "org.openapitools.api");
Assert.assertEquals(codegen.getBasePackage(), "org.openapitools");
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.BASE_PACKAGE), "org.openapitools");
Assert.assertEquals(codegen.getInvokerPackage(), "org.openapitools");
Assert.assertEquals(codegen.getServerPort(), "8080");
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.SERVER_PORT), "8080");
}
Expand Down Expand Up @@ -112,4 +113,14 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
Assert.assertFalse(codegen.getUseBeanValidation());
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.USE_BEANVALIDATION), false);
}

@Test
public void testSettingInvokerPackageToBasePackage() throws Exception {
final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE, "xyz.yyyyy.bbbb.invoker");
codegen.processOpts();

Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.bbbb.invoker");
Assert.assertEquals(codegen.getBasePackage(), "xyz.yyyyy.bbbb.invoker");
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.3-SNAPSHOT
3.3.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
compile("io.swagger:swagger-annotations:1.5.21")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
compile("com.fasterxml.jackson.module:jackson-module-kotlin")

testCompile("org.springframework.boot:spring-boot-starter-test") {
exclude(module = "junit")
Expand Down
4 changes: 4 additions & 0 deletions samples/server/openapi3/petstore/kotlin-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<!-- Bean Validation API support -->
<dependency>
<groupId>javax.validation</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import kotlin.collections.Map
@Controller
@Validated
@Api(value = "Pet", description = "The Pet API")
@RequestMapping("\${openapi.openAPIPetstore.base-path:/v2}")
@RequestMapping("\${api.base-path:/v2}")
class PetApiController(@Autowired(required = true) val service: PetApiService) {

@ApiOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlin.collections.Map
@Controller
@Validated
@Api(value = "Store", description = "The Store API")
@RequestMapping("\${openapi.openAPIPetstore.base-path:/v2}")
@RequestMapping("\${api.base-path:/v2}")
class StoreApiController(@Autowired(required = true) val service: StoreApiService) {

@ApiOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlin.collections.Map
@Controller
@Validated
@Api(value = "User", description = "The User API")
@RequestMapping("\${openapi.openAPIPetstore.base-path:/v2}")
@RequestMapping("\${api.base-path:/v2}")
class UserApiController(@Autowired(required = true) val service: UserApiService) {

@ApiOperation(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
spring.application.name=openAPIPetstore
server.port=8080
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
spring:
application:
name: openAPIPetstore

jackson:
serialization:
WRITE_DATES_AS_TIMESTAMPS: false

server:
port: 8080
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.3-SNAPSHOT
3.3.0-SNAPSHOT
1 change: 1 addition & 0 deletions samples/server/petstore/kotlin-springboot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
compile("io.swagger:swagger-annotations:1.5.21")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
compile("com.fasterxml.jackson.module:jackson-module-kotlin")

testCompile("org.springframework.boot:spring-boot-starter-test") {
exclude(module = "junit")
Expand Down
4 changes: 4 additions & 0 deletions samples/server/petstore/kotlin-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<!-- Bean Validation API support -->
<dependency>
<groupId>javax.validation</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import kotlin.collections.Map
@Controller
@Validated
@Api(value = "Pet", description = "The Pet API")
@RequestMapping("\${openapi.openAPIPetstore.base-path:/v2}")
@RequestMapping("\${api.base-path:/v2}")
class PetApiController(@Autowired(required = true) val service: PetApiService) {

@ApiOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlin.collections.Map
@Controller
@Validated
@Api(value = "Store", description = "The Store API")
@RequestMapping("\${openapi.openAPIPetstore.base-path:/v2}")
@RequestMapping("\${api.base-path:/v2}")
class StoreApiController(@Autowired(required = true) val service: StoreApiService) {

@ApiOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlin.collections.Map
@Controller
@Validated
@Api(value = "User", description = "The User API")
@RequestMapping("\${openapi.openAPIPetstore.base-path:/v2}")
@RequestMapping("\${api.base-path:/v2}")
class UserApiController(@Autowired(required = true) val service: UserApiService) {

@ApiOperation(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
spring.application.name=openAPIPetstore
server.port=8080
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
spring:
application:
name: openAPIPetstore

jackson:
serialization:
WRITE_DATES_AS_TIMESTAMPS: false

server:
port: 8080

0 comments on commit 1ae3403

Please sign in to comment.