Skip to content

Commit 9753086

Browse files
authored
Fix #13369: kotlin-client with okhttp doesn't escape path parameters with slashes correctly (#13370)
* Fix #13369 * Regenarate samples * Fix support for okhttp3
1 parent 0b66045 commit 9753086

File tree

66 files changed

+315
-135
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+315
-135
lines changed

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/api.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package {{apiPackage}}
33

44
import java.io.IOException
55
import okhttp3.OkHttpClient
6+
import okhttp3.HttpUrl
67

78
{{#imports}}import {{import}}
89
{{/imports}}
@@ -224,13 +225,16 @@ import {{packageName}}.infrastructure.toMultiValue
224225

225226
return RequestConfig(
226227
method = RequestMethod.{{httpMethod}},
227-
path = "{{path}}"{{#pathParams}}.replace("{"+"{{baseName}}"+"}", {{#isContainer}}{{paramName}}.joinToString(","){{/isContainer}}{{^isContainer}}{{{paramName}}}{{#isEnum}}.value{{/isEnum}}.toString(){{/isContainer}}){{/pathParams}},
228+
path = "{{path}}"{{#pathParams}}.replace("{"+"{{baseName}}"+"}", encodeURIComponent({{#isContainer}}{{paramName}}.joinToString(","){{/isContainer}}{{^isContainer}}{{{paramName}}}{{#isEnum}}.value{{/isEnum}}.toString(){{/isContainer}})){{/pathParams}},
228229
query = localVariableQuery,
229230
headers = localVariableHeaders,
230231
body = localVariableBody
231232
)
232233
}
233234

234235
{{/operation}}
236+
237+
private fun encodeURIComponent(uriComponent: kotlin.String): kotlin.String =
238+
HttpUrl.Builder().scheme("http").host("localhost").addPathSegment(uriComponent).build().encodedPathSegments{{#jvm-okhttp3}}(){{/jvm-okhttp3}}[0]
235239
}
236240
{{/operations}}

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ import com.squareup.moshi.adapter
313313
{{/hasAuthMethods}}
314314

315315
val url = httpUrl.newBuilder()
316-
.addPathSegments(requestConfig.path.trimStart('/'))
316+
.addEncodedPathSegments(requestConfig.path.trimStart('/'))
317317
.apply {
318318
requestConfig.query.forEach { query ->
319319
query.value.forEach { queryValue ->

samples/client/petstore/kotlin-allOff-discriminator/src/main/kotlin/org/openapitools/client/apis/BirdApi.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package org.openapitools.client.apis
2222

2323
import java.io.IOException
2424
import okhttp3.OkHttpClient
25+
import okhttp3.HttpUrl
2526

2627
import org.openapitools.client.models.Bird
2728

@@ -112,11 +113,14 @@ class BirdApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient =
112113

113114
return RequestConfig(
114115
method = RequestMethod.GET,
115-
path = "/v1/bird/{id}".replace("{"+"id"+"}", id.toString()),
116+
path = "/v1/bird/{id}".replace("{"+"id"+"}", encodeURIComponent(id.toString())),
116117
query = localVariableQuery,
117118
headers = localVariableHeaders,
118119
body = localVariableBody
119120
)
120121
}
121122

123+
124+
private fun encodeURIComponent(uriComponent: kotlin.String): kotlin.String =
125+
HttpUrl.Builder().scheme("http").host("localhost").addPathSegment(uriComponent).build().encodedPathSegments[0]
122126
}

samples/client/petstore/kotlin-allOff-discriminator/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
145145
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
146146

147147
val url = httpUrl.newBuilder()
148-
.addPathSegments(requestConfig.path.trimStart('/'))
148+
.addEncodedPathSegments(requestConfig.path.trimStart('/'))
149149
.apply {
150150
requestConfig.query.forEach { query ->
151151
query.value.forEach { queryValue ->

samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package org.openapitools.client.apis
2222

2323
import java.io.IOException
2424
import okhttp3.OkHttpClient
25+
import okhttp3.HttpUrl
2526

2627

2728
import com.squareup.moshi.Json
@@ -108,11 +109,14 @@ class DefaultApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient
108109

109110
return RequestConfig(
110111
method = RequestMethod.GET,
111-
path = "/{ids}".replace("{"+"ids"+"}", ids.joinToString(",")),
112+
path = "/{ids}".replace("{"+"ids"+"}", encodeURIComponent(ids.joinToString(","))),
112113
query = localVariableQuery,
113114
headers = localVariableHeaders,
114115
body = localVariableBody
115116
)
116117
}
117118

119+
120+
private fun encodeURIComponent(uriComponent: kotlin.String): kotlin.String =
121+
HttpUrl.Builder().scheme("http").host("localhost").addPathSegment(uriComponent).build().encodedPathSegments()[0]
118122
}

samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
143143
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
144144

145145
val url = httpUrl.newBuilder()
146-
.addPathSegments(requestConfig.path.trimStart('/'))
146+
.addEncodedPathSegments(requestConfig.path.trimStart('/'))
147147
.apply {
148148
requestConfig.query.forEach { query ->
149149
query.value.forEach { queryValue ->

samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package org.openapitools.client.apis
2222

2323
import java.io.IOException
2424
import okhttp3.OkHttpClient
25+
import okhttp3.HttpUrl
2526

2627

2728
import com.squareup.moshi.Json
@@ -108,11 +109,14 @@ class DefaultApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient
108109

109110
return RequestConfig(
110111
method = RequestMethod.GET,
111-
path = "/{ids}".replace("{"+"ids"+"}", ids.joinToString(",")),
112+
path = "/{ids}".replace("{"+"ids"+"}", encodeURIComponent(ids.joinToString(","))),
112113
query = localVariableQuery,
113114
headers = localVariableHeaders,
114115
body = localVariableBody
115116
)
116117
}
117118

119+
120+
private fun encodeURIComponent(uriComponent: kotlin.String): kotlin.String =
121+
HttpUrl.Builder().scheme("http").host("localhost").addPathSegment(uriComponent).build().encodedPathSegments[0]
118122
}

samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
145145
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
146146

147147
val url = httpUrl.newBuilder()
148-
.addPathSegments(requestConfig.path.trimStart('/'))
148+
.addEncodedPathSegments(requestConfig.path.trimStart('/'))
149149
.apply {
150150
requestConfig.query.forEach { query ->
151151
query.value.forEach { queryValue ->

samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package org.openapitools.client.apis
2222

2323
import java.io.IOException
2424
import okhttp3.OkHttpClient
25+
import okhttp3.HttpUrl
2526

2627
import org.openapitools.client.models.Apa
2728

@@ -117,4 +118,7 @@ class DefaultApi(basePath: kotlin.String = defaultBasePath, client: OkHttpClient
117118
)
118119
}
119120

121+
122+
private fun encodeURIComponent(uriComponent: kotlin.String): kotlin.String =
123+
HttpUrl.Builder().scheme("http").host("localhost").addPathSegment(uriComponent).build().encodedPathSegments[0]
120124
}

samples/client/petstore/kotlin-bigdecimal-default-okhttp4/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
145145
val httpUrl = baseUrl.toHttpUrlOrNull() ?: throw IllegalStateException("baseUrl is invalid.")
146146

147147
val url = httpUrl.newBuilder()
148-
.addPathSegments(requestConfig.path.trimStart('/'))
148+
.addEncodedPathSegments(requestConfig.path.trimStart('/'))
149149
.apply {
150150
requestConfig.query.forEach { query ->
151151
query.value.forEach { queryValue ->

0 commit comments

Comments
 (0)