JsonToKotlinClass icon indicating copy to clipboard operation
JsonToKotlinClass copied to clipboard

classes which are references are duplicated

Open huehnerlady opened this issue 3 years ago • 3 comments

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

huehnerlady avatar May 19 '21 14:05 huehnerlady

@TedZen PLS have a look at this

wuseal avatar May 20 '21 02:05 wuseal

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.

KundlJ avatar Jul 06 '21 14:07 KundlJ

I'v test it, This error should be fixed in 3.7.2-EAP-10

wuseal avatar Jul 27 '21 07:07 wuseal