From 911d6c98ccc8c3b934644aca0a71a9d0b3903707 Mon Sep 17 00:00:00 2001 From: altro3 Date: Fri, 23 Aug 2024 14:26:37 +0700 Subject: [PATCH] Set default values to not required parameters (#1722) Fixed #1719 --- .../generator/AbstractMicronautJavaCodegen.java | 1 + .../client/params/cookieParams.mustache | 2 +- .../client/params/headerParams.mustache | 2 +- .../client/params/pathParams.mustache | 2 +- .../client/params/queryParams.mustache | 2 +- .../generator/JavaMicronautClientCodegenTest.java | 1 + .../generator/KotlinMicronautClientCodegenTest.java | 3 ++- .../test/resources/3_0/params-with-default-value.yml | 11 +++++++++++ 8 files changed, 19 insertions(+), 5 deletions(-) diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java index 27f9cc9e74..76e1f1696c 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java @@ -1878,6 +1878,7 @@ public void setDateTimeLibrary(String name) { public boolean getUseInlineModelResolver() { return false; } + @Override public void postProcess() { // disable output donation suggestion diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/cookieParams.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/cookieParams.mustache index b63f0db2cb..ad83145471 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/cookieParams.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/cookieParams.mustache @@ -1 +1 @@ -{{#isCookieParam}}@CookieValue({{#defaultValue}}value = {{/defaultValue}}"{{baseName}}"{{#defaultValue}}, defaultValue = "{{defaultValue}}"{{/defaultValue}}) {{>common/params/validation}}{{>client/params/type}}{{{paramName}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{/vendorExtensions.defaultValueInit}}{{/isCookieParam}} \ No newline at end of file +{{#isCookieParam}}@CookieValue({{#defaultValue}}value = {{/defaultValue}}"{{baseName}}"{{#defaultValue}}, defaultValue = "{{defaultValue}}"{{/defaultValue}}) {{>common/params/validation}}{{>client/params/type}}{{{paramName}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{^isNullable}}{{^required}} = null{{/required}}{{/isNullable}}{{/vendorExtensions.defaultValueInit}}{{/isCookieParam}} \ No newline at end of file diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/headerParams.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/headerParams.mustache index 3e5a5d7966..310521e8d5 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/headerParams.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/headerParams.mustache @@ -1 +1 @@ -{{#isHeaderParam}}@Header({{#defaultValue}}name = {{/defaultValue}}"{{baseName}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{>common/params/validation}}{{>client/params/type}}{{{paramName}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{/vendorExtensions.defaultValueInit}}{{/isHeaderParam}} \ No newline at end of file +{{#isHeaderParam}}@Header({{#defaultValue}}name = {{/defaultValue}}"{{baseName}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{>common/params/validation}}{{>client/params/type}}{{{paramName}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{^isNullable}}{{^required}} = null{{/required}}{{/isNullable}}{{/vendorExtensions.defaultValueInit}}{{/isHeaderParam}} \ No newline at end of file diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/pathParams.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/pathParams.mustache index bde00bd3c4..e2f157e188 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/pathParams.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/pathParams.mustache @@ -1 +1 @@ -{{#isPathParam}}@PathVariable({{#defaultValue}}name = {{/defaultValue}}"{{baseName}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{>common/params/validation}}{{>client/params/type}}{{{paramName}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{/vendorExtensions.defaultValueInit}}{{/isPathParam}} \ No newline at end of file +{{#isPathParam}}@PathVariable({{#defaultValue}}name = {{/defaultValue}}"{{baseName}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{>common/params/validation}}{{>client/params/type}}{{{paramName}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{^isNullable}}{{^required}} = null{{/required}}{{/isNullable}}{{/vendorExtensions.defaultValueInit}}{{/isPathParam}} \ No newline at end of file diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/queryParams.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/queryParams.mustache index 07baede1c4..82aa6487b3 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/queryParams.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/params/queryParams.mustache @@ -1 +1 @@ -{{#isQueryParam}}@QueryValue({{#defaultValue}}value = {{/defaultValue}}"{{{baseName}}}"{{!default value}}{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{!validation and type}}{{>common/params/validation}}{{>client/params/type}}{{{paramName}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{/vendorExtensions.defaultValueInit}}{{/isQueryParam}} \ No newline at end of file +{{#isQueryParam}}@QueryValue({{#defaultValue}}value = {{/defaultValue}}"{{{baseName}}}"{{!default value}}{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{!validation and type}}{{>common/params/validation}}{{>client/params/type}}{{{paramName}}}: {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{^vendorExtensions.defaultValueInit}}{{#isNullable}} = null{{/isNullable}}{{^isNullable}}{{^required}} = null{{/required}}{{/isNullable}}{{/vendorExtensions.defaultValueInit}}{{/isQueryParam}} \ No newline at end of file diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java index ef66ed1333..52531dccd6 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java @@ -539,6 +539,7 @@ void testParamsWithDefaultValue() { String path = outputPath + "src/main/java/org/openapitools/"; assertFileContains(path + "api/DefaultApi.java", + "@QueryValue(\"ids\") @Nullable List<@NotNull Integer> ids", "@PathVariable(name = \"apiVersion\", defaultValue = \"v5\") @Nullable BrowseSearchOrdersApiVersionParameter apiVersio", "@Header(name = \"Content-Type\", defaultValue = \"application/json\") @Nullable String contentType" ); diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java index 19b2588f76..b68b5bb3e5 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java @@ -502,7 +502,7 @@ void testCommonPathParametersWithRef() { assertFileContains(path + "api/WeatherForecastApisApi.kt", "@Get(\"/v1/forecast/{id}\")", "@PathVariable(\"id\") @NotNull id: String,", - "@QueryValue(\"hourly\") @Nullable hourly: List?,"); + "@QueryValue(\"hourly\") @Nullable hourly: List? = null,"); assertFileContains(path + "model/V1ForecastIdGetHourlyParameterInner.kt", "enum class V1ForecastIdGetHourlyParameterInner(", @@ -594,6 +594,7 @@ void testParamsWithDefaultValue() { String path = outputPath + "src/main/kotlin/org/openapitools/"; assertFileContains(path + "api/DefaultApi.kt", + "@QueryValue(\"ids\") @Nullable ids: List<@NotNull Int>? = null,", "@Header(\"X-Favor-Token\") @Nullable xFavorToken: String? = null,", "@PathVariable(name = \"apiVersion\", defaultValue = \"v5\") @Nullable apiVersion: BrowseSearchOrdersApiVersionParameter? = BrowseSearchOrdersApiVersionParameter.V5,", "@Header(name = \"Content-Type\", defaultValue = \"application/json\") @Nullable contentType: String? = \"application/json\"" diff --git a/openapi-generator/src/test/resources/3_0/params-with-default-value.yml b/openapi-generator/src/test/resources/3_0/params-with-default-value.yml index 94e4622ce0..70ee7ca86b 100644 --- a/openapi-generator/src/test/resources/3_0/params-with-default-value.yml +++ b/openapi-generator/src/test/resources/3_0/params-with-default-value.yml @@ -8,6 +8,7 @@ paths: summary: 'Browser/search multiple order' operationId: browseSearchOrders parameters: + - $ref: '#/components/parameters/ids' - $ref: '#/components/parameters/XFavorToken' - $ref: '#/components/parameters/ApiVersion' - $ref: '#/components/parameters/ContentType' @@ -36,6 +37,16 @@ components: - v5 - v6 - v7 + ids: + name: ids + in: query + required: false + description: if passed will return all active orders for a given ids + schema: + type: array + items: + type: integer + default: null ContentType: name: Content-Type in: header