eclipse-vertx / vertx-codegen

Vert.x code generator for asynchronous polyglot APIs
Apache License 2.0
105 stars 92 forks source link

@DataObject converter doesn't for Kotlin List<Enum> #330

Closed rgmz closed 3 years ago

rgmz commented 3 years ago

Version

4.0.3

Context

This issue is the product of two issues.

Firstly, @DataObject does not compile with List<Enum> or Set<Enum>fields:

Could not generate element for ...: Illegal type ? extends Enum of kind Wildcard

A workaround is to use the @JvmField annotation; however, because no getter or setter are generated the converter won't include this field.

Do you have a reproducer?

Here's a simple reproducer:

// MyDataObject.kt
@DataObject(generateConverter = true)
class MyDataObject {
  @JvmField
  var selectedEnums: List<MyEnum> = emptyList()

  // other stuff...
}
// MyDataObjectConverter.java
public class MyDataObjectConverter { 

  public static void fromJson(Iterable<Map.Entry<String, Object> json, MyDataObject obj) {
    for (Map.Entry<String, Object> member : json) {
      switch (member.getKey()) {
      }
    }
  }

  public static void toJson(MyDataObject obj, JsonObject json) { toJson(obj, json.getMap()); }

  public static void toJson(MyDataObject obj, Map<String, Object> json) {
  }
}
vietj commented 3 years ago

I don't see how this can be fixed in Vert.x, the generator does not handle generic wildcards on purpose.

you can use the work around and then write your own converter code that handle the field and delegate for the remaining fields to the generated converter.

rgmz commented 3 years ago

Disregard this.

I found an old note about this bug which appeared to still be active, however, it can actually be resolved by using @JvmSuppressWildcards:

@DataObject(generateConverter = true)
class MyDataObject {
  var selectedEnums: List<@JvmSuppressWildcards MyEnum> = emptyList()

  // other stuff...
}
vietj commented 3 years ago

ah that is a good solution indeed.