JsonToKotlinClass
JsonToKotlinClass copied to clipboard
classes which are references are duplicated
Hi,
I wanted to separate the Issues described in #351 as it is not just enum classes, that are duplicated, but it seems every class you reference is created as often as you reference them.
Example:
I have a schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Foo",
"type": "object",
"additionalProperties": false,
"properties": {
"value1": {
"$ref": "#/definitions/Value"
},
"value4": {
"$ref": "#/definitions/Value"
}
},
"definitions": {
"Value": {
"type": "object",
"additionalProperties": false,
"properties": {
"valueAsString": {
"type": "string"
}
}
}
}
}
Which generates:
package some.package
data class Foo(
val value1: Value? = null,
val value4: Value? = null
) {
data class Value(
val valueAsString: String? = null
)
data class Value(
val valueAsString: String? = null
)
}
although I would expect:
package some.package
data class Foo(
val value1: Value? = null,
val value4: Value? = null
) {
data class Value(
val valueAsString: String? = null
)
}
The error while compiling is Duplicate JVM class name 'Foo$Value' generated from: Value, Value
I am currently using the lib wu.seal.jsontokotlin:library:3.6.1
My generation code:
fun generateKotlinFooClasses() {
val file = File("Foo.json")
val jsonString = file.readText()
val actualOutput = JsonToKotlinBuilder()
.setPackageName("some.package")
.enableVarProperties(false) // optional, default : false
.setPropertyTypeStrategy(PropertyTypeStrategy.Nullable) // optional, default : PropertyTypeStrategy.NotNullable
.setDefaultValueStrategy(DefaultValueStrategy.AllowNull) // optional, default : DefaultValueStrategy.AvoidNull
.setAnnotationLib(TargetJsonConverter.None) // optional, default: TargetJsonConverter.None
.enableOrderByAlphabetic(true) // optional : default : false
.enableInnerClassModel(true) // optional, default : false
.enableCreateAnnotationOnlyWhenNeeded(true) // optional, default : false
.setIndent(2) // optional, default : 4
.enableKeepAnnotationOnClass(false) // optional, default : false
.enableKeepAnnotationOnClassAndroidX(false) // optional, default : false
.enableAnnotationAndPropertyInSameLine(false) // optional, default : false
.enableParcelableSupport(false) // optional, default : false
.build(jsonString, "Foo") // finally, get KotlinClassCode string
val kotlinClass = File("api/Api.kt")
kotlinClass.createNewFile()
kotlinClass.writeText(actualOutput)
}
@TedZen PLS have a look at this
This appears to be an issue with both json and json schema. I'm guessing it's related to the recursive calls to find other objects but haven't been able to narrow it down further.
I'v test it, This error should be fixed in 3.7.2-EAP-10