[kotlin-spring] use spring resource for file handling (#2455)

* [kotlin-spring] use org.springframework.core.io.Resource for file handling

* run ./bin/kotlin-springboot-petstore-server.sh

* run ./bin/kotlin-springboot-petstore-server.sh after building the CLI

* use MultipartFile for file in form and the specified type otherwise

* remplace tab with space

* [kotlin-springboot] replace all instance of MultipartFile by Resource

* run ./bin/kotlin-springboot-petstore-server.sh
This commit is contained in:
sylvainmoindron 2019-03-27 14:44:24 +01:00 committed by William Cheng
parent 46e8ccbd1e
commit 74fbd3454b
12 changed files with 50 additions and 25 deletions

View File

@ -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"
@ -109,6 +109,10 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
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",
@ -562,4 +566,33 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
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;
}
}

View File

@ -20,7 +20,6 @@ import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.validation.annotation.Validated
{{/useBeanValidation}}
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.multipart.MultipartFile
import org.springframework.beans.factory.annotation.Autowired
{{#useBeanValidation}}

View File

@ -1 +1 @@
{{#isFormParam}}{{^isFile}}{{#swaggerAnnotations}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{/swaggerAnnotations}} @RequestParam(value="{{baseName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) {{paramName}}: {{>optionalDataType}} {{/isFile}}{{#isFile}}{{#swaggerAnnotations}}@ApiParam(value = "file detail"){{/swaggerAnnotations}} {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestPart("file") {{baseName}}: MultipartFile{{/isFile}}{{/isFormParam}}
{{#isFormParam}}{{^isFile}}{{#swaggerAnnotations}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{/swaggerAnnotations}} @RequestParam(value="{{baseName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) {{paramName}}: {{>optionalDataType}} {{/isFile}}{{#isFile}}{{#swaggerAnnotations}}@ApiParam(value = "file detail"){{/swaggerAnnotations}} {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestPart("file") {{baseName}}: {{>optionalDataType}}{{/isFile}}{{/isFormParam}}

View File

@ -7,7 +7,7 @@ package {{package}}
interface {{classname}}Service {
{{#operation}}
fun {{operationId}}({{#allParams}}{{paramName}}: {{^isFile}}{{>optionalDataType}}{{/isFile}}{{#isFile}}org.springframework.web.multipart.MultipartFile{{/isFile}}{{#hasMore}},{{/hasMore}}{{/allParams}}): {{>returnTypes}}
fun {{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}{{#hasMore}},{{/hasMore}}{{/allParams}}): {{>returnTypes}}
{{/operation}}
}
{{/operations}}

View File

@ -3,13 +3,12 @@ package {{package}}
{{#imports}}import {{import}}
{{/imports}}
import org.springframework.stereotype.Service
@Service
{{#operations}}
class {{classname}}ServiceImpl : {{classname}}Service {
{{#operation}}
override fun {{operationId}}({{#allParams}}{{paramName}}: {{^isFile}}{{>optionalDataType}}{{/isFile}}{{#isFile}}org.springframework.web.multipart.MultipartFile{{/isFile}}{{#hasMore}},{{/hasMore}}{{/allParams}}): {{>returnTypes}} {
override fun {{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}{{#hasMore}},{{/hasMore}}{{/allParams}}): {{>returnTypes}} {
TODO("Implement me")
}
{{/operation}}

View File

@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.validation.annotation.Validated
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.multipart.MultipartFile
import org.springframework.beans.factory.annotation.Autowired
import javax.validation.Valid
@ -136,7 +135,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") @RequestParam(value="name", required=false) name: String? ,@ApiParam(value = "Updated status of the pet") @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)
}
@ -153,7 +152,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") @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: org.springframework.core.io.Resource): ResponseEntity<ModelApiResponse> {
return ResponseEntity(service.uploadFile(petId, additionalMetadata, file), HttpStatus.OK)
}
}

View File

@ -19,5 +19,5 @@ interface PetApiService {
fun updatePetWithForm(petId: Long,name: String?,status: String?): Unit
fun uploadFile(petId: Long,additionalMetadata: String?,file: org.springframework.web.multipart.MultipartFile): ModelApiResponse
fun uploadFile(petId: Long,additionalMetadata: String,file: org.springframework.core.io.Resource): ModelApiResponse
}

View File

@ -3,7 +3,6 @@ package org.openapitools.api
import org.openapitools.model.ModelApiResponse
import org.openapitools.model.Pet
import org.springframework.stereotype.Service
@Service
class PetApiServiceImpl : PetApiService {
@ -35,7 +34,7 @@ class PetApiServiceImpl : PetApiService {
TODO("Implement me")
}
override fun uploadFile(petId: Long,additionalMetadata: String?,file: org.springframework.web.multipart.MultipartFile): ModelApiResponse {
override fun uploadFile(petId: Long,additionalMetadata: String,file: org.springframework.core.io.Resource): ModelApiResponse {
TODO("Implement me")
}
}

View File

@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.validation.annotation.Validated
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.multipart.MultipartFile
import org.springframework.beans.factory.annotation.Autowired
import javax.validation.Valid

View File

@ -2,7 +2,6 @@ package org.openapitools.api
import org.openapitools.model.Order
import org.springframework.stereotype.Service
@Service
class StoreApiServiceImpl : StoreApiService {

View File

@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.validation.annotation.Validated
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.multipart.MultipartFile
import org.springframework.beans.factory.annotation.Autowired
import javax.validation.Valid

View File

@ -2,7 +2,6 @@ package org.openapitools.api
import org.openapitools.model.User
import org.springframework.stereotype.Service
@Service
class UserApiServiceImpl : UserApiService {