From 833f64976825f742b7d86042429a2b71758d36d8 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 23 May 2023 13:45:10 +0800 Subject: [PATCH] [Kotlin] minor enhancements, remove KotlinServerDeprecatedGenerator (#15608) * better code foramt, add library creator * remove kotlin server deprecated generator --- README.md | 1 + .../languages/KotlinClientCodegen.java | 18 +- .../KotlinServerDeprecatedCodegen.java | 265 ------------------ .../languages/KotlinSpringServerCodegen.java | 2 +- 4 files changed, 11 insertions(+), 275 deletions(-) delete mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinServerDeprecatedCodegen.java diff --git a/README.md b/README.md index afb52f259ab0..819e5e9f3acf 100644 --- a/README.md +++ b/README.md @@ -984,6 +984,7 @@ Here is a list of template creators: * Kotlin: @jimschubert [:heart:](https://www.patreon.com/jimschubert) * Kotlin (MultiPlatform): @andrewemery * Kotlin (Volley): @alisters + * Kotlin (jvm-spring-webclient): @stefankoppier * Lua: @daurnimator * N4JS: @mmews-n4 * Nim: @hokamoto diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java index f7654283c32e..3aa92513f64b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java @@ -882,22 +882,22 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List> optionsSupportedPerFramework = new ImmutableMap.Builder>() - .put(Constants.KTOR, Arrays.asList( - Constants.AUTOMATIC_HEAD_REQUESTS, - Constants.CONDITIONAL_HEADERS, - Constants.HSTS, - Constants.CORS, - Constants.COMPRESSION - )) - .build(); - - /** - * Constructs an instance of `KotlinServerDeprecatedCodegen`. - */ - public KotlinServerDeprecatedCodegen() { - super(); - - modifyFeatureSet(features -> features - .includeDocumentationFeatures(DocumentationFeature.Readme) - .wireFormatFeatures(EnumSet.of(WireFormatFeature.JSON, WireFormatFeature.XML)) - .securityFeatures(EnumSet.of( - SecurityFeature.BasicAuth, - SecurityFeature.ApiKey, - SecurityFeature.OAuth2_Implicit - )) - .excludeGlobalFeatures( - GlobalFeature.XMLStructureDefinitions, - GlobalFeature.Callbacks, - GlobalFeature.LinkObjects, - GlobalFeature.ParameterStyling - ) - .excludeSchemaSupportFeatures( - SchemaSupportFeature.Polymorphism - ) - .excludeParameterFeatures( - ParameterFeature.Cookie - ) - ); - - generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata) - .stability(Stability.DEPRECATED) - .build(); - - artifactId = "kotlin-server-deprecated"; - packageName = "org.openapitools.server"; - - // cliOptions default redefinition need to be updated - updateOption(CodegenConstants.ARTIFACT_ID, this.artifactId); - updateOption(CodegenConstants.PACKAGE_NAME, this.packageName); - - outputFolder = "generated-code" + File.separator + "kotlin-server-deprecated"; - modelTemplateFiles.put("model.mustache", ".kt"); - apiTemplateFiles.put("api.mustache", ".kt"); - embeddedTemplateDir = templateDir = "kotlin-server-deprecated"; - apiPackage = packageName + ".apis"; - modelPackage = packageName + ".models"; - - supportedLibraries.put(Constants.KTOR, "ktor framework"); - - // TODO: Configurable server engine. Defaults to netty in build.gradle. - CliOption library = new CliOption(CodegenConstants.LIBRARY, CodegenConstants.LIBRARY_DESC); - library.setDefault(DEFAULT_LIBRARY); - library.setEnum(supportedLibraries); - - cliOptions.add(library); - - addSwitch(Constants.AUTOMATIC_HEAD_REQUESTS, Constants.AUTOMATIC_HEAD_REQUESTS_DESC, getAutoHeadFeatureEnabled()); - addSwitch(Constants.CONDITIONAL_HEADERS, Constants.CONDITIONAL_HEADERS_DESC, getConditionalHeadersFeatureEnabled()); - addSwitch(Constants.HSTS, Constants.HSTS_DESC, getHstsFeatureEnabled()); - addSwitch(Constants.CORS, Constants.CORS_DESC, getCorsFeatureEnabled()); - addSwitch(Constants.COMPRESSION, Constants.COMPRESSION_DESC, getCompressionFeatureEnabled()); - } - - public Boolean getAutoHeadFeatureEnabled() { - return autoHeadFeatureEnabled; - } - - public void setAutoHeadFeatureEnabled(Boolean autoHeadFeatureEnabled) { - this.autoHeadFeatureEnabled = autoHeadFeatureEnabled; - } - - public Boolean getCompressionFeatureEnabled() { - return compressionFeatureEnabled; - } - - public void setCompressionFeatureEnabled(Boolean compressionFeatureEnabled) { - this.compressionFeatureEnabled = compressionFeatureEnabled; - } - - public Boolean getConditionalHeadersFeatureEnabled() { - return conditionalHeadersFeatureEnabled; - } - - public void setConditionalHeadersFeatureEnabled(Boolean conditionalHeadersFeatureEnabled) { - this.conditionalHeadersFeatureEnabled = conditionalHeadersFeatureEnabled; - } - - public Boolean getCorsFeatureEnabled() { - return corsFeatureEnabled; - } - - public void setCorsFeatureEnabled(Boolean corsFeatureEnabled) { - this.corsFeatureEnabled = corsFeatureEnabled; - } - - public String getHelp() { - return "Generates a Kotlin server (Ktor v1.1.3). IMPORTANT: this generator has been deprecated." + - " Please migrate to `kotlin-server` which supports Ktor v1.5.2+."; - } - - public Boolean getHstsFeatureEnabled() { - return hstsFeatureEnabled; - } - - public void setHstsFeatureEnabled(Boolean hstsFeatureEnabled) { - this.hstsFeatureEnabled = hstsFeatureEnabled; - } - - public String getName() { - return "kotlin-server-deprecated"; - } - - public CodegenType getTag() { - return CodegenType.SERVER; - } - - @Override - public void processOpts() { - super.processOpts(); - - // set default library to "ktor" - if (StringUtils.isEmpty(library)) { - this.setLibrary(DEFAULT_LIBRARY); - additionalProperties.put(CodegenConstants.LIBRARY, DEFAULT_LIBRARY); - LOGGER.info("`library` option is empty. Default to {}", DEFAULT_LIBRARY); - } - - if (additionalProperties.containsKey(Constants.AUTOMATIC_HEAD_REQUESTS)) { - setAutoHeadFeatureEnabled(convertPropertyToBooleanAndWriteBack(Constants.AUTOMATIC_HEAD_REQUESTS)); - } else { - additionalProperties.put(Constants.AUTOMATIC_HEAD_REQUESTS, getAutoHeadFeatureEnabled()); - } - - if (additionalProperties.containsKey(Constants.CONDITIONAL_HEADERS)) { - setConditionalHeadersFeatureEnabled(convertPropertyToBooleanAndWriteBack(Constants.CONDITIONAL_HEADERS)); - } else { - additionalProperties.put(Constants.CONDITIONAL_HEADERS, getConditionalHeadersFeatureEnabled()); - } - - if (additionalProperties.containsKey(Constants.HSTS)) { - setHstsFeatureEnabled(convertPropertyToBooleanAndWriteBack(Constants.HSTS)); - } else { - additionalProperties.put(Constants.HSTS, getHstsFeatureEnabled()); - } - - if (additionalProperties.containsKey(Constants.CORS)) { - setCorsFeatureEnabled(convertPropertyToBooleanAndWriteBack(Constants.CORS)); - } else { - additionalProperties.put(Constants.CORS, getCorsFeatureEnabled()); - } - - if (additionalProperties.containsKey(Constants.COMPRESSION)) { - setCompressionFeatureEnabled(convertPropertyToBooleanAndWriteBack(Constants.COMPRESSION)); - } else { - additionalProperties.put(Constants.COMPRESSION, getCompressionFeatureEnabled()); - } - - boolean generateApis = additionalProperties.containsKey(CodegenConstants.GENERATE_APIS) && (Boolean) additionalProperties.get(CodegenConstants.GENERATE_APIS); - String packageFolder = (sourceFolder + File.separator + packageName).replace(".", File.separator); - - supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); - supportingFiles.add(new SupportingFile("Dockerfile.mustache", "", "Dockerfile")); - - supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle")); - supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle")); - supportingFiles.add(new SupportingFile("gradle.properties", "", "gradle.properties")); - - supportingFiles.add(new SupportingFile("AppMain.kt.mustache", packageFolder, "AppMain.kt")); - supportingFiles.add(new SupportingFile("Configuration.kt.mustache", packageFolder, "Configuration.kt")); - - if (generateApis) { - supportingFiles.add(new SupportingFile("Paths.kt.mustache", packageFolder, "Paths.kt")); - } - - supportingFiles.add(new SupportingFile("application.conf.mustache", resourcesFolder, "application.conf")); - supportingFiles.add(new SupportingFile("logback.xml", resourcesFolder, "logback.xml")); - - final String infrastructureFolder = (sourceFolder + File.separator + packageName + File.separator + "infrastructure").replace(".", File.separator); - - supportingFiles.add(new SupportingFile("ApiKeyAuth.kt.mustache", infrastructureFolder, "ApiKeyAuth.kt")); - } - - public static class Constants { - public final static String KTOR = "ktor"; - public final static String AUTOMATIC_HEAD_REQUESTS = "featureAutoHead"; - public final static String AUTOMATIC_HEAD_REQUESTS_DESC = "Automatically provide responses to HEAD requests for existing routes that have the GET verb defined."; - public final static String CONDITIONAL_HEADERS = "featureConditionalHeaders"; - public final static String CONDITIONAL_HEADERS_DESC = "Avoid sending content if client already has same content, by checking ETag or LastModified properties."; - public final static String HSTS = "featureHSTS"; - public final static String HSTS_DESC = "Avoid sending content if client already has same content, by checking ETag or LastModified properties."; - public final static String CORS = "featureCORS"; - public final static String CORS_DESC = "Ktor by default provides an interceptor for implementing proper support for Cross-Origin Resource Sharing (CORS). See enable-cors.org."; - public final static String COMPRESSION = "featureCompression"; - public final static String COMPRESSION_DESC = "Adds ability to compress outgoing content using gzip, deflate or custom encoder and thus reduce size of the response."; - } - - @Override - public void postProcess() { - System.out.println("################################################################################"); - System.out.println("# Thanks for using OpenAPI Generator. #"); - System.out.println("# Please consider donation to help us maintain this project \uD83D\uDE4F #"); - System.out.println("# https://opencollective.com/openapi_generator/donate #"); - System.out.println("# #"); - System.out.println("# This generator's contributed by Jim Schubert (https://github.com/jimschubert)#"); - System.out.println("# Please support his work directly via https://patreon.com/jimschubert \uD83D\uDE4F #"); - System.out.println("################################################################################"); - } -} 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 540cef172372..c0cf3ca16c34 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 @@ -607,7 +607,7 @@ public void processOpts() { if (AnnotationLibrary.SWAGGER1.equals(getAnnotationLibrary())) { throw new IllegalArgumentException(AnnotationLibrary.SWAGGER1.getPropertyName() + " is not supported with Spring Boot > 3.x"); } - useJakartaEe=true; + useJakartaEe = true; additionalProperties.put(USE_JAKARTA_EE, useJakartaEe); applyJakartaPackage(); }