OpenAPITools / OpenAPITools/openapi-generator

[BUG] Kotlin DTO with object member not generated properly

Open
#17,658 5 comments 2 reactions 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
openapi-generator version

7.2.0, don't know about previous versions as this was my first attempt

OpenAPI declaration file content or url

With this schema :

openapi: 3.0.0
paths:
info:
  title: Eixample API
  description: The API for the Eixample backend.
  version: 0.1.0
servers:
  - description: Development
    url: https://api.dev.eixample.dev
components:
  securitySchemes:
    bearer:
      type: http
      scheme: bearer
      bearerFormat: JWT
  schemas:
    Geozone:
      type: object
      properties:
        geoJson:
          type: object
          description: The definition of the geometry for the geozone.
          additionalProperties: true
        id:
          type: string
          description: The unique identifier for the geozone.
          format: uuid
      required:
        - geoJson
        - id

I obtain this class:

/**
 *
 * Please note:
 * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * Do not edit this file manually.
 *
 */

@file:Suppress(
    "ArrayInDataClass",
    "EnumEntryName",
    "RemoveRedundantQualifierName",
    "UnusedImport"
)

package com.heetch.common.business.repositories.eixampleApi.dto

import com.heetch.common.business.repositories.eixampleApi.dto.AllowedLocation

import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.Contextual

/**
 * 
 *
 * @param geoJson The definition of the geometry for the geozone.
 * @param id The unique identifier for the geozone.
 */
@Serializable

data class Geozone (

    /* The definition of the geometry for the geozone. */
    @Contextual @SerialName(value = "geoJson")
    val geoJson: kotlin.collections.Map<kotlin.String, kotlin.Any>,

    /* The unique identifier for the geozone. */
    @Contextual @SerialName(value = "id")
    val id: java.util.UUID,

Which causes an error on val geoJson: kotlin.collections.Map<kotlin.String, kotlin.Any> described as:

Serializer has not been found for type 'Any'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
Generation Details

Gradle plugin task:

openApiGenerate {
    generatorName.set("kotlin")
    inputSpec.set("$rootDir/some.yaml")
    outputDir.set(generatedSourcesPath)
    packageName.set("some.package")
    modelPackage.set("some.package")
    configOptions.set(mapOf(
        "dateLibrary" to "java8",
        "collectionType" to "list",
        "serializationLibrary" to "kotlinx_serialization",
    ))
}
Steps to reproduce

Execute the openApiGenerate task

Related issues/PRs
Suggest a fix

Adding @Contextual to the second type of Map : kotlin.collections.Map<kotlin.String, @Contextual kotlin.Any> seems to fix the issue.

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

Start by reproducing the issue with the Gradle openApiGenerate task, using the Kotlin generator, kotlinx_serialization, and the provided schema. Trace how an object with additionalProperties becomes the generated Kotlin Map type; done means the generated DTO compiles without the kotlinx.serialization Any serializer error.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.