OpenAPITools / OpenAPITools/openapi-generator

[BUG][KOTLIN] Generator outputs Type Mismatch for `object` body param

Open
#11,628 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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
  1. Create a schema model of type object and no other properties
  2. Create an endpoint which accepts this schema in the request body
  3. Run api through kotlin/multiplatform generator
  4. 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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.