From a7ef162624facf5e7f1366412d8637bf159b87dd Mon Sep 17 00:00:00 2001 From: Alexandru Gologan Date: Wed, 18 Nov 2020 15:23:35 +0200 Subject: [PATCH] Initialize optional query parameters with null --- .../kotlin/retrofit2/queryParams.mustache | 2 +- .../generatecodesamples/apis/ResourceApi.kt | 10 +++++----- .../generatecodesamples/ValidParameterTest.kt | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/gradle-plugin/plugin/src/main/resources/kotlin/retrofit2/queryParams.mustache b/gradle-plugin/plugin/src/main/resources/kotlin/retrofit2/queryParams.mustache index 1ceefb35..821918f2 100644 --- a/gradle-plugin/plugin/src/main/resources/kotlin/retrofit2/queryParams.mustache +++ b/gradle-plugin/plugin/src/main/resources/kotlin/retrofit2/queryParams.mustache @@ -1 +1 @@ -{{#isQueryParam}}@retrofit2.http.Query("{{baseName}}") {{#collectionFormat}}{{^isCollectionFormatMulti}}@{{{collectionFormat.toUpperCase}}} {{/isCollectionFormatMulti}}{{/collectionFormat}}{{paramName}}: {{{dataType}}}{{/isQueryParam}} \ No newline at end of file +{{#isQueryParam}}@retrofit2.http.Query("{{baseName}}") {{#collectionFormat}}{{^isCollectionFormatMulti}}@{{{collectionFormat.toUpperCase}}} {{/isCollectionFormatMulti}}{{/collectionFormat}}{{paramName}}: {{{dataType}}}{{^required}} = null{{/required}}{{/isQueryParam}} \ No newline at end of file diff --git a/samples/junit-tests/src/main/java/com/yelp/codegen/generatecodesamples/apis/ResourceApi.kt b/samples/junit-tests/src/main/java/com/yelp/codegen/generatecodesamples/apis/ResourceApi.kt index e830258a..064ee998 100644 --- a/samples/junit-tests/src/main/java/com/yelp/codegen/generatecodesamples/apis/ResourceApi.kt +++ b/samples/junit-tests/src/main/java/com/yelp/codegen/generatecodesamples/apis/ResourceApi.kt @@ -116,11 +116,11 @@ interface ResourceApi { ) @GET("/symbols/in/parameter/name") fun getSymbolsInParameterName( - @retrofit2.http.Query("parameter") parameter: String?, - @retrofit2.http.Query("brackets[]") brackets: String?, - @retrofit2.http.Query("brackets[withText]") bracketsWithText: String?, - @retrofit2.http.Query("dot.") dot: String?, - @retrofit2.http.Query("dot.withText") dotWithText: String? + @retrofit2.http.Query("parameter") parameter: String? = null, + @retrofit2.http.Query("brackets[]") brackets: String? = null, + @retrofit2.http.Query("brackets[withText]") bracketsWithText: String? = null, + @retrofit2.http.Query("dot.") dot: String? = null, + @retrofit2.http.Query("dot.withText") dotWithText: String? = null ): Completable /** * The endpoint is owned by junittests service owner diff --git a/samples/junit-tests/src/test/java/com/yelp/codegen/generatecodesamples/ValidParameterTest.kt b/samples/junit-tests/src/test/java/com/yelp/codegen/generatecodesamples/ValidParameterTest.kt index ba7e70fd..3cc135c5 100644 --- a/samples/junit-tests/src/test/java/com/yelp/codegen/generatecodesamples/ValidParameterTest.kt +++ b/samples/junit-tests/src/test/java/com/yelp/codegen/generatecodesamples/ValidParameterTest.kt @@ -3,6 +3,7 @@ package com.yelp.codegen.generatecodesamples import com.yelp.codegen.generatecodesamples.apis.ResourceApi import com.yelp.codegen.generatecodesamples.tools.MockServerApiRule import okhttp3.mockwebserver.MockResponse +import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Rule @@ -36,4 +37,22 @@ class ValidParameterTest { assertTrue("dot.=testDot" in requestPath) assertTrue("dot.withText=testDotWithText" in requestPath) } + + @Test + fun optionalParameters() { + mockServerRule.server.enqueue(MockResponse()) + + val defaultApi = mockServerRule.getApi() + val pet = defaultApi.getSymbolsInParameterName().blockingGet() + + val requestPath = mockServerRule.server.takeRequest().path + assertNull(pet) + + // No parameters should be present in the path + assertFalse("parameter=" in requestPath) + assertFalse("brackets%5B%5D=" in requestPath) + assertFalse("brackets%5BwithText%5D=" in requestPath) + assertFalse("dot.=" in requestPath) + assertFalse("dot.withText=" in requestPath) + } }