Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Kotlin][Spring] fix flag appendRequestToHandler with delegatePattern #19206

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/configs/kotlin-spring-boot-delegate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ additionalProperties:
annotationLibrary: swagger2
useSwaggerUI: "true"
delegatePattern: "true"
appendRequestToHandler: "true"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i set this option to false but i got compilation error in samples/server/petstore/kotlin-springboot-delegate

does it work for you locally if you set the option appendRequestToHandler to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the build.gradle.kts is broken.
Do you get this error?


    ~/git/openapi-generator/samples/server/petstore/kotlin-springboot-delegate    issue_18555 +4 !1  gradle build                                                                                                                                                                                        ✔ 

> Task :compileKotlin FAILED
e: /home/peter/git/openapi-generator/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/PetApiController.kt: (7, 19): Unresolved reference: Generated
e: /home/peter/git/openapi-generator/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/PetApiDelegate.kt: (18, 19): Unresolved reference: Generated
e: /home/peter/git/openapi-generator/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/StoreApiController.kt: (7, 19): Unresolved reference: Generated
e: /home/peter/git/openapi-generator/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/StoreApiDelegate.kt: (17, 19): Unresolved reference: Generated
e: /home/peter/git/openapi-generator/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/UserApiController.kt: (7, 19): Unresolved reference: Generated
e: /home/peter/git/openapi-generator/samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/api/UserApiDelegate.kt: (17, 19): Unresolved reference: Generated

FAILURE: Build failed with an exception.

I get it regardless of the appendRequestToHandler setting. It looks like the mustache template for the spring-boot < 3 is broken. Here https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-spring/libraries/spring-boot/buildGradleKts.mustache#L60 and the next line it needs javax instead of jakarta.

These two lines should be:

    compile("javax.validation:validation-api")
    compile("javax.annotation:javax.annotation-api:1.3.2")

I can create a PR for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why didn't the workflows fail?

