Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
craigberry1 opened this issue Feb 16, 2022 · 1 comment
Open
5 of 6 tasks

Comments

@craigberry1
Copy link

craigberry1 commented Feb 16, 2022

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants