forked from loafle/openapi-generator-original
[kotlin-spring] add support for the delegate pattern (#3925)
* #2526 support the delegate pattern for kotlin-spring * fix the diamond * update the doc * fix ci * use Resource? instead of MultipartFile when dealing with files * bump after rebase on master
This commit is contained in:
committed by
William Cheng
parent
8ca1788a75
commit
964260101b
@@ -28,4 +28,5 @@ sidebar_label: kotlin-spring
|
||||
|useBeanValidation|Use BeanValidation API annotations to validate data types| |true|
|
||||
|reactive|use coroutines for reactive behavior| |false|
|
||||
|interfaceOnly|Whether to generate only API interface stubs without the server files.| |false|
|
||||
|delegatePattern|Whether to generate the server files using the delegate pattern| |false|
|
||||
|library|library template (sub-template)|<dl><dt>**spring-boot**</dt><dd>Spring-boot Server application.</dd><dl>|spring-boot|
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.samskivert.mustache.Template;
|
||||
import com.samskivert.mustache.Mustache.Lambda;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.Operation;
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
|
||||
@@ -63,6 +64,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
public static final String SERVICE_IMPLEMENTATION = "serviceImplementation";
|
||||
public static final String REACTIVE = "reactive";
|
||||
public static final String INTERFACE_ONLY = "interfaceOnly";
|
||||
public static final String DELEGATE_PATTERN = "delegatePattern";
|
||||
|
||||
private String basePackage;
|
||||
private String invokerPackage;
|
||||
@@ -77,6 +79,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
private boolean serviceImplementation = false;
|
||||
private boolean reactive = false;
|
||||
private boolean interfaceOnly = false;
|
||||
private boolean delegatePattern = false;
|
||||
|
||||
public KotlinSpringServerCodegen() {
|
||||
super();
|
||||
@@ -125,6 +128,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
addSwitch(USE_BEANVALIDATION, "Use BeanValidation API annotations to validate data types", useBeanValidation);
|
||||
addSwitch(REACTIVE, "use coroutines for reactive behavior", reactive);
|
||||
addSwitch(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files.", interfaceOnly);
|
||||
addSwitch(DELEGATE_PATTERN, "Whether to generate the server files using the delegate pattern", delegatePattern);
|
||||
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
|
||||
setLibrary(SPRING_BOOT);
|
||||
|
||||
@@ -214,6 +218,10 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
this.interfaceOnly = interfaceOnly;
|
||||
}
|
||||
|
||||
public void setDelegatePattern(boolean delegatePattern) {
|
||||
this.delegatePattern = delegatePattern;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUseBeanValidation(boolean useBeanValidation) {
|
||||
this.useBeanValidation = useBeanValidation;
|
||||
@@ -331,15 +339,30 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
this.setInterfaceOnly(Boolean.valueOf(additionalProperties.get(INTERFACE_ONLY).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(DELEGATE_PATTERN)) {
|
||||
this.setDelegatePattern(Boolean.valueOf(additionalProperties.get(DELEGATE_PATTERN).toString()));
|
||||
if (!this.interfaceOnly) {
|
||||
this.setSwaggerAnnotations(true);
|
||||
}
|
||||
}
|
||||
|
||||
modelTemplateFiles.put("model.mustache", ".kt");
|
||||
|
||||
if (interfaceOnly) {
|
||||
if (!this.interfaceOnly && this.delegatePattern) {
|
||||
apiTemplateFiles.put("apiInterface.mustache", ".kt");
|
||||
apiTemplateFiles.put("apiController.mustache", "Controller.kt");
|
||||
} else if (interfaceOnly) {
|
||||
apiTemplateFiles.put("apiInterface.mustache", ".kt");
|
||||
} else {
|
||||
apiTemplateFiles.put("api.mustache", ".kt");
|
||||
apiTestTemplateFiles.put("api_test.mustache", ".kt");
|
||||
}
|
||||
|
||||
if (SPRING_BOOT.equals(library)) {
|
||||
supportingFiles.add(new SupportingFile("apiUtil.mustache",
|
||||
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiUtil.kt"));
|
||||
}
|
||||
|
||||
if (this.serviceInterface) {
|
||||
apiTemplateFiles.put("service.mustache", "Service.kt");
|
||||
} else if (this.serviceImplementation) {
|
||||
@@ -349,6 +372,11 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
apiTemplateFiles.put("serviceImpl.mustache", "ServiceImpl.kt");
|
||||
}
|
||||
|
||||
if (this.delegatePattern) {
|
||||
additionalProperties.put("isDelegate", "true");
|
||||
apiTemplateFiles.put("apiDelegate.mustache", "Delegate.kt");
|
||||
}
|
||||
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||
|
||||
|
||||
@@ -373,6 +401,12 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
|
||||
// spring uses the jackson lib, and we disallow configuration.
|
||||
additionalProperties.put("jackson", "true");
|
||||
|
||||
// add lambda for mustache templates
|
||||
additionalProperties.put("lambdaEscapeDoubleQuote",
|
||||
(Mustache.Lambda) (fragment, writer) -> writer.write(fragment.execute().replaceAll("\"", Matcher.quoteReplacement("\\\""))));
|
||||
additionalProperties.put("lambdaRemoveLineBreak",
|
||||
(Mustache.Lambda) (fragment, writer) -> writer.write(fragment.execute().replaceAll("\\r|\\n", "")));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -381,6 +415,31 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
.put("escapeDoubleQuote", new EscapeLambda("\"", "\\\""));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
|
||||
if (library.equals(SPRING_BOOT) && this.delegatePattern) {
|
||||
String basePath = resourcePath;
|
||||
if (basePath.startsWith("/")) {
|
||||
basePath = basePath.substring(1);
|
||||
}
|
||||
int pos = basePath.indexOf("/");
|
||||
if (pos > 0) {
|
||||
basePath = basePath.substring(0, pos);
|
||||
}
|
||||
|
||||
if (basePath.equals("")) {
|
||||
basePath = "default";
|
||||
} else {
|
||||
co.subresourceOperation = !co.path.isEmpty();
|
||||
}
|
||||
List<CodegenOperation> opList = operations.computeIfAbsent(basePath, k -> new ArrayList<>());
|
||||
opList.add(co);
|
||||
co.baseName = basePath;
|
||||
} else {
|
||||
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preprocessOpenAPI(OpenAPI openAPI) {
|
||||
super.preprocessOpenAPI(openAPI);
|
||||
|
||||
24
modules/openapi-generator/src/main/resources/kotlin-spring/apiController.mustache
vendored
Normal file
24
modules/openapi-generator/src/main/resources/kotlin-spring/apiController.mustache
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
package {{package}};
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import java.util.Optional;
|
||||
{{>generatedAnnotation}}
|
||||
|
||||
@Controller
|
||||
{{=<% %>=}}
|
||||
@RequestMapping("\${openapi.<%title%>.base-path:<%>defaultBasePath%>}")
|
||||
<%={{ }}=%>
|
||||
{{#operations}}
|
||||
class {{classname}}Controller(
|
||||
@org.springframework.beans.factory.annotation.Autowired(required = false) delegate: {{classname}}Delegate
|
||||
) : {{classname}} {
|
||||
private val delegate: {{classname}}Delegate
|
||||
|
||||
init {
|
||||
this.delegate = Optional.ofNullable(delegate).orElse(object : {{classname}}Delegate {})
|
||||
}
|
||||
|
||||
override fun getDelegate(): {{classname}}Delegate = delegate
|
||||
}
|
||||
{{/operations}}
|
||||
42
modules/openapi-generator/src/main/resources/kotlin-spring/apiDelegate.mustache
vendored
Normal file
42
modules/openapi-generator/src/main/resources/kotlin-spring/apiDelegate.mustache
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
package {{package}}
|
||||
|
||||
{{#imports}}import {{import}}
|
||||
{{/imports}}
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.context.request.NativeWebRequest
|
||||
import org.springframework.core.io.Resource
|
||||
{{#reactive}}
|
||||
import org.springframework.web.server.ServerWebExchange
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
{{/reactive}}
|
||||
|
||||
import java.util.Optional
|
||||
{{#async}}
|
||||
import java.util.concurrent.CompletableFuture
|
||||
{{/async}}
|
||||
|
||||
{{#operations}}
|
||||
/**
|
||||
* A delegate to be called by the {@link {{classname}}Controller}}.
|
||||
* Implement this interface with a {@link org.springframework.stereotype.Service} annotated class.
|
||||
*/
|
||||
{{>generatedAnnotation}}
|
||||
interface {{classname}}Delegate {
|
||||
|
||||
fun getRequest(): Optional<NativeWebRequest> = Optional.empty()
|
||||
{{#operation}}
|
||||
|
||||
/**
|
||||
* @see {{classname}}#{{operationId}}
|
||||
*/
|
||||
fun {{operationId}}({{#allParams}}{{paramName}}: {{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{{dataType}}}{{/isBodyParam}}{{/isFile}}{{#isFile}}Resource?{{/isFile}}{{#hasMore}},
|
||||
{{/hasMore}}{{/allParams}}): {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {
|
||||
{{>methodBody}}
|
||||
}
|
||||
|
||||
{{/operation}}
|
||||
}
|
||||
{{/operations}}
|
||||
@@ -1,3 +1,8 @@
|
||||
/**
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) ({{{generatorVersion}}}).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
package {{package}}
|
||||
|
||||
{{#imports}}import {{import}}
|
||||
@@ -14,7 +19,7 @@ import io.swagger.annotations.AuthorizationScope
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.stereotype.Controller
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestPart
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
@@ -56,6 +61,10 @@ import kotlin.collections.Map
|
||||
<%={{ }}=%>
|
||||
{{#operations}}
|
||||
interface {{classname}} {
|
||||
{{#isDelegate}}
|
||||
|
||||
fun getDelegate(): {{classname}}Delegate = object: {{classname}}Delegate {}
|
||||
{{/isDelegate}}
|
||||
{{#operation}}
|
||||
|
||||
{{#swaggerAnnotations}}
|
||||
@@ -76,7 +85,12 @@ interface {{classname}} {
|
||||
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}],{{/hasConsumes}}{{/singleContentTypes}}
|
||||
method = [RequestMethod.{{httpMethod}}])
|
||||
{{#reactive}}{{^isListContainer}}suspend {{/isListContainer}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}): ResponseEntity<{{>returnTypes}}> {
|
||||
{{^isDelegate}}
|
||||
return {{>returnValue}}
|
||||
{{/isDelegate}}
|
||||
{{#isDelegate}}
|
||||
return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}});
|
||||
{{/isDelegate}}
|
||||
}
|
||||
{{/operation}}
|
||||
}
|
||||
|
||||
23
modules/openapi-generator/src/main/resources/kotlin-spring/apiUtil.mustache
vendored
Normal file
23
modules/openapi-generator/src/main/resources/kotlin-spring/apiUtil.mustache
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
package {{apiPackage}}
|
||||
|
||||
{{^reactive}}
|
||||
import org.springframework.web.context.request.NativeWebRequest
|
||||
|
||||
import javax.servlet.http.HttpServletResponse
|
||||
import java.io.IOException
|
||||
{{/reactive}}
|
||||
|
||||
object ApiUtil {
|
||||
{{^reactive}}
|
||||
fun setExampleResponse(req: NativeWebRequest, contentType: String, example: String) {
|
||||
try {
|
||||
val res = req.getNativeResponse(HttpServletResponse::class.java)
|
||||
res.setCharacterEncoding("UTF-8")
|
||||
res.addHeader("Content-Type", contentType)
|
||||
res.getWriter().print(example)
|
||||
} catch (e: IOException) {
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
}
|
||||
{{/reactive}}
|
||||
}
|
||||
3
modules/openapi-generator/src/main/resources/kotlin-spring/generatedAnnotation.mustache
vendored
Normal file
3
modules/openapi-generator/src/main/resources/kotlin-spring/generatedAnnotation.mustache
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{{^hideGenerationTimestamp}}
|
||||
@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}")
|
||||
{{/hideGenerationTimestamp}}
|
||||
@@ -0,0 +1 @@
|
||||
{{contextPath}}
|
||||
23
modules/openapi-generator/src/main/resources/kotlin-spring/methodBody.mustache
vendored
Normal file
23
modules/openapi-generator/src/main/resources/kotlin-spring/methodBody.mustache
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
{{#examples}}
|
||||
{{#-first}}
|
||||
{{#async}}
|
||||
return CompletableFuture.supplyAsync(()-> {
|
||||
{{/async}}getRequest().ifPresent { request ->
|
||||
{{#async}} {{/async}} for (mediaType in MediaType.parseMediaTypes(request.getHeader("Accept"))) {
|
||||
{{/-first}}
|
||||
{{#async}} {{/async}}{{^async}} {{/async}} if (mediaType.isCompatibleWith(MediaType.valueOf("{{{contentType}}}"))) {
|
||||
{{#async}} {{/async}}{{^async}} {{/async}} ApiUtil.setExampleResponse(request, "{{{contentType}}}", "{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{example}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}")
|
||||
{{#async}} {{/async}}{{^async}} {{/async}} break
|
||||
{{#async}} {{/async}}{{^async}} {{/async}} }
|
||||
{{#-last}}
|
||||
{{#async}} {{/async}}{{^async}} {{/async}} }
|
||||
{{#async}} {{/async}} }
|
||||
{{#async}} {{/async}} return ResponseEntity({{#returnSuccessCode}}HttpStatus.valueOf({{{statusCode}}}){{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}})
|
||||
{{#async}}
|
||||
}, Runnable::run)
|
||||
{{/async}}
|
||||
{{/-last}}
|
||||
{{/examples}}
|
||||
{{^examples}}
|
||||
return {{#async}}CompletableFuture.completedFuture({{/async}}ResponseEntity({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}})
|
||||
{{/examples}}
|
||||
@@ -121,7 +121,7 @@ public class KotlinSpringServerCodegenTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSettingInvokerPackageToBasePackage() throws Exception {
|
||||
public void testSettingInvokerPackageToBasePackage() {
|
||||
final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
|
||||
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE, "xyz.yyyyy.bbbb.invoker");
|
||||
codegen.processOpts();
|
||||
@@ -129,4 +129,23 @@ public class KotlinSpringServerCodegenTest {
|
||||
Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.bbbb.invoker");
|
||||
Assert.assertEquals(codegen.getBasePackage(), "xyz.yyyyy.bbbb.invoker");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelegatePattern() {
|
||||
final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
|
||||
codegen.additionalProperties().put(KotlinSpringServerCodegen.DELEGATE_PATTERN, true);
|
||||
codegen.processOpts();
|
||||
|
||||
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.DELEGATE_PATTERN), true);
|
||||
Assert.assertEquals(codegen.additionalProperties().get("isDelegate"), "true");
|
||||
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.SWAGGER_ANNOTATIONS), false);
|
||||
Assert.assertTrue(codegen.getSwaggerAnnotations());
|
||||
|
||||
Assert.assertEquals(codegen.apiTemplateFiles().get("apiController.mustache"), "Controller.kt");
|
||||
Assert.assertEquals(codegen.apiTemplateFiles().get("apiDelegate.mustache"), "Delegate.kt");
|
||||
Assert.assertEquals(codegen.apiTemplateFiles().get("apiInterface.mustache"), ".kt");
|
||||
Assert.assertEquals(codegen.apiTemplateFiles().get("apiInterface.mustache"), ".kt");
|
||||
|
||||
Assert.assertTrue(codegen.supportingFiles().stream().anyMatch(supportingFile -> supportingFile.templateFile.equals("apiUtil.mustache")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
4.1.3-SNAPSHOT
|
||||
4.2.2-SNAPSHOT
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.openapitools.api
|
||||
|
||||
|
||||
object ApiUtil {
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.openapitools.model.ModelApiResponse
|
||||
import org.openapitools.model.Pet
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import kotlinx.coroutines.flow.Flow;
|
||||
import kotlinx.coroutines.test.runBlockingTest
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class PetApiTest {
|
||||
|
||||
private val service: PetApiService = PetApiServiceImpl()
|
||||
private val api: PetApiController = PetApiController(service)
|
||||
|
||||
|
||||
/**
|
||||
* Add a new pet to the store
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun addPetTest() = runBlockingTest {
|
||||
val pet:Pet? = null
|
||||
val response: ResponseEntity<Unit> = api.addPet(pet!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a pet
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun deletePetTest() = runBlockingTest {
|
||||
val petId:kotlin.Long? = null
|
||||
val apiKey:kotlin.String? = null
|
||||
val response: ResponseEntity<Unit> = api.deletePet(petId!!, apiKey!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds Pets by status
|
||||
*
|
||||
* Multiple status values can be provided with comma separated strings
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun findPetsByStatusTest() = runBlockingTest {
|
||||
val status:kotlin.collections.List<kotlin.String>? = null
|
||||
val response: ResponseEntity<Flow<Pet>> = api.findPetsByStatus(status!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds Pets by tags
|
||||
*
|
||||
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun findPetsByTagsTest() = runBlockingTest {
|
||||
val tags:kotlin.collections.List<kotlin.String>? = null
|
||||
val response: ResponseEntity<Flow<Pet>> = api.findPetsByTags(tags!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Find pet by ID
|
||||
*
|
||||
* Returns a single pet
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getPetByIdTest() = runBlockingTest {
|
||||
val petId:kotlin.Long? = null
|
||||
val response: ResponseEntity<Pet> = api.getPetById(petId!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing pet
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun updatePetTest() = runBlockingTest {
|
||||
val pet:Pet? = null
|
||||
val response: ResponseEntity<Unit> = api.updatePet(pet!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a pet in the store with form data
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun updatePetWithFormTest() = runBlockingTest {
|
||||
val petId:kotlin.Long? = null
|
||||
val name:kotlin.String? = null
|
||||
val status:kotlin.String? = null
|
||||
val response: ResponseEntity<Unit> = api.updatePetWithForm(petId!!, name!!, status!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* uploads an image
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun uploadFileTest() = runBlockingTest {
|
||||
val petId:kotlin.Long? = null
|
||||
val additionalMetadata:kotlin.String? = null
|
||||
val file:org.springframework.core.io.Resource? = null
|
||||
val response: ResponseEntity<ModelApiResponse> = api.uploadFile(petId!!, additionalMetadata!!, file!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.openapitools.model.Order
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import kotlinx.coroutines.flow.Flow;
|
||||
import kotlinx.coroutines.test.runBlockingTest
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class StoreApiTest {
|
||||
|
||||
private val service: StoreApiService = StoreApiServiceImpl()
|
||||
private val api: StoreApiController = StoreApiController(service)
|
||||
|
||||
|
||||
/**
|
||||
* Delete purchase order by ID
|
||||
*
|
||||
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun deleteOrderTest() = runBlockingTest {
|
||||
val orderId:kotlin.String? = null
|
||||
val response: ResponseEntity<Unit> = api.deleteOrder(orderId!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns pet inventories by status
|
||||
*
|
||||
* Returns a map of status codes to quantities
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getInventoryTest() = runBlockingTest {
|
||||
val response: ResponseEntity<Map<String, kotlin.Int>> = api.getInventory()
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Find purchase order by ID
|
||||
*
|
||||
* For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getOrderByIdTest() = runBlockingTest {
|
||||
val orderId:kotlin.Long? = null
|
||||
val response: ResponseEntity<Order> = api.getOrderById(orderId!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Place an order for a pet
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun placeOrderTest() = runBlockingTest {
|
||||
val order:Order? = null
|
||||
val response: ResponseEntity<Order> = api.placeOrder(order!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.openapitools.model.User
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import kotlinx.coroutines.flow.Flow;
|
||||
import kotlinx.coroutines.test.runBlockingTest
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class UserApiTest {
|
||||
|
||||
private val service: UserApiService = UserApiServiceImpl()
|
||||
private val api: UserApiController = UserApiController(service)
|
||||
|
||||
|
||||
/**
|
||||
* Create user
|
||||
*
|
||||
* This can only be done by the logged in user.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun createUserTest() = runBlockingTest {
|
||||
val user:User? = null
|
||||
val response: ResponseEntity<Unit> = api.createUser(user!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates list of users with given input array
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun createUsersWithArrayInputTest() = runBlockingTest {
|
||||
val user:kotlin.collections.List<User>? = null
|
||||
val response: ResponseEntity<Unit> = api.createUsersWithArrayInput(user!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates list of users with given input array
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun createUsersWithListInputTest() = runBlockingTest {
|
||||
val user:kotlin.collections.List<User>? = null
|
||||
val response: ResponseEntity<Unit> = api.createUsersWithListInput(user!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete user
|
||||
*
|
||||
* This can only be done by the logged in user.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun deleteUserTest() = runBlockingTest {
|
||||
val username:kotlin.String? = null
|
||||
val response: ResponseEntity<Unit> = api.deleteUser(username!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user by user name
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getUserByNameTest() = runBlockingTest {
|
||||
val username:kotlin.String? = null
|
||||
val response: ResponseEntity<User> = api.getUserByName(username!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs user into the system
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun loginUserTest() = runBlockingTest {
|
||||
val username:kotlin.String? = null
|
||||
val password:kotlin.String? = null
|
||||
val response: ResponseEntity<kotlin.String> = api.loginUser(username!!, password!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs out current logged in user session
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun logoutUserTest() = runBlockingTest {
|
||||
val response: ResponseEntity<Unit> = api.logoutUser()
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Updated user
|
||||
*
|
||||
* This can only be done by the logged in user.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun updateUserTest() = runBlockingTest {
|
||||
val username:kotlin.String? = null
|
||||
val user:User? = null
|
||||
val response: ResponseEntity<Unit> = api.updateUser(username!!, user!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
4.1.3-SNAPSHOT
|
||||
4.2.2-SNAPSHOT
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.springframework.web.context.request.NativeWebRequest
|
||||
|
||||
import javax.servlet.http.HttpServletResponse
|
||||
import java.io.IOException
|
||||
|
||||
object ApiUtil {
|
||||
fun setExampleResponse(req: NativeWebRequest, contentType: String, example: String) {
|
||||
try {
|
||||
val res = req.getNativeResponse(HttpServletResponse::class.java)
|
||||
res.setCharacterEncoding("UTF-8")
|
||||
res.addHeader("Content-Type", contentType)
|
||||
res.getWriter().print(example)
|
||||
} catch (e: IOException) {
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.openapitools.model.ModelApiResponse
|
||||
import org.openapitools.model.Pet
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class PetApiTest {
|
||||
|
||||
private val service: PetApiService = PetApiServiceImpl()
|
||||
private val api: PetApiController = PetApiController(service)
|
||||
|
||||
|
||||
/**
|
||||
* Add a new pet to the store
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun addPetTest() {
|
||||
val pet:Pet? = null
|
||||
val response: ResponseEntity<Unit> = api.addPet(pet!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a pet
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun deletePetTest() {
|
||||
val petId:kotlin.Long? = null
|
||||
val apiKey:kotlin.String? = null
|
||||
val response: ResponseEntity<Unit> = api.deletePet(petId!!, apiKey!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds Pets by status
|
||||
*
|
||||
* Multiple status values can be provided with comma separated strings
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun findPetsByStatusTest() {
|
||||
val status:kotlin.collections.List<kotlin.String>? = null
|
||||
val response: ResponseEntity<List<Pet>> = api.findPetsByStatus(status!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds Pets by tags
|
||||
*
|
||||
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun findPetsByTagsTest() {
|
||||
val tags:kotlin.collections.List<kotlin.String>? = null
|
||||
val response: ResponseEntity<List<Pet>> = api.findPetsByTags(tags!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Find pet by ID
|
||||
*
|
||||
* Returns a single pet
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getPetByIdTest() {
|
||||
val petId:kotlin.Long? = null
|
||||
val response: ResponseEntity<Pet> = api.getPetById(petId!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing pet
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun updatePetTest() {
|
||||
val pet:Pet? = null
|
||||
val response: ResponseEntity<Unit> = api.updatePet(pet!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a pet in the store with form data
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun updatePetWithFormTest() {
|
||||
val petId:kotlin.Long? = null
|
||||
val name:kotlin.String? = null
|
||||
val status:kotlin.String? = null
|
||||
val response: ResponseEntity<Unit> = api.updatePetWithForm(petId!!, name!!, status!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* uploads an image
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun uploadFileTest() {
|
||||
val petId:kotlin.Long? = null
|
||||
val additionalMetadata:kotlin.String? = null
|
||||
val file:org.springframework.core.io.Resource? = null
|
||||
val response: ResponseEntity<ModelApiResponse> = api.uploadFile(petId!!, additionalMetadata!!, file!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.openapitools.model.Order
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class StoreApiTest {
|
||||
|
||||
private val service: StoreApiService = StoreApiServiceImpl()
|
||||
private val api: StoreApiController = StoreApiController(service)
|
||||
|
||||
|
||||
/**
|
||||
* Delete purchase order by ID
|
||||
*
|
||||
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun deleteOrderTest() {
|
||||
val orderId:kotlin.String? = null
|
||||
val response: ResponseEntity<Unit> = api.deleteOrder(orderId!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns pet inventories by status
|
||||
*
|
||||
* Returns a map of status codes to quantities
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getInventoryTest() {
|
||||
val response: ResponseEntity<Map<String, kotlin.Int>> = api.getInventory()
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Find purchase order by ID
|
||||
*
|
||||
* For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getOrderByIdTest() {
|
||||
val orderId:kotlin.Long? = null
|
||||
val response: ResponseEntity<Order> = api.getOrderById(orderId!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Place an order for a pet
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun placeOrderTest() {
|
||||
val order:Order? = null
|
||||
val response: ResponseEntity<Order> = api.placeOrder(order!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.openapitools.model.User
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class UserApiTest {
|
||||
|
||||
private val service: UserApiService = UserApiServiceImpl()
|
||||
private val api: UserApiController = UserApiController(service)
|
||||
|
||||
|
||||
/**
|
||||
* Create user
|
||||
*
|
||||
* This can only be done by the logged in user.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun createUserTest() {
|
||||
val user:User? = null
|
||||
val response: ResponseEntity<Unit> = api.createUser(user!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates list of users with given input array
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun createUsersWithArrayInputTest() {
|
||||
val user:kotlin.collections.List<User>? = null
|
||||
val response: ResponseEntity<Unit> = api.createUsersWithArrayInput(user!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates list of users with given input array
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun createUsersWithListInputTest() {
|
||||
val user:kotlin.collections.List<User>? = null
|
||||
val response: ResponseEntity<Unit> = api.createUsersWithListInput(user!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete user
|
||||
*
|
||||
* This can only be done by the logged in user.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun deleteUserTest() {
|
||||
val username:kotlin.String? = null
|
||||
val response: ResponseEntity<Unit> = api.deleteUser(username!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user by user name
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getUserByNameTest() {
|
||||
val username:kotlin.String? = null
|
||||
val response: ResponseEntity<User> = api.getUserByName(username!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs user into the system
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun loginUserTest() {
|
||||
val username:kotlin.String? = null
|
||||
val password:kotlin.String? = null
|
||||
val response: ResponseEntity<kotlin.String> = api.loginUser(username!!, password!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs out current logged in user session
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun logoutUserTest() {
|
||||
val response: ResponseEntity<Unit> = api.logoutUser()
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Updated user
|
||||
*
|
||||
* This can only be done by the logged in user.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun updateUserTest() {
|
||||
val username:kotlin.String? = null
|
||||
val user:User? = null
|
||||
val response: ResponseEntity<Unit> = api.updateUser(username!!, user!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.openapitools.api
|
||||
|
||||
|
||||
object ApiUtil {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.springframework.web.context.request.NativeWebRequest
|
||||
|
||||
import javax.servlet.http.HttpServletResponse
|
||||
import java.io.IOException
|
||||
|
||||
object ApiUtil {
|
||||
fun setExampleResponse(req: NativeWebRequest, contentType: String, example: String) {
|
||||
try {
|
||||
val res = req.getNativeResponse(HttpServletResponse::class.java)
|
||||
res.setCharacterEncoding("UTF-8")
|
||||
res.addHeader("Content-Type", contentType)
|
||||
res.getWriter().print(example)
|
||||
} catch (e: IOException) {
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user