OpenAPITools / OpenAPITools/openapi-generator

[REQ][KOTLIN] Empty companion object on every generated class

Open
#10,452 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

Please create an (empty) companion object for every generated class. It enables users to define extension functions on the companion.
This is useful because it allows users to add factory functions or constructor like functions to a type.

Describe the solution you'd like

Every generated (data) class should have a companion object. If it does not need to have methods it shall be empty.
Example:

definition:

components:
  schemas:
    CheckResult:
      description: Holds a check result.
      type: object
          required:
            - checkType
          properties:
            checkType:
              type: string
              description: The type of the check

generated code before:

/**
 * Holds a check result.
 * @param checkType The type of the check
 */
data class CheckResult(

    @field:JsonProperty("checkType", required = true) val checkType: kotlin.String,
)

generated code with the feature implemented:


/**
 * Holds a check result.
 * @param checkType The type of the check
 */
data class CheckResult(

    @field:JsonProperty("checkType", required = true) val checkType: kotlin.String,
){
    companion object
}

Describe alternatives you've considered

Without an existing companion object we cannot define extension functions on a type. The alternative is to use factory methods which are not discoverable just from the type, which is not the best editing experience.

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 tracing the Kotlin model-generation path that produces the CheckResult example. Verify generated output for a data class with no existing companion object; the work is done when every generated class includes an empty companion object and the generated Kotlin remains valid.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.