From 7846d23170f66c1df2868d66b5f1b2478f3674e3 Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Fri, 17 Mar 2023 15:45:45 -0500 Subject: [PATCH 1/5] updated php generator --- .../v3/generators/php/PhpClientCodegen.java | 6 ++++ src/main/resources/handlebars/php/.php_cs | 33 +++++++++++-------- .../handlebars/php/HeaderSelector.mustache | 1 - .../handlebars/php/ObjectSerializer.mustache | 31 +++++++++-------- .../handlebars/php/api_test.mustache | 15 +++++---- .../handlebars/php/composer.mustache | 11 ++++--- .../handlebars/php/model_generic.mustache | 10 +++--- .../handlebars/php/model_test.mustache | 12 ++++--- 8 files changed, 69 insertions(+), 50 deletions(-) diff --git a/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java index 8023500f92..db4d8bd56c 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java @@ -295,6 +295,12 @@ public void processOpts() { if (additionalProperties.containsKey(VARIABLE_NAMING_CONVENTION)) { this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION)); } + if (StringUtils.isBlank(composerVendorName)) { + additionalProperties.put(CodegenConstants.GIT_USER_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_USER_ID).toString())); + } + if (StringUtils.isBlank(composerProjectName)) { + additionalProperties.put(CodegenConstants.GIT_REPO_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_REPO_ID).toString())); + } additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\")); diff --git a/src/main/resources/handlebars/php/.php_cs b/src/main/resources/handlebars/php/.php_cs index 6b8e23c818..4fbe53ec5f 100644 --- a/src/main/resources/handlebars/php/.php_cs +++ b/src/main/resources/handlebars/php/.php_cs @@ -1,18 +1,23 @@ level(Symfony\CS\FixerInterface::PSR2_LEVEL) +return PhpCsFixer\Config::create() ->setUsingCache(true) - ->fixers( - [ - 'ordered_use', - 'phpdoc_order', - 'short_array_syntax', - 'strict', - 'strict_param' - ] - ) - ->finder( - Symfony\CS\Finder\DefaultFinder::create() - ->in(__DIR__) + ->setRules([ + '@PSR2' => true, + 'ordered_imports' => true, + 'phpdoc_order' => true, + 'array_syntax' => [ 'syntax' => 'short' ], + 'strict_comparison' => true, + 'strict_param' => true, + 'no_trailing_whitespace' => false, + 'no_trailing_whitespace_in_comment' => false, + 'braces' => false, + 'single_blank_line_at_eof' => false, + 'blank_line_after_namespace' => false, + ]) + ->setFinder( + PhpCsFixer\Finder::create() + ->exclude('test') + ->exclude('tests') + ->in(__DIR__) ); diff --git a/src/main/resources/handlebars/php/HeaderSelector.mustache b/src/main/resources/handlebars/php/HeaderSelector.mustache index 909beb134d..72f585b46c 100644 --- a/src/main/resources/handlebars/php/HeaderSelector.mustache +++ b/src/main/resources/handlebars/php/HeaderSelector.mustache @@ -97,4 +97,3 @@ class HeaderSelector } } } - diff --git a/src/main/resources/handlebars/php/ObjectSerializer.mustache b/src/main/resources/handlebars/php/ObjectSerializer.mustache index 7dac80ddae..dff5417ab1 100644 --- a/src/main/resources/handlebars/php/ObjectSerializer.mustache +++ b/src/main/resources/handlebars/php/ObjectSerializer.mustache @@ -33,11 +33,12 @@ class ObjectSerializer * Serialize data * * @param mixed $data the data to serialize + * @param string $type the SwaggerType of the data * @param string $format the format of the Swagger type of the data * * @return string|object serialized form of $data */ - public static function sanitizeForSerialization($data, $format = null) + public static function sanitizeForSerialization($data, $type = null, $format = null) { if (is_scalar($data) || null === $data) { return $data; @@ -48,6 +49,11 @@ class ObjectSerializer $data[$property] = self::sanitizeForSerialization($value); } return $data; + } elseif ($data instanceof \stdClass) { + foreach ($data as $property => $value) { + $data->$property = self::sanitizeForSerialization($value); + } + return $data; } elseif (is_object($data)) { $values = []; $formats = $data::swaggerFormats(); @@ -57,7 +63,7 @@ class ObjectSerializer if ($value !== null && !in_array($swaggerType, [{{&primitives}}], true) && method_exists($swaggerType, 'getAllowableEnumValues') - && !in_array($value, $swaggerType::getAllowableEnumValues())) { + && !in_array($value, $swaggerType::getAllowableEnumValues(), true)) { $imploded = implode("', '", $swaggerType::getAllowableEnumValues()); throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'"); } @@ -108,23 +114,22 @@ class ObjectSerializer * later. * * @param string[]|string|\DateTime $object an object to be serialized to a string - * @param string|null $format the format of the parameter * * @return string the serialized object */ - public static function toQueryValue($object, $format = null) + public static function toQueryValue($object) { if (is_array($object)) { return implode(',', $object); } else { - return self::toString($object, $format); + return self::toString($object); } } /** * Take value and turn it into a string suitable for inclusion in * the header. If it's a string, pass through unchanged - * If it's a datetime object, format it in RFC3339 + * If it's a datetime object, format it in ISO8601 * * @param string $value a string which will be part of the header * @@ -138,7 +143,7 @@ class ObjectSerializer /** * Take value and turn it into a string suitable for inclusion in * the http body (form parameter). If it's a string, pass through unchanged - * If it's a datetime object, format it in RFC3339 + * If it's a datetime object, format it in ISO8601 * * @param string|\SplFileObject $value the value of the form parameter * @@ -156,18 +161,16 @@ class ObjectSerializer /** * Take value and turn it into a string suitable for inclusion in * the parameter. If it's a string, pass through unchanged - * If it's a datetime object, format it in RFC3339 - * If it's a date, format it in Y-m-d + * If it's a datetime object, format it in ISO8601 * * @param string|\DateTime $value the value of the parameter - * @param string|null $format the format of the parameter * * @return string the header string */ - public static function toString($value, $format = null) + public static function toString($value) { - if ($value instanceof \DateTime) { - return ($format === 'date') ? $value->format('Y-m-d') : $value->format(\DateTime::ATOM); + if ($value instanceof \DateTime) { // datetime in ISO8601 format + return $value->format(\DateTime::ATOM); } else { return $value; } @@ -276,7 +279,7 @@ class ObjectSerializer return new \SplFileObject($filename, 'r'); } elseif (method_exists($class, 'getAllowableEnumValues')) { - if (!in_array($data, $class::getAllowableEnumValues())) { + if (!in_array($data, $class::getAllowableEnumValues(), true)) { $imploded = implode("', '", $class::getAllowableEnumValues()); throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'"); } diff --git a/src/main/resources/handlebars/php/api_test.mustache b/src/main/resources/handlebars/php/api_test.mustache index b2561cfe89..c381754766 100644 --- a/src/main/resources/handlebars/php/api_test.mustache +++ b/src/main/resources/handlebars/php/api_test.mustache @@ -21,6 +21,7 @@ namespace {{invokerPackage}}; use {{backslash}}{{invokerPackage}}\Configuration; use {{backslash}}{{invokerPackage}}\ApiException; use {{backslash}}{{invokerPackage}}\ObjectSerializer; +use PHPUnit\Framework\TestCase; /** * {{classname}}Test Class Doc Comment @@ -30,37 +31,38 @@ use {{backslash}}{{invokerPackage}}\ObjectSerializer; * @author Swagger Codegen team * @link https://github.com/swagger-api/swagger-codegen */ -{{#operations}}class {{classname}}Test extends \PHPUnit_Framework_TestCase +{{#operations}}class {{classname}}Test extends TestCase { /** * Setup before running any test cases */ - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { } /** * Setup before running each test case */ - public function setUp() + public function setUp(): void { } /** * Clean up after running each test case */ - public function tearDown() + public function tearDown(): void { } /** * Clean up after running all test cases */ - public static function tearDownAfterClass() + public static function tearDownAfterClass(): void { } {{#operation}} + {{#contents}} /** * Test case for {{{operationId}}} @@ -68,9 +70,10 @@ use {{backslash}}{{invokerPackage}}\ObjectSerializer; * {{{summary}}}. * */ - public function test{{vendorExtensions.x-testOperationId}}() + public function test{{vendorExtensions.x-testOperationId}}{{#isForm}}Form{{/isForm}}() { } + {{/contents}} {{/operation}} } {{/operations}} diff --git a/src/main/resources/handlebars/php/composer.mustache b/src/main/resources/handlebars/php/composer.mustache index 0a7732fafe..1d9038cf7d 100644 --- a/src/main/resources/handlebars/php/composer.mustache +++ b/src/main/resources/handlebars/php/composer.mustache @@ -8,6 +8,7 @@ "swagger", "php", "sdk", + "rest", "api" ], "homepage": "http://swagger.io", @@ -19,16 +20,16 @@ } ], "require": { - "php": ">=5.5", + "php": "^7.4 || ^8.0", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "guzzlehttp/guzzle": "^6.2" + "guzzlehttp/guzzle": "^7.3", + "guzzlehttp/psr7": "^1.7 || ^2.0" }, "require-dev": { - "phpunit/phpunit": "^4.8", - "squizlabs/php_codesniffer": "~2.6", - "friendsofphp/php-cs-fixer": "~1.12" + "phpunit/phpunit": "^8.0 || ^9.0", + "friendsofphp/php-cs-fixer": "^3.5" }, "autoload": { "psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" } diff --git a/src/main/resources/handlebars/php/model_generic.mustache b/src/main/resources/handlebars/php/model_generic.mustache index ca67203013..be1beb0983 100644 --- a/src/main/resources/handlebars/php/model_generic.mustache +++ b/src/main/resources/handlebars/php/model_generic.mustache @@ -166,7 +166,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa {{#discriminator}} // Initialize discriminator property with the model name. - $discriminator = array_search('{{discriminator.propertyName}}', self::$attributeMap); + $discriminator = array_search('{{discriminator.propertyName}}', self::$attributeMap, true); $this->container[$discriminator] = static::$swaggerModelName; {{/discriminator}} } @@ -353,7 +353,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa * * @return boolean */ - #[\ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function offsetExists($offset) { return isset($this->container[$offset]); @@ -366,7 +366,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa * * @return mixed */ - #[\ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function offsetGet($offset) { return isset($this->container[$offset]) ? $this->container[$offset] : null; @@ -380,7 +380,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa * * @return void */ - #[\ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { if (is_null($offset)) { @@ -397,7 +397,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa * * @return void */ - #[\ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->container[$offset]); diff --git a/src/main/resources/handlebars/php/model_test.mustache b/src/main/resources/handlebars/php/model_test.mustache index 99064308f5..f96884d1db 100644 --- a/src/main/resources/handlebars/php/model_test.mustache +++ b/src/main/resources/handlebars/php/model_test.mustache @@ -21,6 +21,8 @@ namespace {{invokerPackage}}; +use PHPUnit\Framework\TestCase; + /** * {{classname}}Test Class Doc Comment * @@ -30,34 +32,34 @@ namespace {{invokerPackage}}; * @author Swagger Codegen team * @link https://github.com/swagger-api/swagger-codegen */ -class {{classname}}Test extends \PHPUnit_Framework_TestCase +class {{classname}}Test extends TestCase { /** * Setup before running any test case */ - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { } /** * Setup before running each test case */ - public function setUp() + public function setUp(): void { } /** * Clean up after running each test case */ - public function tearDown() + public function tearDown(): void { } /** * Clean up after running all test cases */ - public static function tearDownAfterClass() + public static function tearDownAfterClass(): void { } From e55eef9c447f7953a8a45f70773e5eb61dd8f572 Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Sat, 18 Mar 2023 06:48:32 -0500 Subject: [PATCH 2/5] added check for null value --- .../swagger/codegen/v3/generators/php/PhpClientCodegen.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java index db4d8bd56c..daec128c11 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java @@ -110,7 +110,7 @@ public PhpClientCodegen() { instantiationTypes.put("array", "array"); instantiationTypes.put("map", "map"); - + // provide primitives to mustache template List sortedLanguageSpecificPrimitives= new ArrayList(languageSpecificPrimitives); Collections.sort(sortedLanguageSpecificPrimitives); @@ -295,10 +295,10 @@ public void processOpts() { if (additionalProperties.containsKey(VARIABLE_NAMING_CONVENTION)) { this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION)); } - if (StringUtils.isBlank(composerVendorName)) { + if (StringUtils.isBlank(composerVendorName) && additionalProperties.get(CodegenConstants.GIT_USER_ID) != null) { additionalProperties.put(CodegenConstants.GIT_USER_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_USER_ID).toString())); } - if (StringUtils.isBlank(composerProjectName)) { + if (StringUtils.isBlank(composerProjectName) && additionalProperties.get(CodegenConstants.GIT_REPO_ID) != null) { additionalProperties.put(CodegenConstants.GIT_REPO_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_REPO_ID).toString())); } From c822fd5e7647080d29ecb3253ca3da9637ac3819 Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Sun, 26 Mar 2023 00:11:41 -0500 Subject: [PATCH 3/5] updated kotlin client templates --- .../kotlin/AbstractKotlinCodegen.java | 7 ++++++ .../handlebars/kotlin-client/api.mustache | 24 +++++++++++++++++-- .../kotlin-client/build.gradle.mustache | 16 ++++++------- .../kotlin-client/data_class.mustache | 16 +++++++------ .../kotlin-client/data_class_opt_var.mustache | 2 +- .../kotlin-client/data_class_req_var.mustache | 2 +- 6 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java index f37df9ccb1..027e2304e2 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java @@ -9,6 +9,7 @@ import io.swagger.v3.oas.models.media.MapSchema; import io.swagger.v3.oas.models.media.ObjectSchema; import io.swagger.v3.oas.models.media.Schema; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.github.jknack.handlebars.helper.StringHelpers; @@ -182,6 +183,7 @@ public AbstractKotlinCodegen() { typeMapping.put("binary", "kotlin.Array"); typeMapping.put("Date", "java.time.LocalDate"); typeMapping.put("DateTime", "java.time.LocalDateTime"); + typeMapping.put("ByteArray", "kotlin.ByteArray"); instantiationTypes.put("array", "arrayOf"); instantiationTypes.put("list", "arrayOf"); @@ -508,6 +510,11 @@ public String toVarName(String name) { return super.toVarName(sanitizeKotlinSpecificNames(name)); } + @Override + public String toEnumName(CodegenProperty property) { + return StringUtils.capitalize(property.name); + } + @Override public void addHandlebarHelpers(Handlebars handlebars) { super.addHandlebarHelpers(handlebars); diff --git a/src/main/resources/handlebars/kotlin-client/api.mustache b/src/main/resources/handlebars/kotlin-client/api.mustache index d81f701662..87853e8b78 100644 --- a/src/main/resources/handlebars/kotlin-client/api.mustache +++ b/src/main/resources/handlebars/kotlin-client/api.mustache @@ -25,9 +25,29 @@ class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(base {{#or hasFormParams hasBodyParam}} val localVariableBody: kotlin.Any? = {{^isForm}}{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}{{/isForm}}{{#isForm}}{{#hasFormParams}}mapOf({{#formParams}}"{{{baseName}}}" to "${{paramName}}"{{#hasMore}}, {{/hasMore}}{{/formParams}}){{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}}{{/isForm}} {{/or}} - {{#hasQueryParams}}val localVariableQuery: MultiValueMap = mapOf({{#queryParams}}"{{baseName}}" to {{#isContainer}}toMultiValue({{paramName}}{{^required}}!!{{/required}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf("${{paramName}}"){{/isContainer}}{{#hasMore}}, {{/hasMore}}{{/queryParams}}){{/hasQueryParams}} + {{#hasQueryParams}} + val localVariableQuery: MultiValueMap = mutableMapOf>().apply { + {{#queryParams}} + {{^required}} + if ({{{paramName}}} != null) { + put("{{baseName}}", {{#isContainer}}toMultiValue({{{paramName}}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{#isDateTime}}parseDateToQueryString({{{paramName}}}){{/isDateTime}}{{#isDate}}parseDateToQueryString({{{paramName}}}){{/isDate}}{{^isDateTime}}{{^isDate}}{{{paramName}}}.toString(){{/isDate}}{{/isDateTime}}){{/isContainer}}) + } + {{/required}} + {{#required}} + put("{{baseName}}", {{#isContainer}}toMultiValue({{{paramName}}}.toList(), "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf({{#isDateTime}}parseDateToQueryString({{{paramName}}}){{/isDateTime}}{{#isDate}}parseDateToQueryString({{{paramName}}}){{/isDate}}{{^isDateTime}}{{^isDate}}{{{paramName}}}.toString(){{/isDate}}{{/isDateTime}}){{/isContainer}}) + {{/required}} + {{/queryParams}} + } + {{/hasQueryParams}} {{#or hasFormParams hasHeaderParams}} - val localVariableHeaders: kotlin.collections.Map = mapOf({{#hasFormParams}}"Content-Type" to "multipart/form-data"{{/hasFormParams}}{{^hasHeaderParams}}){{/hasHeaderParams}}{{#hasHeaderParams}}{{#hasFormParams}}, {{/hasFormParams}}{{#headerParams}}"{{baseName}}" to {{#isContainer}}{{paramName}}.joinToString(separator = collectionDelimiter("{{collectionFormat}}"){{/isContainer}}{{^isContainer}}{{paramName}}{{^required}}.toString(){{/required}}{{/isContainer}}{{#hasMore}}, {{/hasMore}}{{/headerParams}}){{/hasHeaderParams}} + val localVariableHeaders: MutableMap = mutableMapOf({{#hasFormParams}}"Content-Type" to {{^consumes}}"multipart/form-data"{{/consumes}}{{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}}{{/hasFormParams}}) + {{#headerParams}} + {{{paramName}}}{{^required}}?{{/required}}.apply { + localVariableHeaders["{{baseName}}"] = {{#isContainer}}this.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}this.toString(){{/isContainer}} + } + {{/headerParams}} + {{^hasFormParams}}{{#hasConsumes}}{{#consumes}}localVariableHeaders["Content-Type"] = "{{{mediaType}}}" + {{/consumes}}{{/hasConsumes}}{{/hasFormParams}}{{#hasProduces}}localVariableHeaders["Accept"] = "{{#produces}}{{{mediaType}}}{{^@last}}, {{/@last}}{{/produces}}"{{/hasProduces}} {{/or}} val localVariableConfig = RequestConfig( RequestMethod.{{httpMethod}}, diff --git a/src/main/resources/handlebars/kotlin-client/build.gradle.mustache b/src/main/resources/handlebars/kotlin-client/build.gradle.mustache index 70c7bfaced..ad7cd4cbec 100644 --- a/src/main/resources/handlebars/kotlin-client/build.gradle.mustache +++ b/src/main/resources/handlebars/kotlin-client/build.gradle.mustache @@ -2,12 +2,12 @@ group '{{groupId}}' version '{{artifactVersion}}' wrapper { - gradleVersion = '5.3' + gradleVersion = '7.5' distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip" } buildscript { - ext.kotlin_version = '1.4.30' + ext.kotlin_version = '1.8.0' repositories { maven { url "https://repo1.maven.org/maven2" } @@ -24,10 +24,10 @@ repositories { } dependencies { - compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" - compile "com.squareup.moshi:moshi-kotlin:1.11.0" - compile "com.squareup.moshi:moshi-adapters:1.11.0" - compile "com.squareup.okhttp3:okhttp:4.9.0" - testCompile "io.kotlintest:kotlintest:2.0.7" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" + implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" + implementation "com.squareup.moshi:moshi-kotlin:1.11.0" + implementation "com.squareup.moshi:moshi-adapters:1.11.0" + implementation "com.squareup.okhttp3:okhttp:4.9.0" + testImplementation "io.kotlintest:kotlintest:2.0.7" } \ No newline at end of file diff --git a/src/main/resources/handlebars/kotlin-client/data_class.mustache b/src/main/resources/handlebars/kotlin-client/data_class.mustache index bee24e30dc..72eb9392d7 100644 --- a/src/main/resources/handlebars/kotlin-client/data_class.mustache +++ b/src/main/resources/handlebars/kotlin-client/data_class.mustache @@ -1,31 +1,33 @@ /** * {{{description}}} -{{#allVars}} +{{#vars}} * @param {{name}} {{{description}}} -{{/allVars}} +{{/vars}} */ {{#hasVars}}data {{/hasVars}}class {{classname}} ( -{{#allVars}} +{{#vars}} {{#required}} {{>data_class_req_var}}{{^@last}},{{/@last}} {{/required}} {{^required}} {{>data_class_opt_var}}{{^@last}},{{/@last}} {{/required}} -{{/allVars}} +{{/vars}} ) { -{{#allVars}} +{{#vars}} +{{#baseItems this}} {{#isEnum}} /** * {{{description}}} * Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^@last}},{{/@last}}{{/enumVars}}{{/allowableValues}} */ - enum class {{nameInCamelCase}}(val value: {{datatype}}{{#isNullable}}?{{/isNullable}}){ + enum class {{{datatypeWithEnum}}}(val value: {{{datatype}}}{{#isNullable}}?{{/isNullable}}){ {{#allowableValues}}{{#enumVars}} {{&name}}({{#value}}{{{value}}}{{/value}}{{^value}}null{{/value}}){{^@last}},{{/@last}}{{#@last}};{{/@last}} {{/enumVars}}{{/allowableValues}} } {{/isEnum}} -{{/allVars}} +{{/baseItems}} +{{/vars}} } \ No newline at end of file diff --git a/src/main/resources/handlebars/kotlin-client/data_class_opt_var.mustache b/src/main/resources/handlebars/kotlin-client/data_class_opt_var.mustache index 7b1c72aa76..7e1e9b6f89 100644 --- a/src/main/resources/handlebars/kotlin-client/data_class_opt_var.mustache +++ b/src/main/resources/handlebars/kotlin-client/data_class_opt_var.mustache @@ -1,4 +1,4 @@ {{#description}} /* {{{description}}} */ {{/description}} - val {{{name}}}: {{#is this 'enum'}}{{classname}}.{{nameInCamelCase}}{{/is}}{{#isNot this 'enum'}}{{{datatype}}}{{/isNot}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}} \ No newline at end of file + val {{{name}}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}} \ No newline at end of file diff --git a/src/main/resources/handlebars/kotlin-client/data_class_req_var.mustache b/src/main/resources/handlebars/kotlin-client/data_class_req_var.mustache index 659ceefc24..72466400f9 100644 --- a/src/main/resources/handlebars/kotlin-client/data_class_req_var.mustache +++ b/src/main/resources/handlebars/kotlin-client/data_class_req_var.mustache @@ -1,4 +1,4 @@ {{#description}} /* {{{description}}} */ {{/description}} - val {{{name}}}: {{#is this 'enum'}}{{classname}}.{{nameInCamelCase}}{{/is}}{{#isNot this 'enum'}}{{{datatype}}}{{/isNot}} \ No newline at end of file + val {{{name}}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}} \ No newline at end of file From 283f9d08ad1153610e80fd08a3770667059371a4 Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Sun, 26 Mar 2023 02:39:40 -0500 Subject: [PATCH 4/5] updated kotlin-server templates --- .../kotlin/AbstractKotlinCodegen.java | 2 ++ .../kotlin/KotlinClientCodegen.java | 6 ---- .../kotlin-server/data_class.mustache | 32 ++++++++++------- .../kotlin-server/data_class_opt_var.mustache | 4 +-- .../kotlin-server/data_class_req_var.mustache | 2 +- .../libraries/ktor/AppMain.kt.mustache | 17 +++++---- .../libraries/ktor/_principal.mustache | 4 +-- .../kotlin-server/libraries/ktor/api.mustache | 1 + .../libraries/ktor/build.gradle.mustache | 35 +++++++++---------- 9 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java index 027e2304e2..d42ae0a90c 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java @@ -1,5 +1,6 @@ package io.swagger.codegen.v3.generators.kotlin; +import com.github.jknack.handlebars.helper.ConditionalHelpers; import io.swagger.codegen.v3.CliOption; import io.swagger.codegen.v3.CodegenConstants; import io.swagger.codegen.v3.CodegenModel; @@ -519,6 +520,7 @@ public String toEnumName(CodegenProperty property) { public void addHandlebarHelpers(Handlebars handlebars) { super.addHandlebarHelpers(handlebars); handlebars.registerHelpers(StringHelpers.class); + handlebars.registerHelpers(ConditionalHelpers.class); } /** diff --git a/src/main/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegen.java index b55c430108..61509bdd89 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegen.java @@ -57,12 +57,6 @@ public KotlinClientCodegen() { cliOptions.add(dateLibrary); } - @Override - public void addHandlebarHelpers(Handlebars handlebars) { - super.addHandlebarHelpers(handlebars); - handlebars.registerHelpers(ConditionalHelpers.class); - } - @Override public String getDefaultTemplateDir() { return "kotlin-client"; diff --git a/src/main/resources/handlebars/kotlin-server/data_class.mustache b/src/main/resources/handlebars/kotlin-server/data_class.mustache index c7d1897367..72eb9392d7 100644 --- a/src/main/resources/handlebars/kotlin-server/data_class.mustache +++ b/src/main/resources/handlebars/kotlin-server/data_class.mustache @@ -1,25 +1,33 @@ /** * {{{description}}} -{{~#vars}} +{{#vars}} * @param {{name}} {{{description}}} -{{~/vars~}} +{{/vars}} */ -data class {{classname}} ( -{{~#requiredVars~}} - {{>data_class_req_var}}{{^@last}},{{/@last}} -{{~/requiredVars~}} -{{#has this 'required'}}{{#has this 'optional'}},{{/has}}{{/has}} -{{~#optionalVars}}{{>data_class_opt_var}}{{^@last}},{{/@last}}{{/optionalVars}} +{{#hasVars}}data {{/hasVars}}class {{classname}} ( + +{{#vars}} +{{#required}} +{{>data_class_req_var}}{{^@last}},{{/@last}} +{{/required}} +{{^required}} +{{>data_class_opt_var}}{{^@last}},{{/@last}} +{{/required}} +{{/vars}} ) { -{{#has this 'enums'}}{{#vars}}{{#is this 'enum'}} +{{#vars}} +{{#baseItems this}} +{{#isEnum}} /** * {{{description}}} * Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^@last}},{{/@last}}{{/enumVars}}{{/allowableValues}} */ - enum class {{nameInCamelCase}}(val value: {{datatype}}){ + enum class {{{datatypeWithEnum}}}(val value: {{{datatype}}}{{#isNullable}}?{{/isNullable}}){ {{#allowableValues}}{{#enumVars}} - {{&name}}({{{value}}}){{^@last}},{{/@last}}{{#@last}};{{/@last}} + {{&name}}({{#value}}{{{value}}}{{/value}}{{^value}}null{{/value}}){{^@last}},{{/@last}}{{#@last}};{{/@last}} {{/enumVars}}{{/allowableValues}} } -{{/is}}{{/vars}}{{/has}} +{{/isEnum}} +{{/baseItems}} +{{/vars}} } \ No newline at end of file diff --git a/src/main/resources/handlebars/kotlin-server/data_class_opt_var.mustache b/src/main/resources/handlebars/kotlin-server/data_class_opt_var.mustache index 2d950f5bab..7e1e9b6f89 100644 --- a/src/main/resources/handlebars/kotlin-server/data_class_opt_var.mustache +++ b/src/main/resources/handlebars/kotlin-server/data_class_opt_var.mustache @@ -1,4 +1,4 @@ {{#description}} /* {{{description}}} */ -{{~/description}} - val {{{name}}}: {{#is this 'enum'}}{{classname}}.{{nameInCamelCase}}{{/is}}{{#isNot this 'enum'}}{{{datatype}}}{{/isNot}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}} \ No newline at end of file +{{/description}} + val {{{name}}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}} \ No newline at end of file diff --git a/src/main/resources/handlebars/kotlin-server/data_class_req_var.mustache b/src/main/resources/handlebars/kotlin-server/data_class_req_var.mustache index 659ceefc24..72466400f9 100644 --- a/src/main/resources/handlebars/kotlin-server/data_class_req_var.mustache +++ b/src/main/resources/handlebars/kotlin-server/data_class_req_var.mustache @@ -1,4 +1,4 @@ {{#description}} /* {{{description}}} */ {{/description}} - val {{{name}}}: {{#is this 'enum'}}{{classname}}.{{nameInCamelCase}}{{/is}}{{#isNot this 'enum'}}{{{datatype}}}{{/isNot}} \ No newline at end of file + val {{{name}}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}} \ No newline at end of file diff --git a/src/main/resources/handlebars/kotlin-server/libraries/ktor/AppMain.kt.mustache b/src/main/resources/handlebars/kotlin-server/libraries/ktor/AppMain.kt.mustache index 247a58289a..5aaec82958 100644 --- a/src/main/resources/handlebars/kotlin-server/libraries/ktor/AppMain.kt.mustache +++ b/src/main/resources/handlebars/kotlin-server/libraries/ktor/AppMain.kt.mustache @@ -36,6 +36,8 @@ import io.ktor.util.KtorExperimentalAPI {{#hasAuthMethods}} import io.ktor.auth.Authentication import io.ktor.auth.oauth +import io.ktor.auth.basic +import io.ktor.auth.UserIdPrincipal import io.ktor.metrics.dropwizard.DropwizardMetrics import io.swagger.server.infrastructure.ApiKeyCredential import io.swagger.server.infrastructure.ApiPrincipal @@ -88,13 +90,14 @@ fun Application.main() { {{#authMethods}} {{#isBasic}} basic("{{{name}}}") { - validate { credentials -> - // TODO: "Apply your basic authentication functionality." - // Accessible in-method via call.principal() - if (credentials.name == "Swagger" && "Codegen" == credentials.password) { - UserIdPrincipal(credentials.name) - } else { - null + validate { credentials -> + // TODO: "Apply your basic authentication functionality." + // Accessible in-method via call.principal() + if (credentials.name == "Swagger" && "Codegen" == credentials.password) { + UserIdPrincipal(credentials.name) + } else { + null + } } } {{/isBasic}} diff --git a/src/main/resources/handlebars/kotlin-server/libraries/ktor/_principal.mustache b/src/main/resources/handlebars/kotlin-server/libraries/ktor/_principal.mustache index c868b628bd..fc41a74dd8 100644 --- a/src/main/resources/handlebars/kotlin-server/libraries/ktor/_principal.mustache +++ b/src/main/resources/handlebars/kotlin-server/libraries/ktor/_principal.mustache @@ -1,8 +1,8 @@ {{#authMethods}} {{#@first}} -{{#isBasic}} +{{#or isBasic isBearer}} val principal = call.authentication.principal() -{{/isBasic}} +{{/or}} {{#isApiKey}} val principal = call.authentication.principal() {{/isApiKey}} diff --git a/src/main/resources/handlebars/kotlin-server/libraries/ktor/api.mustache b/src/main/resources/handlebars/kotlin-server/libraries/ktor/api.mustache index e8b099e412..d154d91873 100644 --- a/src/main/resources/handlebars/kotlin-server/libraries/ktor/api.mustache +++ b/src/main/resources/handlebars/kotlin-server/libraries/ktor/api.mustache @@ -15,6 +15,7 @@ import io.ktor.locations.delete import io.ktor.locations.get import io.ktor.locations.post import io.ktor.locations.put +import io.ktor.locations.patch import io.ktor.response.respond import io.ktor.response.respondText import io.ktor.routing.Route diff --git a/src/main/resources/handlebars/kotlin-server/libraries/ktor/build.gradle.mustache b/src/main/resources/handlebars/kotlin-server/libraries/ktor/build.gradle.mustache index afc640feda..bd2a90be7e 100644 --- a/src/main/resources/handlebars/kotlin-server/libraries/ktor/build.gradle.mustache +++ b/src/main/resources/handlebars/kotlin-server/libraries/ktor/build.gradle.mustache @@ -2,16 +2,19 @@ group '{{groupId}}' version '{{artifactVersion}}' wrapper { - gradleVersion = '5.3' + gradleVersion = '7.5' distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip" } buildscript { - ext.kotlin_version = '1.4.30' - ext.ktor_version = '1.5.1' - ext.shadow_version = '5.2.0' + ext.kotlin_version = '1.8.0' + ext.ktor_version = "1.5.4" + ext.shadow_version = "6.1.0" repositories { + maven { + url "https://repo1.maven.org/maven2" + } maven { url "https://plugins.gradle.org/m2/" } @@ -40,12 +43,6 @@ compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } -kotlin { - experimental { - coroutines "enable" - } -} - shadowJar { baseName = '{{artifactId}}' classifier = null @@ -59,13 +56,13 @@ repositories { } dependencies { - compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - compile "io.ktor:ktor-server-netty:$ktor_version" - compile "io.ktor:ktor-metrics:$ktor_version" - compile "io.ktor:ktor-locations:$ktor_version" - compile "io.ktor:ktor-gson:$ktor_version" - compile "io.ktor:ktor-client-core:$ktor_version" - compile "io.ktor:ktor-client-apache:$ktor_version" - compile "ch.qos.logback:logback-classic:1.2.9" - testCompile group: 'junit', name: 'junit', version: '4.12' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" + implementation "io.ktor:ktor-server-netty:$ktor_version" + implementation "io.ktor:ktor-metrics:$ktor_version" + implementation "io.ktor:ktor-locations:$ktor_version" + implementation "io.ktor:ktor-gson:$ktor_version" + implementation "io.ktor:ktor-client-core:$ktor_version" + implementation "io.ktor:ktor-client-apache:$ktor_version" + implementation "ch.qos.logback:logback-classic:1.2.9" + testImplementation group: 'junit', name: 'junit', version: '4.12' } From f918fcdbb84b430465902719456391d6c057f4a1 Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Wed, 5 Apr 2023 02:36:44 -0500 Subject: [PATCH 5/5] added comment in empty template to fix null bytes error --- src/main/resources/handlebars/python/__init__test.mustache | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/handlebars/python/__init__test.mustache b/src/main/resources/handlebars/python/__init__test.mustache index e69de29bb2..576f56f87e 100644 --- a/src/main/resources/handlebars/python/__init__test.mustache +++ b/src/main/resources/handlebars/python/__init__test.mustache @@ -0,0 +1 @@ +# coding: utf-8 \ No newline at end of file