pwall567 / json-kotlin-gradle

Gradle JSON Schema code generation plugin
17 stars 7 forks source link

No Companion Objects generated #7

Open jufickel-b opened 12 months ago

jufickel-b commented 12 months ago

Hi,

in my src/main/resources/codegen-config.json I have set companionObject: true. When generating the code, no Companion Objects are generated. However, the package name setting in the same codegen-config.json works as expected.

What could be the reason?

pwall567 commented 12 months ago

Hi @jufickel-b , I can't think of any obvious reason why the companion object is not generated.

Can you confirm that you are using version 0.86 or later (that was the version at which this feature was introduced)? If you are using this version or later, can you please give an example of a schema and the generated code illustrating the issue.

Thanks, Peter

jufickel-b commented 12 months ago

The version I use is 0.91.

This is how a schema looks like:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "someId",
    "title": "MyBattery",
    "description": "My battery",
    "type": "object",
    "properties": {
        "soc": {
            "description": "State of charge",
            "type": "integer",
            "minimum": 0,
            "maximum": 100
        }
    },
    "required": [
        "soc"
    ]
}

And this his how the generated code looks like:

/**
 * My battery
 */
data class MyBattery(
    /** State of charge */
    val soc: Int
) {

    init {
        require(soc in 0..100) { "soc not in range 0..100 - $soc" }
    }

}

I hope, it helps.

pwall567 commented 11 months ago

Thanks for providing the additional information, but I am still unable to reproduce the issue. This is the output I get when generating from that schema:

/**
 * My battery
 */
data class MyBattery(
    /** State of charge */
    val soc: Int
) {

    init {
        require(soc in 0..100) { "soc not in range 0..100 - $soc" }
    }

    companion object

}

Are you able to provide a complete set of files (build.gradle[.kts], codegen-cofig.json and all input files) that illustrate the issue?

jufickel-b commented 11 months ago

I was able to fix the issue. For me it was necessary to explicitly add the path to the config file in order to get this config applied. So in my build.gradle.kts it now looks like:

configure<JSONSchemaCodegen> {
    inputs { inputFile(file("${project.projectDir}/../../json/schema/")) }
    configFile.set(file("src/main/resources/codegen-config.json"))
    outputDir.set(file("${project.buildDir}/generated-sources/kotlin"))
}
pwall567 commented 11 months ago

Thanks for letting me know. I'm a little concerned that setting the configFile to that setting has solved your problem – that is supposed to be the default!

I'll investigate that further, but for now, I'm happy to hear that you've found a configuration that works for you.

I'll leave the issue open for a few days in case you have ongoing problems; feel free to close it if you consider it resolved.