Open jufickel-b opened 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
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.
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?
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"))
}
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.
Hi,
in my
src/main/resources/codegen-config.json
I have setcompanionObject: true
. When generating the code, no Companion Objects are generated. However, the package name setting in the samecodegen-config.json
works as expected.What could be the reason?