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] use spring resource for file handling #2455

Merged
merged 9 commits into from
Mar 27, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
LoggerFactory.getLogger(KotlinSpringServerCodegen.class);

private static final HashSet<String> VARIABLE_RESERVED_WORDS =
new HashSet<String>(Arrays.asList(
new HashSet<>(Arrays.asList(
"ApiClient",
"ApiException",
"ApiResponse"
Expand Down Expand Up @@ -109,6 +109,10 @@ public KotlinSpringServerCodegen() {
importMapping.put("Date", "java.time.LocalDate");
importMapping.put("DateTime", "java.time.OffsetDateTime");

// use resource for file handling
typeMapping.put("file", "org.springframework.core.io.Resource");


languageSpecificPrimitives.addAll(Arrays.asList(
"Any",
"Byte",
Expand Down Expand Up @@ -562,4 +566,33 @@ public CodegenModel fromModel(String name, Schema schema) {

return m;
}

/**
* Output the proper model name (capitalized).
* In case the name belongs to the TypeSystem it won't be renamed.
*
* @param name the name of the model
* @return capitalized model name
*/
@Override
public String toModelName(final String name) {
// Allow for explicitly configured spring.*
if (name.startsWith("org.springframework.") ) {
return name;
}
return super.toModelName(name);
}

/**
* Check the type to see if it needs import the library/module/package
*
* @param type name of the type
* @return true if the library/module/package of the corresponding type needs to be imported
*/
@Override
protected boolean needToImport(String type) {
// provides extra protection against improperly trying to import language primitives and java types
boolean imports = !type.startsWith("org.springframework.") && super.needToImport(type);
return imports;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.0-SNAPSHOT
4.0.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.api

import org.openapitools.model.ModelApiResponse
import org.openapitools.model.OrgspringframeworkcoreioResource
import org.openapitools.model.Pet
import io.swagger.annotations.*
import org.springframework.http.HttpStatus
Expand Down Expand Up @@ -42,8 +43,8 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
value = ["/pet"],
consumes = ["application/json", "application/xml"],
method = [RequestMethod.POST])
fun addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @Valid @RequestBody pet: Pet): ResponseEntity<Unit> {
return ResponseEntity(service.addPet(pet), HttpStatus.OK)
fun addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @Valid @RequestBody body: Pet): ResponseEntity<Unit> {
return ResponseEntity(service.addPet(body), HttpStatus.OK)
}

@ApiOperation(
Expand All @@ -56,7 +57,7 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
@RequestMapping(
value = ["/pet/{petId}"],
method = [RequestMethod.DELETE])
fun deletePet(@ApiParam(value = "Pet id to delete", required=true) @PathVariable("petId") petId: Long,@ApiParam(value = "" ) @RequestHeader(value="api_key", required=false) apiKey: String?): ResponseEntity<Unit> {
fun deletePet(@ApiParam(value = "Pet id to delete", required=true, defaultValue="null") @PathVariable("petId") petId: Long,@ApiParam(value = "" , defaultValue="null") @RequestHeader(value="api_key", required=false) apiKey: String): ResponseEntity<Unit> {
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the default should be null (without double quotes) or should be omitted.

I don't think it's related to this change so we will need to fix it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I saw lot of change in the gérérated code (last time it was générated was 3.3.9 on Sep 2018).
i alsa saw a change in the body parameter name
(@Valid @RequestBody pet: Pet
changed to
@Valid @RequestBody body: Pet

Copy link
Member

Choose a reason for hiding this comment

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

@wing328 This looks like it's probably an issue with the spec document? An id path parameter, by definition, can't be null otherwise the request won't match the path definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is another PR for this problem i think
#2487

return ResponseEntity(service.deletePet(petId, apiKey), HttpStatus.OK)
}

Expand All @@ -73,7 +74,7 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
value = ["/pet/findByStatus"],
produces = ["application/xml", "application/json"],
method = [RequestMethod.GET])
fun findPetsByStatus(@NotNull @ApiParam(value = "Status values that need to be considered for filter", required = true, allowableValues = "available, pending, sold") @Valid @RequestParam(value = "status", required = true) status: List<String>): ResponseEntity<List<Pet>> {
fun findPetsByStatus(@NotNull @ApiParam(value = "Status values that need to be considered for filter", required = true, allowableValues = "available, pending, sold", defaultValue = "null") @Valid @RequestParam(value = "status", required = true, defaultValue="null") status: List<String>): ResponseEntity<List<Pet>> {
return ResponseEntity(service.findPetsByStatus(status), HttpStatus.OK)
}

Expand All @@ -90,7 +91,7 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
value = ["/pet/findByTags"],
produces = ["application/xml", "application/json"],
method = [RequestMethod.GET])
fun findPetsByTags(@NotNull @ApiParam(value = "Tags to filter by", required = true) @Valid @RequestParam(value = "tags", required = true) tags: List<String>): ResponseEntity<List<Pet>> {
fun findPetsByTags(@NotNull @ApiParam(value = "Tags to filter by", required = true, defaultValue = "null") @Valid @RequestParam(value = "tags", required = true, defaultValue="null") tags: List<String>): ResponseEntity<List<Pet>> {
return ResponseEntity(service.findPetsByTags(tags), HttpStatus.OK)
}

Expand All @@ -106,7 +107,7 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
value = ["/pet/{petId}"],
produces = ["application/xml", "application/json"],
method = [RequestMethod.GET])
fun getPetById(@ApiParam(value = "ID of pet to return", required=true) @PathVariable("petId") petId: Long): ResponseEntity<Pet> {
fun getPetById(@ApiParam(value = "ID of pet to return", required=true, defaultValue="null") @PathVariable("petId") petId: Long): ResponseEntity<Pet> {
return ResponseEntity(service.getPetById(petId), HttpStatus.OK)
}

Expand All @@ -121,8 +122,8 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
value = ["/pet"],
consumes = ["application/json", "application/xml"],
method = [RequestMethod.PUT])
fun updatePet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @Valid @RequestBody pet: Pet): ResponseEntity<Unit> {
return ResponseEntity(service.updatePet(pet), HttpStatus.OK)
fun updatePet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @Valid @RequestBody body: Pet): ResponseEntity<Unit> {
return ResponseEntity(service.updatePet(body), HttpStatus.OK)
}

@ApiOperation(
Expand All @@ -136,7 +137,7 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
value = ["/pet/{petId}"],
consumes = ["application/x-www-form-urlencoded"],
method = [RequestMethod.POST])
fun updatePetWithForm(@ApiParam(value = "ID of pet that needs to be updated", required=true) @PathVariable("petId") petId: Long,@ApiParam(value = "Updated name of the pet", defaultValue="null") @RequestParam(value="name", required=false) name: String ,@ApiParam(value = "Updated status of the pet", defaultValue="null") @RequestParam(value="status", required=false) status: String ): ResponseEntity<Unit> {
fun updatePetWithForm(@ApiParam(value = "ID of pet that needs to be updated", required=true, defaultValue="null") @PathVariable("petId") petId: Long,@ApiParam(value = "Updated name of the pet", defaultValue="null") @RequestParam(value="name", required=false) name: String ,@ApiParam(value = "Updated status of the pet", defaultValue="null") @RequestParam(value="status", required=false) status: String ): ResponseEntity<Unit> {
return ResponseEntity(service.updatePetWithForm(petId, name, status), HttpStatus.OK)
}

Expand All @@ -153,7 +154,7 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
produces = ["application/json"],
consumes = ["multipart/form-data"],
method = [RequestMethod.POST])
fun uploadFile(@ApiParam(value = "ID of pet to update", required=true) @PathVariable("petId") petId: Long,@ApiParam(value = "Additional data to pass to server", defaultValue="null") @RequestParam(value="additionalMetadata", required=false) additionalMetadata: String ,@ApiParam(value = "file detail") @Valid @RequestPart("file") file: MultipartFile): ResponseEntity<ModelApiResponse> {
fun uploadFile(@ApiParam(value = "ID of pet to update", required=true, defaultValue="null") @PathVariable("petId") petId: Long,@ApiParam(value = "Additional data to pass to server", defaultValue="null") @RequestParam(value="additionalMetadata", required=false) additionalMetadata: String ,@ApiParam(value = "file detail") @Valid @RequestPart("file") file: MultipartFile): ResponseEntity<ModelApiResponse> {
return ResponseEntity(service.uploadFile(petId, additionalMetadata, file), HttpStatus.OK)
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package org.openapitools.api

import org.openapitools.model.ModelApiResponse
import org.openapitools.model.OrgspringframeworkcoreioResource
import org.openapitools.model.Pet

interface PetApiService {

fun addPet(pet: Pet): Unit
fun addPet(body: Pet): Unit

fun deletePet(petId: Long,apiKey: String?): Unit
fun deletePet(petId: Long,apiKey: String): Unit

fun findPetsByStatus(status: List<String>): List<Pet>

fun findPetsByTags(tags: List<String>): List<Pet>

fun getPetById(petId: Long): Pet

fun updatePet(pet: Pet): Unit
fun updatePet(body: Pet): Unit

fun updatePetWithForm(petId: Long,name: String,status: String): Unit

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package org.openapitools.api

import org.openapitools.model.ModelApiResponse
import org.openapitools.model.OrgspringframeworkcoreioResource
sylvainmoindron marked this conversation as resolved.
Show resolved Hide resolved
import org.openapitools.model.Pet
import org.springframework.stereotype.Service

@Service
class PetApiServiceImpl : PetApiService {

override fun addPet(pet: Pet): Unit {
override fun addPet(body: Pet): Unit {
TODO("Implement me")
}

override fun deletePet(petId: Long,apiKey: String?): Unit {
override fun deletePet(petId: Long,apiKey: String): Unit {
TODO("Implement me")
}

Expand All @@ -27,7 +28,7 @@ class PetApiServiceImpl : PetApiService {
TODO("Implement me")
}

override fun updatePet(pet: Pet): Unit {
override fun updatePet(body: Pet): Unit {
TODO("Implement me")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class StoreApiController(@Autowired(required = true) val service: StoreApiServic
@RequestMapping(
value = ["/store/order/{orderId}"],
method = [RequestMethod.DELETE])
fun deleteOrder(@ApiParam(value = "ID of the order that needs to be deleted", required=true) @PathVariable("orderId") orderId: String): ResponseEntity<Unit> {
fun deleteOrder(@ApiParam(value = "ID of the order that needs to be deleted", required=true, defaultValue="null") @PathVariable("orderId") orderId: String): ResponseEntity<Unit> {
return ResponseEntity(service.deleteOrder(orderId), HttpStatus.OK)
}

Expand Down Expand Up @@ -71,7 +71,7 @@ class StoreApiController(@Autowired(required = true) val service: StoreApiServic
value = ["/store/order/{orderId}"],
produces = ["application/xml", "application/json"],
method = [RequestMethod.GET])
fun getOrderById(@Min(1L) @Max(5L) @ApiParam(value = "ID of pet that needs to be fetched", required=true) @PathVariable("orderId") orderId: Long): ResponseEntity<Order> {
fun getOrderById(@Min(1L) @Max(5L) @ApiParam(value = "ID of pet that needs to be fetched", required=true, defaultValue="null") @PathVariable("orderId") orderId: Long): ResponseEntity<Order> {
return ResponseEntity(service.getOrderById(orderId), HttpStatus.OK)
}

Expand All @@ -86,7 +86,7 @@ class StoreApiController(@Autowired(required = true) val service: StoreApiServic
value = ["/store/order"],
produces = ["application/xml", "application/json"],
method = [RequestMethod.POST])
fun placeOrder(@ApiParam(value = "order placed for purchasing the pet" ,required=true ) @Valid @RequestBody order: Order): ResponseEntity<Order> {
return ResponseEntity(service.placeOrder(order), HttpStatus.OK)
fun placeOrder(@ApiParam(value = "order placed for purchasing the pet" ,required=true ) @Valid @RequestBody body: Order): ResponseEntity<Order> {
return ResponseEntity(service.placeOrder(body), HttpStatus.OK)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface StoreApiService {

fun getOrderById(orderId: Long): Order

fun placeOrder(order: Order): Order
fun placeOrder(body: Order): Order
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StoreApiServiceImpl : StoreApiService {
TODO("Implement me")
}

override fun placeOrder(order: Order): Order {
override fun placeOrder(body: Order): Order {
TODO("Implement me")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class UserApiController(@Autowired(required = true) val service: UserApiService)
@RequestMapping(
value = ["/user"],
method = [RequestMethod.POST])
fun createUser(@ApiParam(value = "Created user object" ,required=true ) @Valid @RequestBody user: User): ResponseEntity<Unit> {
return ResponseEntity(service.createUser(user), HttpStatus.OK)
fun createUser(@ApiParam(value = "Created user object" ,required=true ) @Valid @RequestBody body: User): ResponseEntity<Unit> {
return ResponseEntity(service.createUser(body), HttpStatus.OK)
}

@ApiOperation(
Expand All @@ -52,8 +52,8 @@ class UserApiController(@Autowired(required = true) val service: UserApiService)
@RequestMapping(
value = ["/user/createWithArray"],
method = [RequestMethod.POST])
fun createUsersWithArrayInput(@ApiParam(value = "List of user object" ,required=true ) @Valid @RequestBody user: List<User>): ResponseEntity<Unit> {
return ResponseEntity(service.createUsersWithArrayInput(user), HttpStatus.OK)
fun createUsersWithArrayInput(@ApiParam(value = "List of user object" ,required=true ) @Valid @RequestBody body: List<User>): ResponseEntity<Unit> {
return ResponseEntity(service.createUsersWithArrayInput(body), HttpStatus.OK)
}

@ApiOperation(
Expand All @@ -65,8 +65,8 @@ class UserApiController(@Autowired(required = true) val service: UserApiService)
@RequestMapping(
value = ["/user/createWithList"],
method = [RequestMethod.POST])
fun createUsersWithListInput(@ApiParam(value = "List of user object" ,required=true ) @Valid @RequestBody user: List<User>): ResponseEntity<Unit> {
return ResponseEntity(service.createUsersWithListInput(user), HttpStatus.OK)
fun createUsersWithListInput(@ApiParam(value = "List of user object" ,required=true ) @Valid @RequestBody body: List<User>): ResponseEntity<Unit> {
return ResponseEntity(service.createUsersWithListInput(body), HttpStatus.OK)
}

@ApiOperation(
Expand All @@ -78,7 +78,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService)
@RequestMapping(
value = ["/user/{username}"],
method = [RequestMethod.DELETE])
fun deleteUser(@ApiParam(value = "The name that needs to be deleted", required=true) @PathVariable("username") username: String): ResponseEntity<Unit> {
fun deleteUser(@ApiParam(value = "The name that needs to be deleted", required=true, defaultValue="null") @PathVariable("username") username: String): ResponseEntity<Unit> {
Copy link
Member

Choose a reason for hiding this comment

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

This looks unrelated to this PR, but I don't think passing a null from Spring's internals to a Kotlin String will work. We may be missing nullable type work on the input parameter.

return ResponseEntity(service.deleteUser(username), HttpStatus.OK)
}

Expand All @@ -93,7 +93,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService)
value = ["/user/{username}"],
produces = ["application/xml", "application/json"],
method = [RequestMethod.GET])
fun getUserByName(@ApiParam(value = "The name that needs to be fetched. Use user1 for testing.", required=true) @PathVariable("username") username: String): ResponseEntity<User> {
fun getUserByName(@ApiParam(value = "The name that needs to be fetched. Use user1 for testing.", required=true, defaultValue="null") @PathVariable("username") username: String): ResponseEntity<User> {
return ResponseEntity(service.getUserByName(username), HttpStatus.OK)
}

Expand All @@ -108,7 +108,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService)
value = ["/user/login"],
produces = ["application/xml", "application/json"],
method = [RequestMethod.GET])
fun loginUser(@NotNull @ApiParam(value = "The user name for login", required = true) @Valid @RequestParam(value = "username", required = true) username: String,@NotNull @ApiParam(value = "The password for login in clear text", required = true) @Valid @RequestParam(value = "password", required = true) password: String): ResponseEntity<String> {
fun loginUser(@NotNull @ApiParam(value = "The user name for login", required = true, defaultValue = "null") @Valid @RequestParam(value = "username", required = true, defaultValue="null") username: String,@NotNull @ApiParam(value = "The password for login in clear text", required = true, defaultValue = "null") @Valid @RequestParam(value = "password", required = true, defaultValue="null") password: String): ResponseEntity<String> {
return ResponseEntity(service.loginUser(username, password), HttpStatus.OK)
}

Expand All @@ -134,7 +134,7 @@ class UserApiController(@Autowired(required = true) val service: UserApiService)
@RequestMapping(
value = ["/user/{username}"],
method = [RequestMethod.PUT])
fun updateUser(@ApiParam(value = "name that need to be deleted", required=true) @PathVariable("username") username: String,@ApiParam(value = "Updated user object" ,required=true ) @Valid @RequestBody user: User): ResponseEntity<Unit> {
return ResponseEntity(service.updateUser(username, user), HttpStatus.OK)
fun updateUser(@ApiParam(value = "name that need to be deleted", required=true, defaultValue="null") @PathVariable("username") username: String,@ApiParam(value = "Updated user object" ,required=true ) @Valid @RequestBody body: User): ResponseEntity<Unit> {
return ResponseEntity(service.updateUser(username, body), HttpStatus.OK)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import org.openapitools.model.User

interface UserApiService {

fun createUser(user: User): Unit
fun createUser(body: User): Unit

fun createUsersWithArrayInput(user: List<User>): Unit
fun createUsersWithArrayInput(body: List<User>): Unit

fun createUsersWithListInput(user: List<User>): Unit
fun createUsersWithListInput(body: List<User>): Unit

fun deleteUser(username: String): Unit

Expand All @@ -18,5 +18,5 @@ interface UserApiService {

fun logoutUser(): Unit

fun updateUser(username: String,user: User): Unit
fun updateUser(username: String,body: User): Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import org.springframework.stereotype.Service
@Service
class UserApiServiceImpl : UserApiService {

override fun createUser(user: User): Unit {
override fun createUser(body: User): Unit {
TODO("Implement me")
}

override fun createUsersWithArrayInput(user: List<User>): Unit {
override fun createUsersWithArrayInput(body: List<User>): Unit {
TODO("Implement me")
}

override fun createUsersWithListInput(user: List<User>): Unit {
override fun createUsersWithListInput(body: List<User>): Unit {
TODO("Implement me")
}

Expand All @@ -34,7 +34,7 @@ class UserApiServiceImpl : UserApiService {
TODO("Implement me")
}

override fun updateUser(username: String,user: User): Unit {
override fun updateUser(username: String,body: User): Unit {
TODO("Implement me")
}
}
Loading