Skip to content

Commit de78670

Browse files
author
andrewwilsonnew
committed
second pass
1 parent cc6d9d5 commit de78670

16 files changed

Lines changed: 88 additions & 22 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ public class KotlinMiskServerCodegen extends AbstractKotlinCodegen implements Be
5353

5454
private static final String ROOT_PACKAGE = "rootPackage";
5555
public static final String GENERATE_STUB_IMPL_CLASSES = "generateStubImplClasses";
56+
public static final String ADD_MODEL_MOSHI_JSON_ANNOTATION = "addModelMoshiJsonAnnotation";
5657

5758
private boolean useBeanValidation = true;
5859

5960
@Setter
6061
private boolean generateStubImplClasses = false;
6162

63+
@Setter
64+
private boolean addModelMoshiJsonAnnotation = true;
65+
6266
protected String rootPackage = "org.openapitools.server.api";
6367
protected String apiVersion = "1.0.0-SNAPSHOT";
6468

@@ -84,6 +88,7 @@ public KotlinMiskServerCodegen() {
8488

8589
addSwitch(USE_BEANVALIDATION, "Use BeanValidation API annotations to validate data types", useBeanValidation);
8690
addSwitch(GENERATE_STUB_IMPL_CLASSES, "Generate Stub Impl Classes", generateStubImplClasses);
91+
addSwitch(ADD_MODEL_MOSHI_JSON_ANNOTATION, "Add a Moshi JSON adapter annotation to all model classes", addModelMoshiJsonAnnotation);
8792

8893
modifyFeatureSet(features -> features
8994
.includeDocumentationFeatures(DocumentationFeature.Readme)
@@ -170,6 +175,10 @@ public void processOpts() {
170175
}
171176
writePropertyBack(GENERATE_STUB_IMPL_CLASSES, generateStubImplClasses);
172177

178+
if (additionalProperties.containsKey(ADD_MODEL_MOSHI_JSON_ANNOTATION)) {
179+
setAddModelMoshiJsonAnnotation(convertPropertyToBoolean(ADD_MODEL_MOSHI_JSON_ANNOTATION));
180+
}
181+
writePropertyBack(ADD_MODEL_MOSHI_JSON_ANNOTATION, addModelMoshiJsonAnnotation);
173182
applyJakartaPackage();
174183

175184
String apiModuleFolder = (sourceFolder + File.separator + apiPackage).replace(".", File.separator);

modules/openapi-generator/src/main/resources/kotlin-misk/apiAction.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ import misk.web.mediatype.MediaTypes
3232
{{#imports}}import {{import}}
3333
{{/imports}}
3434

35+
/**
36+
* @TODO("Fill out implementation")
37+
*/
3538
{{#operations}}
3639
@Singleton
3740
class {{classname}}Action @Inject constructor(
@@ -44,7 +47,8 @@ class {{classname}}Action @Inject constructor(
4447
@RequestContentType({{#consumes}}{{{mediaType}}}{{^-last}}, {{/-last}}{{/consumes}}){{/hasConsumes}}{{#hasProduces}}
4548
@ResponseContentType({{#produces}}{{{mediaType}}}{{^-last}}, {{/-last}}{{/produces}}){{/hasProduces}}
4649
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
47-
override fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}}){{#returnType}}: {{{returnType}}}{{/returnType}} {
50+
override fun {{operationId}}({{#allParams}}
51+
{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}}){{#returnType}}: {{{returnType}}}{{/returnType}} {
4852
TODO()
4953
}
5054
{{/operation}}

modules/openapi-generator/src/main/resources/kotlin-misk/apiImpl.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class {{classname}}Impl @Inject constructor(
3030
): {{classname}} {
3131
{{#operation}}
3232

33-
override fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}}){{#returnType}}: {{{returnType}}}{{/returnType}} {
33+
override fun {{operationId}}({{#allParams}}
34+
{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}}){{#returnType}}: {{{returnType}}}{{/returnType}} {
3435
TODO()
3536
}
3637
{{/operation}}

modules/openapi-generator/src/main/resources/kotlin-misk/apiInterface.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import misk.web.RequestHeader
2323
interface {{classname}} {
2424
{{#operation}}
2525

26-
fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}}){{#returnType}}: {{{returnType}}}{{/returnType}}
26+
fun {{operationId}}({{#allParams}}
27+
{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{^-last}}, {{/-last}}{{/allParams}}){{#returnType}}: {{{returnType}}}{{/returnType}} {
2728
{{/operation}}
2829
}
2930
{{/operations}}

modules/openapi-generator/src/main/resources/kotlin-misk/model.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package {{modelPackage}}
33
{{#imports}}
44
import {{import}}
55
{{/imports}}
6-
6+
import com.squareup.moshi.JsonClass
77
{{#models}}
88
{{#model}}
99
{{#isEnum}}
@@ -16,6 +16,8 @@ enum class {{classname}} {
1616
}
1717
{{/isEnum}}
1818
{{^isEnum}}
19+
20+
@JsonClass(generateAdapter = true)
1921
data class {{classname}}(
2022
{{#vars}}
2123
{{#description}}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ protected void verifyOptions() {
3939
verify(codegen).setUseBeanValidation(Boolean.valueOf(KotlinMiskServerCodegenOptionsProvider.USE_BEAN_VALIDATION));
4040
verify(codegen).setModuleClassName(KotlinMiskServerCodegenOptionsProvider.MODULE_CLASS_NAME);
4141
verify(codegen).setGenerateStubImplClasses(Boolean.valueOf(KotlinMiskServerCodegenOptionsProvider.GENERATE_STUB_IMPL_CLASSES));
42+
verify(codegen).setAddModelMoshiJsonAnnotation(Boolean.valueOf(KotlinMiskServerCodegenOptionsProvider.ADD_MODEL_MOSHI_JSON_ANNOTATION));
4243
}
4344
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/KotlinMiskServerCodegenOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class KotlinMiskServerCodegenOptionsProvider implements OptionsProvider {
2424
public static final String ADDITIONAL_MODEL_TYPE_ANNOTATIONS_VALUE = "";
2525
public static final String USE_BEAN_VALIDATION = "false";
2626
public static final String GENERATE_STUB_IMPL_CLASSES = "false";
27+
public static final String ADD_MODEL_MOSHI_JSON_ANNOTATION = "true";
2728
public static final String MODULE_CLASS_NAME = "OpenApiModule";
2829

2930
@Override
@@ -53,6 +54,7 @@ public Map<String, String> createOptions() {
5354
.put(KotlinMiskServerCodegen.MODULE_CLASS_NAME, MODULE_CLASS_NAME)
5455
.put(BeanValidationFeatures.USE_BEANVALIDATION, USE_BEAN_VALIDATION)
5556
.put(KotlinMiskServerCodegen.GENERATE_STUB_IMPL_CLASSES, GENERATE_STUB_IMPL_CLASSES)
57+
.put(KotlinMiskServerCodegen.ADD_MODEL_MOSHI_JSON_ANNOTATION, ADD_MODEL_MOSHI_JSON_ANNOTATION)
5658
.build();
5759
}
5860

samples/server/petstore/kotlin-misk-config/src/main/kotlin/org/openapitools/server/api/api/PetApiAction.kt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import misk.web.mediatype.MediaTypes
3030
import org.openapitools.server.api.model.ModelApiResponse
3131
import org.openapitools.server.api.model.Pet
3232

33+
/**
34+
* @TODO("Fill out implementation")
35+
*/
3336
@Singleton
3437
class PetApiAction @Inject constructor(
3538
private val petApi: PetApi
@@ -40,38 +43,44 @@ class PetApiAction @Inject constructor(
4043
@RequestContentType(MediaTypes.APPLICATION_JSON, MediaTypes.APPLICATION_XML)
4144
@ResponseContentType(MediaTypes.APPLICATION_XML, MediaTypes.APPLICATION_JSON)
4245
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
43-
override fun addPet(@Valid @RequestBody pet: Pet): Pet {
46+
override fun addPet(
47+
@Valid @RequestBody pet: Pet): Pet {
4448
TODO()
4549
}
4650

4751
@Delete("/pet/{petId}")
4852
@Description("Deletes a pet")
4953
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
50-
override fun deletePet(@PathParam("petId") petId: kotlin.Long, @RequestHeader(value = "api_key") apiKey: kotlin.String?) {
54+
override fun deletePet(
55+
@PathParam("petId") petId: kotlin.Long,
56+
@RequestHeader(value = "api_key") apiKey: kotlin.String?) {
5157
TODO()
5258
}
5359

5460
@Get("/pet/findByStatus")
5561
@Description("Finds Pets by status")
5662
@ResponseContentType(MediaTypes.APPLICATION_XML, MediaTypes.APPLICATION_JSON)
5763
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
58-
override fun findPetsByStatus( @QueryParam(value = "status") status: kotlin.Array<kotlin.String>): kotlin.Array<Pet> {
64+
override fun findPetsByStatus(
65+
@QueryParam(value = "status") status: kotlin.Array<kotlin.String>): kotlin.Array<Pet> {
5966
TODO()
6067
}
6168

6269
@Get("/pet/findByTags")
6370
@Description("Finds Pets by tags")
6471
@ResponseContentType(MediaTypes.APPLICATION_XML, MediaTypes.APPLICATION_JSON)
6572
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
66-
override fun findPetsByTags( @QueryParam(value = "tags") tags: kotlin.Array<kotlin.String>): kotlin.Array<Pet> {
73+
override fun findPetsByTags(
74+
@QueryParam(value = "tags") tags: kotlin.Array<kotlin.String>): kotlin.Array<Pet> {
6775
TODO()
6876
}
6977

7078
@Get("/pet/{petId}")
7179
@Description("Find pet by ID")
7280
@ResponseContentType(MediaTypes.APPLICATION_XML, MediaTypes.APPLICATION_JSON)
7381
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
74-
override fun getPetById(@PathParam("petId") petId: kotlin.Long): Pet {
82+
override fun getPetById(
83+
@PathParam("petId") petId: kotlin.Long): Pet {
7584
TODO()
7685
}
7786

@@ -80,15 +89,19 @@ class PetApiAction @Inject constructor(
8089
@RequestContentType(MediaTypes.APPLICATION_JSON, MediaTypes.APPLICATION_XML)
8190
@ResponseContentType(MediaTypes.APPLICATION_XML, MediaTypes.APPLICATION_JSON)
8291
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
83-
override fun updatePet(@Valid @RequestBody pet: Pet): Pet {
92+
override fun updatePet(
93+
@Valid @RequestBody pet: Pet): Pet {
8494
TODO()
8595
}
8696

8797
@Post("/pet/{petId}")
8898
@Description("Updates a pet in the store with form data")
8999
@RequestContentType(MediaTypes.APPLICATION_FORM_URLENCODED)
90100
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
91-
override fun updatePetWithForm(@PathParam("petId") petId: kotlin.Long, @QueryParam(value = "name") name: kotlin.String? , @QueryParam(value = "status") status: kotlin.String? ) {
101+
override fun updatePetWithForm(
102+
@PathParam("petId") petId: kotlin.Long,
103+
@QueryParam(value = "name") name: kotlin.String? ,
104+
@QueryParam(value = "status") status: kotlin.String? ) {
92105
TODO()
93106
}
94107

@@ -97,7 +110,10 @@ class PetApiAction @Inject constructor(
97110
@RequestContentType(MediaTypes.FORM_DATA)
98111
@ResponseContentType(MediaTypes.APPLICATION_JSON)
99112
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
100-
override fun uploadFile(@PathParam("petId") petId: kotlin.Long, @QueryParam(value = "additionalMetadata") additionalMetadata: kotlin.String? , @Valid file: HttpCall): ModelApiResponse {
113+
override fun uploadFile(
114+
@PathParam("petId") petId: kotlin.Long,
115+
@QueryParam(value = "additionalMetadata") additionalMetadata: kotlin.String? ,
116+
@Valid file: HttpCall): ModelApiResponse {
101117
TODO()
102118
}
103119
}

samples/server/petstore/kotlin-misk-config/src/main/kotlin/org/openapitools/server/api/api/StoreApiAction.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import misk.web.interceptors.LogRequestResponse
2929
import misk.web.mediatype.MediaTypes
3030
import org.openapitools.server.api.model.Order
3131

32+
/**
33+
* @TODO("Fill out implementation")
34+
*/
3235
@Singleton
3336
class StoreApiAction @Inject constructor(
3437
private val storeApi: StoreApi
@@ -37,7 +40,8 @@ class StoreApiAction @Inject constructor(
3740
@Delete("/store/order/{orderId}")
3841
@Description("Delete purchase order by ID")
3942
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
40-
override fun deleteOrder(@PathParam("orderId") orderId: kotlin.String) {
43+
override fun deleteOrder(
44+
@PathParam("orderId") orderId: kotlin.String) {
4145
TODO()
4246
}
4347

@@ -53,7 +57,8 @@ class StoreApiAction @Inject constructor(
5357
@Description("Find purchase order by ID")
5458
@ResponseContentType(MediaTypes.APPLICATION_XML, MediaTypes.APPLICATION_JSON)
5559
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
56-
override fun getOrderById(@Min(1L) @Max(5L) @PathParam("orderId") orderId: kotlin.Long): Order {
60+
override fun getOrderById(
61+
@Min(1L) @Max(5L) @PathParam("orderId") orderId: kotlin.Long): Order {
5762
TODO()
5863
}
5964

@@ -62,7 +67,8 @@ class StoreApiAction @Inject constructor(
6267
@RequestContentType(MediaTypes.APPLICATION_JSON)
6368
@ResponseContentType(MediaTypes.APPLICATION_XML, MediaTypes.APPLICATION_JSON)
6469
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
65-
override fun placeOrder(@Valid @RequestBody order: Order): Order {
70+
override fun placeOrder(
71+
@Valid @RequestBody order: Order): Order {
6672
TODO()
6773
}
6874
}

samples/server/petstore/kotlin-misk-config/src/main/kotlin/org/openapitools/server/api/api/UserApiAction.kt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import misk.web.interceptors.LogRequestResponse
2929
import misk.web.mediatype.MediaTypes
3030
import org.openapitools.server.api.model.User
3131

32+
/**
33+
* @TODO("Fill out implementation")
34+
*/
3235
@Singleton
3336
class UserApiAction @Inject constructor(
3437
private val userApi: UserApi
@@ -38,46 +41,53 @@ class UserApiAction @Inject constructor(
3841
@Description("Create user")
3942
@RequestContentType(MediaTypes.APPLICATION_JSON)
4043
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
41-
override fun createUser(@Valid @RequestBody user: User) {
44+
override fun createUser(
45+
@Valid @RequestBody user: User) {
4246
TODO()
4347
}
4448

4549
@Post("/user/createWithArray")
4650
@Description("Creates list of users with given input array")
4751
@RequestContentType(MediaTypes.APPLICATION_JSON)
4852
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
49-
override fun createUsersWithArrayInput(@Valid @RequestBody user: kotlin.Array<User>) {
53+
override fun createUsersWithArrayInput(
54+
@Valid @RequestBody user: kotlin.Array<User>) {
5055
TODO()
5156
}
5257

5358
@Post("/user/createWithList")
5459
@Description("Creates list of users with given input array")
5560
@RequestContentType(MediaTypes.APPLICATION_JSON)
5661
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
57-
override fun createUsersWithListInput(@Valid @RequestBody user: kotlin.Array<User>) {
62+
override fun createUsersWithListInput(
63+
@Valid @RequestBody user: kotlin.Array<User>) {
5864
TODO()
5965
}
6066

6167
@Delete("/user/{username}")
6268
@Description("Delete user")
6369
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
64-
override fun deleteUser(@PathParam("username") username: kotlin.String) {
70+
override fun deleteUser(
71+
@PathParam("username") username: kotlin.String) {
6572
TODO()
6673
}
6774

6875
@Get("/user/{username}")
6976
@Description("Get user by user name")
7077
@ResponseContentType(MediaTypes.APPLICATION_XML, MediaTypes.APPLICATION_JSON)
7178
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
72-
override fun getUserByName(@PathParam("username") username: kotlin.String): User {
79+
override fun getUserByName(
80+
@PathParam("username") username: kotlin.String): User {
7381
TODO()
7482
}
7583

7684
@Get("/user/login")
7785
@Description("Logs user into the system")
7886
@ResponseContentType(MediaTypes.APPLICATION_XML, MediaTypes.APPLICATION_JSON)
7987
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
80-
override fun loginUser( @QueryParam(value = "username") username: kotlin.String, @QueryParam(value = "password") password: kotlin.String): kotlin.String {
88+
override fun loginUser(
89+
@QueryParam(value = "username") username: kotlin.String,
90+
@QueryParam(value = "password") password: kotlin.String): kotlin.String {
8191
TODO()
8292
}
8393

@@ -92,7 +102,9 @@ class UserApiAction @Inject constructor(
92102
@Description("Updated user")
93103
@RequestContentType(MediaTypes.APPLICATION_JSON)
94104
@LogRequestResponse(bodySampling = 1.0, errorBodySampling = 1.0)
95-
override fun updateUser(@PathParam("username") username: kotlin.String, @Valid @RequestBody user: User) {
105+
override fun updateUser(
106+
@PathParam("username") username: kotlin.String,
107+
@Valid @RequestBody user: User) {
96108
TODO()
97109
}
98110
}

0 commit comments

Comments
 (0)