'openapi client generator cannot deal with multiple http 2xx response codes
I have received an openapi yaml descriptor of an existing service, and I would like to generate openapi rest client for kotlin.
I use openapi generator to generate client, but it cannot deal with multiple http 20x status codes.
I have created a simple openapi descriptor to reproduce the problem:
openapi descriptor:
openapi: 3.0.1
info:
title: TEST API
description: TEST API
version: 1.0.0
servers:
- url: /v1
paths:
/test:
post:
tags:
- test
summary: Multiple http 2xx test
description: Multiple http 2xx test
operationId: PostTest
requestBody:
description: Test request
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/TestElement'
application/xml:
schema:
type: array
items:
$ref: '#/components/schemas/TestElement'
responses:
'200':
description: response with no answare
'201':
description: response with Test Element
content:
application/json:
schema:
$ref: '#/components/schemas/TestElement'
components:
schemas:
TestElement:
required:
- name
- id
type: object
properties:
id:
type: integer
format: int32
name:
type: string
additionalProperties: false
I use gradle to generate kts openapi client, with the following code:
openApiGenerate {
generatorName.set("kotlin")
verbose.set(false)
inputSpec.set("${projectDir}/src/main/resources/rest_api_test.yaml")
outputDir.set("${buildDir}/generated")
packageName.set("com.test.client")
invokerPackage.set("com.test.client.invoker")
modelPackage.set("com.test.client.model")
configOptions.put("dateLibrary", "java8-localdatetime")
configOptions.put("serializationLibrary", "jackson")
generateModelTests.set(false)
generateApiTests.set(false)
library.set("jvm-okhttp4")
}
The generated client contains the following code:
/**
* Multiple http 2xx test
* Multiple http 2xx test
* @param testElement Test request (optional)
* @return void
* @throws UnsupportedOperationException If the API returns an informational or redirection response
* @throws ClientException If the API returns a client error response
* @throws ServerException If the API returns a server error response
*/
@Throws(UnsupportedOperationException::class, ClientException::class, ServerException::class)
fun postTest(testElement: kotlin.collections.List<TestElement>?) : Unit {
val localVariableConfig = postTestRequestConfig(testElement = testElement)
val localVarResponse = request<kotlin.collections.List<TestElement>, Unit>(
localVariableConfig
)
return when (localVarResponse.responseType) {
ResponseType.Success -> Unit
ResponseType.Informational -> throw UnsupportedOperationException("Client does not support Informational responses.")
ResponseType.Redirection -> throw UnsupportedOperationException("Client does not support Redirection responses.")
ResponseType.ClientError -> {
val localVarError = localVarResponse as ClientError<*>
throw ClientException("Client error : ${localVarError.statusCode} ${localVarError.message.orEmpty()}", localVarError.statusCode, localVarResponse)
}
ResponseType.ServerError -> {
val localVarError = localVarResponse as ServerError<*>
throw ServerException("Server error : ${localVarError.statusCode} ${localVarError.message.orEmpty()}", localVarError.statusCode, localVarResponse)
}
}
}
Reading the generate source, I see it cannot deal with http 201 json response, the processing of TestElement response strucutre is missing. ResponseType.Success is also means http 200 and http 201, so it requires empty answare for http 201 instead of a definied TestElement json structure.
Is it possible to use multiple http 2xx responses with openapi generator? Are this type of http 20x responses valid in openapi?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
