-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Open
Labels
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
object
and 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
skydjol