OpenAPITools / OpenAPITools/openapi-generator

[BUG] figma OpenAPI specification to kotlin

Open
#20,601 0 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)?
    Was not possible
  • Have you tested with the latest master to confirm the issue still exists?
    I've used the latest version.
  • 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)
    I'm willing to sponsor/donate $100 for a fully working kotlin code from the figma openapi specification.
Description

I like to generate kotlin data classes from figma openapi specification

Full example: figmaopenapitokotlin.zip

openapi-generator version

id("org.openapi.generator") version "7.11.0"

OpenAPI file

https://github.com/figma/rest-api-spec/blob/main/openapi/openapi.yaml

Gradle file
group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnitPlatform()
}
kotlin {
    jvmToolchain(11)
}

plugins {
    kotlin("jvm") version "2.0.10"
    application
    id("org.jetbrains.kotlin.plugin.serialization") version "2.1.0"
    id("org.openapi.generator") version "7.11.0"
}

repositories {
    mavenCentral()
}

sourceSets.main {
    java.srcDirs("build/generated-sources/src/main")
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.8.0")
}

openApiGenerate {
    generatorName.set("kotlin")
    inputSpec.set("$projectDir/src/main/resources/figma/openapi.yaml")
    outputDir.set("$buildDir/generated-sources")
    apiPackage.set("org.example.figmaopenapitokotlin.figmaclient.api") // Package for API classes
    modelPackage.set("org.example.figmaopenapitokotlin.figmaclient.model") // Package for model classes
    invokerPackage.set("org.example.figmaopenapitokotlin.figmaclient.invoker") // Package for supporting classes
    configOptions.set(
        mapOf(
            "dateLibrary" to "java8",
            "serializationLibrary" to "kotlinx_serialization"
        )
    )
    typeMappings.set(
        mapOf(
            // does not work: Too many arguments for 'constructor(): String'.
            "number" to "kotlin.String",
            "decimal" to "kotlin.String"
        )
    )
    globalProperties.set(
        mapOf(
            "models" to "" // Generate only models
        )
    )
    skipValidateSpec.set(true)
}

tasks.register("fixGeneratedCode") {
    group = "build"
    description = "Replaces invalid kotlin.String constructor calls with string literals"

    doLast {
        val generatedSourceDir = file("build/generated-sources") // Adjust the path to match your generated sources directory
        if (generatedSourceDir.exists()) {
            generatedSourceDir.walkTopDown()
                .filter { it.isFile && it.extension == "kt" }
                .forEach { file ->
                    val updatedContent = file.readText()
                        .replace("""kotlin.String\("(.*?)"\)""".toRegex(), "\"$1\"") // Replace kotlin.String("value") with "value"
                    file.writeText(updatedContent)
                }
        } else {
            println("Generated source directory not found: $generatedSourceDir")
        }
    }
}

Generation Details
  1. .\gradlew openapigenerate
  2. .\gradlew fixGeneratedCode
    To get rid of the string constructor problem
  3. .\gradlew assemble
Steps to reproduce

gradlew shows errors

  1. Strings are created with a constructor (from number and decimal) which might be easy to solve like my fixGeneratedCode task. But there might be a better solution.
  2. There are a lot of errors like
> Task :compileKotlin FAILED
e: file:///C:/Users/bladenth/IdeaProjects/figmaopenapitokotlin/build/generated-sources/src/main/kotlin/org/example/figmaopenapitokotlin/figmaclient/model/ActivityLogEntity.kt:45:1 @Serializable annotation without arguments can be used only on sealed interfaces.Non-sealed interfaces are polymorphically serializable by default.
e: file:///C:/Users/bladenth/IdeaProjects/figmaopenapitokotlin/build/generated-sources/src/main/kotlin/org/example/figmaopenapitokotlin/figmaclient/model/BooleanOperationNode.kt:199:56 Initializer type mismatch: expected 'org.example.figmaopenapitokotlin.figmaclient.model.BooleanOperationNode.LayoutGrow?', actual 'kotlin.String'.
Related issues/PRs
Suggest a fix

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

Reproduce the generation using the linked Figma openapi.yaml and the shown Gradle openApiGenerate configuration, then inspect the generated ActivityLogEntity.kt and BooleanOperationNode.kt errors. Compare the Kotlin serialization output and number/decimal mappings with the compile failures; done means the generated models compile without the manual fixGeneratedCode task.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
backend-api-design, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.