beanValidations: "true"
requestMappingMode: none
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ public void setReturnContainer(final String returnContainer) {
final List<CodegenParameter> allParams = operation.allParams;
if (allParams != null) {
if (this.isAppendRequestToHandler()) {
allParams.add(new RequestCodegenParameter(true));
allParams.add(new RequestCodegenParameter());
}
allParams.forEach(param ->
// This is necessary in case 'modelMutable' is enabled,
Expand Down Expand Up @@ -972,7 +972,16 @@ protected boolean needToImport(String type) {
@Data
@EqualsAndHashCode(callSuper = true)
static class RequestCodegenParameter extends CodegenParameter {
boolean isRequestObject;

boolean isRequestObject = true;

public RequestCodegenParameter() {
this.isOptional = false;
this.required = true;
this.paramName = "serverHttpRequest";
this.dataType = "ServerHttpRequest";
}

}

public RequestMappingMode getRequestMappingMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.core.io.Resource
{{#appendRequestToHandler}}
import org.springframework.http.server.reactive.ServerHttpRequest
{{/appendRequestToHandler}}
{{#reactive}}
import kotlinx.coroutines.flow.Flow
{{/reactive}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.swagger.v3.oas.annotations.security.*
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.http.server.reactive.ServerHttpRequest

import org.springframework.web.bind.annotation.*
import org.springframework.validation.annotation.Validated
Expand Down Expand Up @@ -57,8 +58,8 @@ interface PetApi {
produces = ["application/xml", "application/json"],
consumes = ["application/json", "application/xml"]
)
fun addPet(@Parameter(description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody pet: Pet): ResponseEntity<Pet> {
return getDelegate().addPet(pet)
fun addPet(@Parameter(description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody pet: Pet,serverHttpRequest: ServerHttpRequest): ResponseEntity<Pet> {
return getDelegate().addPet(pet, serverHttpRequest)
}

@Operation(
Expand All @@ -75,8 +76,8 @@ 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?): ResponseEntity<Unit> {
return getDelegate().deletePet(petId, apiKey)
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<Unit> {
return getDelegate().deletePet(petId, apiKey, serverHttpRequest)
}

@Operation(
Expand All @@ -95,8 +96,8 @@ 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<kotlin.String>): ResponseEntity<List<Pet>> {
return getDelegate().findPetsByStatus(status)
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<kotlin.String>,serverHttpRequest: ServerHttpRequest): ResponseEntity<List<Pet>> {
return getDelegate().findPetsByStatus(status, serverHttpRequest)
}

@Operation(
Expand All @@ -115,8 +116,8 @@ 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<kotlin.String>): ResponseEntity<List<Pet>> {
return getDelegate().findPetsByTags(tags)
fun findPetsByTags(@NotNull @Parameter(description = "Tags to filter by", required = true) @Valid @RequestParam(value = "tags", required = true) tags: kotlin.collections.List<kotlin.String>,serverHttpRequest: ServerHttpRequest): ResponseEntity<List<Pet>> {
return getDelegate().findPetsByTags(tags, serverHttpRequest)
}

@Operation(
Expand All @@ -136,8 +137,8 @@ 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): ResponseEntity<Pet> {
return getDelegate().getPetById(petId)
fun getPetById(@Parameter(description = "ID of pet to return", required = true) @PathVariable("petId") petId: kotlin.Long,serverHttpRequest: ServerHttpRequest): ResponseEntity<Pet> {
return getDelegate().getPetById(petId, serverHttpRequest)
}

@Operation(
Expand All @@ -159,8 +160,8 @@ interface PetApi {
produces = ["application/xml", "application/json"],
consumes = ["application/json", "application/xml"]
)
fun updatePet(@Parameter(description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody pet: Pet): ResponseEntity<Pet> {
return getDelegate().updatePet(pet)
fun updatePet(@Parameter(description = "Pet object that needs to be added to the store", required = true) @Valid @RequestBody pet: Pet,serverHttpRequest: ServerHttpRequest): ResponseEntity<Pet> {
return getDelegate().updatePet(pet, serverHttpRequest)
}

@Operation(
Expand All @@ -178,8 +179,8 @@ 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? ): ResponseEntity<Unit> {
return getDelegate().updatePetWithForm(petId, name, status)
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<Unit> {
return getDelegate().updatePetWithForm(petId, name, status, serverHttpRequest)
}

@Operation(
Expand All @@ -198,7 +199,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 to upload") @Valid @RequestPart("file", required = false) file: org.springframework.core.io.Resource?): ResponseEntity<ModelApiResponse> {
return getDelegate().uploadFile(petId, additionalMetadata, file)
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 to upload") @Valid @RequestPart("file", required = false) file: org.springframework.core.io.Resource?,serverHttpRequest: ServerHttpRequest): ResponseEntity<ModelApiResponse> {
return getDelegate().uploadFile(petId, additionalMetadata, file, serverHttpRequest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.core.io.Resource
import org.springframework.http.server.reactive.ServerHttpRequest

import java.util.Optional

Expand All @@ -22,7 +23,8 @@ interface PetApiDelegate {
/**
* @see PetApi#addPet
*/
fun addPet(pet: Pet): ResponseEntity<Pet> {
fun addPet(pet: Pet,
serverHttpRequest: ServerHttpRequest): ResponseEntity<Pet> {
getRequest().ifPresent { request ->
for (mediaType in MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
Expand All @@ -44,7 +46,8 @@ interface PetApiDelegate {
* @see PetApi#deletePet
*/
fun deletePet(petId: kotlin.Long,
apiKey: kotlin.String?): ResponseEntity<Unit> {
apiKey: kotlin.String?,
serverHttpRequest: ServerHttpRequest): ResponseEntity<Unit> {
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)

}
Expand All @@ -53,7 +56,8 @@ interface PetApiDelegate {
/**
* @see PetApi#findPetsByStatus
*/
fun findPetsByStatus(status: kotlin.collections.List<kotlin.String>): ResponseEntity<List<Pet>> {
fun findPetsByStatus(status: kotlin.collections.List<kotlin.String>,
serverHttpRequest: ServerHttpRequest): ResponseEntity<List<Pet>> {
getRequest().ifPresent { request ->
for (mediaType in MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
Expand All @@ -74,7 +78,8 @@ interface PetApiDelegate {
/**
* @see PetApi#findPetsByTags
*/
fun findPetsByTags(tags: kotlin.collections.List<kotlin.String>): ResponseEntity<List<Pet>> {
fun findPetsByTags(tags: kotlin.collections.List<kotlin.String>,
serverHttpRequest: ServerHttpRequest): ResponseEntity<List<Pet>> {
getRequest().ifPresent { request ->
for (mediaType in MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
Expand All @@ -95,7 +100,8 @@ interface PetApiDelegate {
/**
* @see PetApi#getPetById
*/
fun getPetById(petId: kotlin.Long): ResponseEntity<Pet> {
fun getPetById(petId: kotlin.Long,
serverHttpRequest: ServerHttpRequest): ResponseEntity<Pet> {
getRequest().ifPresent { request ->
for (mediaType in MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
Expand All @@ -116,7 +122,8 @@ interface PetApiDelegate {
/**
* @see PetApi#updatePet
*/
fun updatePet(pet: Pet): ResponseEntity<Pet> {
fun updatePet(pet: Pet,
serverHttpRequest: ServerHttpRequest): ResponseEntity<Pet> {
getRequest().ifPresent { request ->
for (mediaType in MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
Expand All @@ -139,7 +146,8 @@ interface PetApiDelegate {
*/
fun updatePetWithForm(petId: kotlin.Long,
name: kotlin.String?,
status: kotlin.String?): ResponseEntity<Unit> {
status: kotlin.String?,
serverHttpRequest: ServerHttpRequest): ResponseEntity<Unit> {
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)

}
Expand All @@ -150,7 +158,8 @@ interface PetApiDelegate {
*/
fun uploadFile(petId: kotlin.Long,
additionalMetadata: kotlin.String?,
file: Resource?): ResponseEntity<ModelApiResponse> {
file: Resource?,
serverHttpRequest: ServerHttpRequest): ResponseEntity<ModelApiResponse> {
getRequest().ifPresent { request ->
for (mediaType in MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.swagger.v3.oas.annotations.security.*
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.http.server.reactive.ServerHttpRequest

import org.springframework.web.bind.annotation.*
import org.springframework.validation.annotation.Validated
Expand Down Expand Up @@ -53,8 +54,8 @@ 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): ResponseEntity<Unit> {
return getDelegate().deleteOrder(orderId)
fun deleteOrder(@Parameter(description = "ID of the order that needs to be deleted", required = true) @PathVariable("orderId") orderId: kotlin.String,serverHttpRequest: ServerHttpRequest): ResponseEntity<Unit> {
return getDelegate().deleteOrder(orderId, serverHttpRequest)
}

@Operation(
Expand All @@ -72,8 +73,8 @@ interface StoreApi {
value = ["/store/inventory"],
produces = ["application/json"]
)
fun getInventory(): ResponseEntity<Map<String, kotlin.Int>> {
return getDelegate().getInventory()
fun getInventory(serverHttpRequest: ServerHttpRequest): ResponseEntity<Map<String, kotlin.Int>> {
return getDelegate().getInventory(serverHttpRequest)
}

@Operation(
Expand All @@ -92,8 +93,8 @@ 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): ResponseEntity<Order> {
return getDelegate().getOrderById(orderId)
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<Order> {
return getDelegate().getOrderById(orderId, serverHttpRequest)
}

@Operation(
Expand All @@ -112,7 +113,7 @@ interface StoreApi {
produces = ["application/xml", "application/json"],
consumes = ["application/json"]
)
fun placeOrder(@Parameter(description = "order placed for purchasing the pet", required = true) @Valid @RequestBody order: Order): ResponseEntity<Order> {
return getDelegate().placeOrder(order)
fun placeOrder(@Parameter(description = "order placed for purchasing the pet", required = true) @Valid @RequestBody order: Order,serverHttpRequest: ServerHttpRequest): ResponseEntity<Order> {
return getDelegate().placeOrder(order, serverHttpRequest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.core.io.Resource
import org.springframework.http.server.reactive.ServerHttpRequest

import java.util.Optional

Expand All @@ -21,7 +22,8 @@ interface StoreApiDelegate {
/**
* @see StoreApi#deleteOrder
*/
fun deleteOrder(orderId: kotlin.String): ResponseEntity<Unit> {
fun deleteOrder(orderId: kotlin.String,
serverHttpRequest: ServerHttpRequest): ResponseEntity<Unit> {
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)

}
Expand All @@ -30,7 +32,7 @@ interface StoreApiDelegate {
/**
* @see StoreApi#getInventory
*/
fun getInventory(): ResponseEntity<Map<String, kotlin.Int>> {
fun getInventory(serverHttpRequest: ServerHttpRequest): ResponseEntity<Map<String, kotlin.Int>> {
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)

}
Expand All @@ -39,7 +41,8 @@ interface StoreApiDelegate {
/**
* @see StoreApi#getOrderById
*/
fun getOrderById(orderId: kotlin.Long): ResponseEntity<Order> {
fun getOrderById(orderId: kotlin.Long,
serverHttpRequest: ServerHttpRequest): ResponseEntity<Order> {
getRequest().ifPresent { request ->
for (mediaType in MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
Expand All @@ -60,7 +63,8 @@ interface StoreApiDelegate {
/**
* @see StoreApi#placeOrder
*/
fun placeOrder(order: Order): ResponseEntity<Order> {
fun placeOrder(order: Order,
serverHttpRequest: ServerHttpRequest): ResponseEntity<Order> {
getRequest().ifPresent { request ->
for (mediaType in MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
Expand Down
Loading
Loading