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

classes which are references are duplicated #352

Open
huehnerlady opened this issue May 19, 2021 · 3 comments
Open

classes which are references are duplicated #352

huehnerlady opened this issue May 19, 2021 · 3 comments
Assignees
Labels

Comments

@huehnerlady
Copy link

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)
}
@wuseal
Copy link
Owner

wuseal commented May 20, 2021

@TedZen PLS have a look at this

@KundlJ
Copy link

KundlJ commented Jul 6, 2021

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.

@wuseal wuseal added the bug label Jul 27, 2021
@wuseal
Copy link
Owner

wuseal commented Jul 27, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants