OpenAPITools / OpenAPITools/openapi-generator
[BUG][KOTLIN] Generator outputs Type Mismatch for `object` body param
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The Kotlin multiplatform generator outputs code with syntax errors due to a type mismatch between the request body and private class created to represent the request body.
open suspend fun postFoo(body: kotlin.String?): HttpResponse<kotlin.String> { // body is a String
val localVariableAuthNames = listOf<String>()
val localVariableBody = PostFooRequest(body) // body passed to request object constructor
val localVariableQuery = mutableMapOf<String, List<String>>()
val localVariableHeaders = mutableMapOf<String, String>()
val localVariableConfig = RequestConfig<kotlin.Any?>(
RequestMethod.POST,
"/foo/post",
query = localVariableQuery,
headers = localVariableHeaders
)
return jsonRequest(
localVariableConfig,
localVariableBody,
localVariableAuthNames
).wrap()
}
@Serializable
private class PostFooRequest(val value: Map<kotlin.String, kotlin.String>) { // constructor takes map not string
@Serializer(PostFooRequest::class)
companion object : KSerializer<PostFooRequest> {
private val serializer: KSerializer<Map<kotlin.String, kotlin.String>> = serializer<Map<String, kotlin.String>>()
override val descriptor = serializer.descriptor
override fun serialize(encoder: Encoder, obj: PostFooRequest) = serializer.serialize(encoder, obj.value)
override fun deserialize(decoder: Decoder) = PostFooRequest(serializer.deserialize(decoder))
}
}
Yields
Type mismatch: inferred type is String but Map<String, String> was expected
openapi-generator version
5.4.0
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: Request body data type mismatch example
version: '1.0'
description: Error example
contact:
name: craigberry1
servers:
- url: 'http://localhost:3000'
paths:
/foo/post:
post:
summary: Get User Info by User ID
tags: []
responses:
'200':
description: OK
content:
application/json:
schema:
type: string
operationId: post-foo
description: Post endpoint that takes plain object in request body referenced from model
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/CategoriesGetRequest'
parameters: []
components:
schemas:
CategoriesGetRequest:
title: CategoriesGetRequest
type: object
Generation Details
openapi-generator generate -i request-body-example.yaml -g kotlin --library multiplatform
Steps to reproduce
- Create a schema model of type
objectand no other properties - Create an endpoint which accepts this schema in the request body
- Run api through kotlin/multiplatform generator
- Observe type mismatch between the request body (
kotlin.String) and the constructor args of the private class created (Map<kotlin.String, kotlin.String>)
Related issues/PRs
I have not found any similar issues.
Suggest a fix
I am not familiar enough with the code to suggest a fix. Some questions I would ask are:
- Why is a request body of type object turned into a String?
- I am curious when these serializable request/response templates are invoked vs creating an actual model: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-client/libraries/multiplatform/serial_wrapper_request_list.mustache
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the supplied OpenAPI YAML through the kotlin generator with the multiplatform library and inspect the generated request model and method. Start with the Kotlin multiplatform templates, especially modules/openapi-generator/src/main/resources/kotlin-client/libraries/multiplatform/serial_wrapper_request_list.mustache; done means the generated client accepts the empty object body without a Kotlin type mismatch.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 39/100