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] figma OpenAPI specification to kotlin #20601

Open
4 of 6 tasks
bernardladenthin opened this issue Feb 5, 2025 · 0 comments
Open
4 of 6 tasks

[BUG] figma OpenAPI specification to kotlin #20601

bernardladenthin opened this issue Feb 5, 2025 · 0 comments

Comments

@bernardladenthin
Copy link

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
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

1 participant