diff --git a/modules/openapi-generator/pom.xml b/modules/openapi-generator/pom.xml index 0b7c4ce16590..f564fa4689cc 100644 --- a/modules/openapi-generator/pom.xml +++ b/modules/openapi-generator/pom.xml @@ -440,7 +440,6 @@ org.projectlombok lombok ${lombok.version} - test diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java index 950d103f5bcc..158e4b114af5 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java @@ -22,6 +22,9 @@ import com.samskivert.mustache.Template; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; import org.openapitools.codegen.CliOption; import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenModel; @@ -380,7 +383,7 @@ public void setUseTags(boolean useTags) { } public boolean isAppendRequestToHandler() { - return Boolean.parseBoolean(additionalProperties.get(APPEND_REQUEST_TO_HANDLER).toString()); + return Boolean.parseBoolean(additionalProperties.getOrDefault(APPEND_REQUEST_TO_HANDLER, false).toString()); } public void setUseSpringBoot3(boolean isSpringBoot3) { @@ -906,6 +909,9 @@ public void setReturnContainer(final String returnContainer) { final List allParams = operation.allParams; if (allParams != null) { + if (this.isAppendRequestToHandler()) { + allParams.add(new RequestCodegenParameter(true)); + } allParams.forEach(param -> // This is necessary in case 'modelMutable' is enabled, // to prevent Spring Request handlers from being generated with @@ -996,4 +1002,11 @@ protected boolean needToImport(String type) { // provides extra protection against improperly trying to import language primitives and java types return !type.startsWith("org.springframework.") && super.needToImport(type); } + + @AllArgsConstructor + @Data + @EqualsAndHashCode(callSuper = true) + static class RequestCodegenParameter extends CodegenParameter { + boolean isRequestObject; + } } diff --git a/modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache b/modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache index 8672c263cf5e..6c308d944823 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache @@ -90,7 +90,7 @@ class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) v produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}}, consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}} ) - {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}{{#appendRequestToHandler}}, serverHttpRequest: ServerHttpRequest{{/appendRequestToHandler}}): ResponseEntity<{{>returnTypes}}> { + {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}> { return {{>returnValue}} } {{/operation}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache b/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache index 97501bac9542..3a831d306876 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache @@ -102,7 +102,7 @@ interface {{classname}} { produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}}, consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}} ) - {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}{{#appendRequestToHandler}}, serverHttpRequest: ServerHttpRequest{{/appendRequestToHandler}}): ResponseEntity<{{>returnTypes}}>{{^skipDefaultInterface}} { + {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}>{{^skipDefaultInterface}} { {{^isDelegate}} return {{>returnValue}} {{/isDelegate}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-spring/requesObject.mustache b/modules/openapi-generator/src/main/resources/kotlin-spring/requesObject.mustache new file mode 100644 index 000000000000..86737ff049a1 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/kotlin-spring/requesObject.mustache @@ -0,0 +1 @@ +{{#isRequestObject}}serverHttpRequest: ServerHttpRequest{{/isRequestObject}} \ No newline at end of file diff --git a/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/PetApi.kt b/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/PetApi.kt index 999d3317e427..84f672f71b46 100644 --- a/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/PetApi.kt +++ b/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/PetApi.kt @@ -54,7 +54,7 @@ interface PetApi { value = ["/pet"], consumes = ["application/json", "application/xml"] ) - fun addPet(@Parameter(description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody body: Pet, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun addPet(@Parameter(description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody body: Pet,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -72,7 +72,7 @@ interface PetApi { method = [RequestMethod.DELETE], value = ["/pet/{petId}"] ) - fun deletePet(@Parameter(description = "Pet id to delete", required = true) @PathVariable("petId") petId: kotlin.Long,@Parameter(description = "", `in` = ParameterIn.HEADER) @RequestHeader(value = "api_key", required = false) apiKey: kotlin.String?, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun deletePet(@Parameter(description = "Pet id to delete", required = true) @PathVariable("petId") petId: kotlin.Long,@Parameter(description = "", `in` = ParameterIn.HEADER) @RequestHeader(value = "api_key", required = false) apiKey: kotlin.String?,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -92,7 +92,7 @@ interface PetApi { value = ["/pet/findByStatus"], produces = ["application/xml", "application/json"] ) - fun findPetsByStatus(@NotNull @Parameter(description = "Status values that need to be considered for filter", required = true, schema = Schema(allowableValues = ["available", "pending", "sold"])) @Valid @RequestParam(value = "status", required = true) status: kotlin.collections.List, serverHttpRequest: ServerHttpRequest): ResponseEntity> { + fun findPetsByStatus(@NotNull @Parameter(description = "Status values that need to be considered for filter", required = true, schema = Schema(allowableValues = ["available", "pending", "sold"])) @Valid @RequestParam(value = "status", required = true) status: kotlin.collections.List,serverHttpRequest: ServerHttpRequest): ResponseEntity> { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -112,7 +112,7 @@ interface PetApi { value = ["/pet/findByTags"], produces = ["application/xml", "application/json"] ) - fun findPetsByTags(@NotNull @Parameter(description = "Tags to filter by", required = true) @Valid @RequestParam(value = "tags", required = true) tags: kotlin.collections.List, serverHttpRequest: ServerHttpRequest): ResponseEntity> { + fun findPetsByTags(@NotNull @Parameter(description = "Tags to filter by", required = true) @Valid @RequestParam(value = "tags", required = true) tags: kotlin.collections.List,serverHttpRequest: ServerHttpRequest): ResponseEntity> { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -133,7 +133,7 @@ interface PetApi { value = ["/pet/{petId}"], produces = ["application/xml", "application/json"] ) - fun getPetById(@Parameter(description = "ID of pet to return", required = true) @PathVariable("petId") petId: kotlin.Long, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun getPetById(@Parameter(description = "ID of pet to return", required = true) @PathVariable("petId") petId: kotlin.Long,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -154,7 +154,7 @@ interface PetApi { value = ["/pet"], consumes = ["application/json", "application/xml"] ) - fun updatePet(@Parameter(description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody body: Pet, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun updatePet(@Parameter(description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody body: Pet,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -173,7 +173,7 @@ interface PetApi { value = ["/pet/{petId}"], consumes = ["application/x-www-form-urlencoded"] ) - fun updatePetWithForm(@Parameter(description = "ID of pet that needs to be updated", required = true) @PathVariable("petId") petId: kotlin.Long,@Parameter(description = "Updated name of the pet") @RequestParam(value = "name", required = false) name: kotlin.String? ,@Parameter(description = "Updated status of the pet") @RequestParam(value = "status", required = false) status: kotlin.String? , serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun updatePetWithForm(@Parameter(description = "ID of pet that needs to be updated", required = true) @PathVariable("petId") petId: kotlin.Long,@Parameter(description = "Updated name of the pet") @RequestParam(value = "name", required = false) name: kotlin.String? ,@Parameter(description = "Updated status of the pet") @RequestParam(value = "status", required = false) status: kotlin.String? ,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -193,7 +193,7 @@ interface PetApi { produces = ["application/json"], consumes = ["multipart/form-data"] ) - fun uploadFile(@Parameter(description = "ID of pet to update", required = true) @PathVariable("petId") petId: kotlin.Long,@Parameter(description = "Additional data to pass to server") @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? ,@Parameter(description = "file detail") @Valid @RequestPart("file") file: org.springframework.core.io.Resource?, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun uploadFile(@Parameter(description = "ID of pet to update", required = true) @PathVariable("petId") petId: kotlin.Long,@Parameter(description = "Additional data to pass to server") @RequestParam(value = "additionalMetadata", required = false) additionalMetadata: kotlin.String? ,@Parameter(description = "file detail") @Valid @RequestPart("file") file: org.springframework.core.io.Resource?,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } } diff --git a/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/StoreApi.kt b/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/StoreApi.kt index 59dcb3967f5c..fc4c49d62120 100644 --- a/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/StoreApi.kt +++ b/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/StoreApi.kt @@ -52,7 +52,7 @@ interface StoreApi { method = [RequestMethod.DELETE], value = ["/store/order/{orderId}"] ) - fun deleteOrder(@Parameter(description = "ID of the order that needs to be deleted", required = true) @PathVariable("orderId") orderId: kotlin.String, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun deleteOrder(@Parameter(description = "ID of the order that needs to be deleted", required = true) @PathVariable("orderId") orderId: kotlin.String,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -71,7 +71,7 @@ interface StoreApi { value = ["/store/inventory"], produces = ["application/json"] ) - fun getInventory(, serverHttpRequest: ServerHttpRequest): ResponseEntity> { + fun getInventory(serverHttpRequest: ServerHttpRequest): ResponseEntity> { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -91,7 +91,7 @@ interface StoreApi { value = ["/store/order/{orderId}"], produces = ["application/xml", "application/json"] ) - fun getOrderById(@Min(1L) @Max(5L) @Parameter(description = "ID of pet that needs to be fetched", required = true) @PathVariable("orderId") orderId: kotlin.Long, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun getOrderById(@Min(1L) @Max(5L) @Parameter(description = "ID of pet that needs to be fetched", required = true) @PathVariable("orderId") orderId: kotlin.Long,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -110,7 +110,7 @@ interface StoreApi { value = ["/store/order"], produces = ["application/xml", "application/json"] ) - fun placeOrder(@Parameter(description = "order placed for purchasing the pet", required = true) @Valid @RequestBody body: Order, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun placeOrder(@Parameter(description = "order placed for purchasing the pet", required = true) @Valid @RequestBody body: Order,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } } diff --git a/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/UserApi.kt b/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/UserApi.kt index af517bd5f980..9980ae801819 100644 --- a/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/UserApi.kt +++ b/samples/server/petstore/kotlin-springboot-request/src/main/kotlin/org/openapitools/api/UserApi.kt @@ -51,7 +51,7 @@ interface UserApi { method = [RequestMethod.POST], value = ["/user"] ) - fun createUser(@Parameter(description = "Created user object", required = true) @Valid @RequestBody body: User, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun createUser(@Parameter(description = "Created user object", required = true) @Valid @RequestBody body: User,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -68,7 +68,7 @@ interface UserApi { method = [RequestMethod.POST], value = ["/user/createWithArray"] ) - fun createUsersWithArrayInput(@Parameter(description = "List of user object", required = true) @Valid @RequestBody body: kotlin.collections.List, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun createUsersWithArrayInput(@Parameter(description = "List of user object", required = true) @Valid @RequestBody body: kotlin.collections.List,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -85,7 +85,7 @@ interface UserApi { method = [RequestMethod.POST], value = ["/user/createWithList"] ) - fun createUsersWithListInput(@Parameter(description = "List of user object", required = true) @Valid @RequestBody body: kotlin.collections.List, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun createUsersWithListInput(@Parameter(description = "List of user object", required = true) @Valid @RequestBody body: kotlin.collections.List,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -103,7 +103,7 @@ interface UserApi { method = [RequestMethod.DELETE], value = ["/user/{username}"] ) - fun deleteUser(@Parameter(description = "The name that needs to be deleted", required = true) @PathVariable("username") username: kotlin.String, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun deleteUser(@Parameter(description = "The name that needs to be deleted", required = true) @PathVariable("username") username: kotlin.String,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -123,7 +123,7 @@ interface UserApi { value = ["/user/{username}"], produces = ["application/xml", "application/json"] ) - fun getUserByName(@Parameter(description = "The name that needs to be fetched. Use user1 for testing.", required = true) @PathVariable("username") username: kotlin.String, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun getUserByName(@Parameter(description = "The name that needs to be fetched. Use user1 for testing.", required = true) @PathVariable("username") username: kotlin.String,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -142,7 +142,7 @@ interface UserApi { value = ["/user/login"], produces = ["application/xml", "application/json"] ) - fun loginUser(@NotNull @Parameter(description = "The user name for login", required = true) @Valid @RequestParam(value = "username", required = true) username: kotlin.String,@NotNull @Parameter(description = "The password for login in clear text", required = true) @Valid @RequestParam(value = "password", required = true) password: kotlin.String, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun loginUser(@NotNull @Parameter(description = "The user name for login", required = true) @Valid @RequestParam(value = "username", required = true) username: kotlin.String,@NotNull @Parameter(description = "The password for login in clear text", required = true) @Valid @RequestParam(value = "password", required = true) password: kotlin.String,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -159,7 +159,7 @@ interface UserApi { method = [RequestMethod.GET], value = ["/user/logout"] ) - fun logoutUser(, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun logoutUser(serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } @@ -177,7 +177,7 @@ interface UserApi { method = [RequestMethod.PUT], value = ["/user/{username}"] ) - fun updateUser(@Parameter(description = "name that need to be deleted", required = true) @PathVariable("username") username: kotlin.String,@Parameter(description = "Updated user object", required = true) @Valid @RequestBody body: User, serverHttpRequest: ServerHttpRequest): ResponseEntity { + fun updateUser(@Parameter(description = "name that need to be deleted", required = true) @PathVariable("username") username: kotlin.String,@Parameter(description = "Updated user object", required = true) @Valid @RequestBody body: User,serverHttpRequest: ServerHttpRequest): ResponseEntity { return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } }