-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kotlin] New 'jvm-spring-webclient' library (#15568)
* Added library 'jvm-spring-webclient' to Kotlin client generator * Reran all generators and generated docs * Changed target of kotlin-jvm-spring-webclient sample from 2_0 to 3_0 * Added build of kotlin-jvm-spring-webclient to github workflow
- Loading branch information
1 parent
5299935
commit 9358ab9
Showing
43 changed files
with
3,104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
generatorName: kotlin | ||
outputDir: samples/client/petstore/kotlin-jvm-spring-webclient | ||
library: jvm-spring-webclient | ||
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/kotlin-client | ||
additionalProperties: | ||
artifactId: kotlin-petstore-spring-webclient | ||
enumUnknownDefaultCase: true | ||
serializationLibrary: jackson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...pi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-webclient/api.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
{{>licenseInfo}} | ||
package {{apiPackage}} | ||
|
||
{{#jackson}} | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
{{/jackson}} | ||
|
||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.springframework.web.reactive.function.client.WebClientResponseException | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.util.MultiValueMap | ||
import org.springframework.web.util.UriComponentsBuilder | ||
import reactor.core.publisher.Mono | ||
|
||
{{#imports}}import {{import}} | ||
{{/imports}} | ||
import {{packageName}}.infrastructure.* | ||
|
||
{{#operations}} | ||
{{#nonPublicApi}}internal {{/nonPublicApi}}class {{classname}}(client: WebClient) : ApiClient(client) { | ||
{{#operation}} | ||
{{#allParams}} | ||
{{#isEnum}} | ||
/** | ||
* enum for parameter {{paramName}} | ||
*/ | ||
{{#nonPublicApi}}internal {{/nonPublicApi}}enum class {{enumName}}{{operationIdCamelCase}}(val value: {{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}kotlin.String{{/isContainer}}) { | ||
{{^enumUnknownDefaultCase}} | ||
{{#allowableValues}} | ||
{{#enumVars}} | ||
{{#jackson}} | ||
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}} | ||
{{/jackson}} | ||
{{/enumVars}} | ||
{{/allowableValues}} | ||
{{/enumUnknownDefaultCase}} | ||
{{#enumUnknownDefaultCase}} | ||
{{#allowableValues}} | ||
{{#enumVars}} | ||
{{^-last}} | ||
{{#jackson}} | ||
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}), | ||
{{/jackson}} | ||
{{/-last}} | ||
{{/enumVars}} | ||
{{/allowableValues}} | ||
{{/enumUnknownDefaultCase}} | ||
} | ||
|
||
{{/isEnum}} | ||
{{/allParams}} | ||
|
||
@Throws(WebClientResponseException::class) | ||
{{#isDeprecated}} | ||
@Deprecated(message = "This operation is deprecated.") | ||
{{/isDeprecated}} | ||
fun {{operationId}}({{#allParams}}{{{paramName}}}: {{#isEnum}}{{#isContainer}}kotlin.collections.List<{{enumName}}{{operationIdCamelCase}}>{{/isContainer}}{{^isContainer}}{{enumName}}{{operationIdCamelCase}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): Mono<{{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}> { | ||
return {{operationId}}WithHttpInfo({{#allParams}}{{{paramName}}} = {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}}) | ||
.map { it.body } | ||
} | ||
|
||
@Throws(WebClientResponseException::class) | ||
{{#isDeprecated}} | ||
@Deprecated(message = "This operation is deprecated.") | ||
{{/isDeprecated}} | ||
fun {{operationId}}WithHttpInfo({{#allParams}}{{{paramName}}}: {{#isEnum}}{{#isContainer}}kotlin.collections.List<{{enumName}}{{operationIdCamelCase}}>{{/isContainer}}{{^isContainer}}{{enumName}}{{operationIdCamelCase}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): Mono<ResponseEntity<{{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}>> { | ||
val localVariableConfig = {{operationId}}RequestConfig({{#allParams}}{{{paramName}}} = {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}}) | ||
return request<{{#hasBodyParam}}{{#bodyParams}}{{{dataType}}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}Unit{{/hasFormParams}}{{#hasFormParams}}Map<String, PartConfig<*>>{{/hasFormParams}}{{/hasBodyParam}}, {{{returnType}}}{{^returnType}}Unit{{/returnType}}>( | ||
localVariableConfig | ||
) | ||
} | ||
|
||
{{#isDeprecated}} | ||
@Deprecated(message = "This operation is deprecated.") | ||
{{/isDeprecated}} | ||
fun {{operationId}}RequestConfig({{#allParams}}{{{paramName}}}: {{#isEnum}}{{#isContainer}}kotlin.collections.List<{{enumName}}{{operationIdCamelCase}}>{{/isContainer}}{{^isContainer}}{{enumName}}{{operationIdCamelCase}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) : RequestConfig<{{#hasBodyParam}}{{#bodyParams}}{{{dataType}}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}Unit{{/hasFormParams}}{{#hasFormParams}}Map<String, PartConfig<*>>{{/hasFormParams}}{{/hasBodyParam}}> { | ||
val localVariableBody = {{#hasBodyParam}}{{! | ||
}}{{#bodyParams}}{{{paramName}}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{! | ||
}}{{^hasFormParams}}null{{/hasFormParams}}{{! | ||
}}{{#hasFormParams}}mapOf({{#formParams}} | ||
"{{{baseName}}}" to PartConfig(body = {{{paramName}}}{{#isEnum}}.value{{/isEnum}}, headers = mutableMapOf({{#contentType}}"Content-Type" to "{{contentType}}"{{/contentType}})),{{! | ||
}}{{/formParams}}){{/hasFormParams}}{{! | ||
}}{{/hasBodyParam}} | ||
val localVariableQuery = {{^hasQueryParams}}mutableMapOf<kotlin.String, kotlin.collections.List<kotlin.String>>() | ||
{{/hasQueryParams}}{{#hasQueryParams}}mutableMapOf<kotlin.String, kotlin.collections.List<kotlin.String>>() | ||
.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}} | ||
{{#isNullable}} | ||
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}}) | ||
} | ||
{{/isNullable}} | ||
{{^isNullable}} | ||
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}}) | ||
{{/isNullable}} | ||
{{/required}} | ||
{{/queryParams}} | ||
} | ||
{{/hasQueryParams}} | ||
val localVariableHeaders: MutableMap<String, String> = 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}} | ||
|
||
return RequestConfig( | ||
method = RequestMethod.{{httpMethod}}, | ||
path = "{{path}}"{{#pathParams}}.replace("{"+"{{baseName}}"+"}", encodeURIComponent({{#isContainer}}{{paramName}}.joinToString(","){{/isContainer}}{{^isContainer}}{{{paramName}}}{{#isEnum}}.value{{/isEnum}}.toString(){{/isContainer}})){{/pathParams}}, | ||
query = localVariableQuery, | ||
headers = localVariableHeaders, | ||
requiresAuthentication = {{#hasAuthMethods}}true{{/hasAuthMethods}}{{^hasAuthMethods}}false{{/hasAuthMethods}}, | ||
body = localVariableBody | ||
) | ||
} | ||
|
||
{{/operation}} | ||
private fun encodeURIComponent(uriComponent: kotlin.String): kotlin.String = | ||
UriComponentsBuilder.newInstance() | ||
.scheme("http") | ||
.host("localhost") | ||
.pathSegment(uriComponent) | ||
.build() | ||
.encode() | ||
.pathSegments | ||
.first() | ||
} | ||
{{/operations}} |
55 changes: 55 additions & 0 deletions
55
...sources/kotlin-client/libraries/jvm-spring-webclient/infrastructure/ApiClient.kt.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package {{packageName}}.infrastructure; | ||
|
||
import org.springframework.http.HttpMethod | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.springframework.http.ResponseEntity | ||
import reactor.core.publisher.Mono | ||
|
||
open class ApiClient(protected val client: WebClient) { | ||
companion object { | ||
protected const val ContentType = "Content-Type" | ||
protected const val Accept = "Accept" | ||
protected const val JsonMediaType = "application/json" | ||
} | ||
|
||
protected inline fun <reified I : Any, reified T: Any?> request(requestConfig: RequestConfig<I>): Mono<ResponseEntity<T>> { | ||
return prepare(defaults(requestConfig)) | ||
.retrieve() | ||
.toEntity(T::class.java) | ||
} | ||
|
||
protected fun <I : Any> prepare(requestConfig: RequestConfig<I>) = | ||
client.method(requestConfig) | ||
.uri(requestConfig) | ||
.headers(requestConfig) | ||
.body(requestConfig) | ||
|
||
protected fun <I> defaults(requestConfig: RequestConfig<I>) = | ||
requestConfig.apply { | ||
if (body != null && headers[ContentType].isNullOrEmpty()) { | ||
headers[ContentType] = JsonMediaType | ||
} | ||
if (headers[Accept].isNullOrEmpty()) { | ||
headers[Accept] = JsonMediaType | ||
} | ||
} | ||
|
||
private fun <I> WebClient.method(requestConfig: RequestConfig<I>)= | ||
method(HttpMethod.valueOf(requestConfig.method.name)) | ||
|
||
private fun <I> WebClient.RequestBodyUriSpec.uri(requestConfig: RequestConfig<I>) = | ||
uri { builder -> | ||
builder.path(requestConfig.path).apply { | ||
requestConfig.query.forEach { (name, value) -> | ||
queryParam(name, value) | ||
} | ||
}.build() | ||
} | ||
|
||
private fun <I> WebClient.RequestBodySpec.headers(requestConfig: RequestConfig<I>) = | ||
apply { requestConfig.headers.forEach { (name, value) -> header(name, value) } } | ||
|
||
private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) = | ||
apply { if (requestConfig.body != null) bodyValue(requestConfig.body) } | ||
} |
23 changes: 23 additions & 0 deletions
23
samples/client/petstore/kotlin-jvm-spring-webclient/.openapi-generator-ignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# OpenAPI Generator Ignore | ||
# Generated by openapi-generator https://github.com/openapitools/openapi-generator | ||
|
||
# Use this file to prevent files from being overwritten by the generator. | ||
# The patterns follow closely to .gitignore or .dockerignore. | ||
|
||
# As an example, the C# client generator defines ApiClient.cs. | ||
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line: | ||
#ApiClient.cs | ||
|
||
# You can match any string of characters against a directory, file or extension with a single asterisk (*): | ||
#foo/*/qux | ||
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux | ||
|
||
# You can recursively match patterns against a directory, file or extension with a double asterisk (**): | ||
#foo/**/qux | ||
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux | ||
|
||
# You can also negate patterns with an exclamation (!). | ||
# For example, you can ignore all files in a docs folder with the file extension .md: | ||
#docs/*.md | ||
# Then explicitly reverse the ignore rule for a single file: | ||
#!docs/README.md |
Oops, something went wrong.