vert-x3 / vertx-service-proxy

EventBus Proxy generation
Apache License 2.0
67 stars 58 forks source link

`toJson` required in a `DataObject`, or will fail to generate service proxy #86

Closed arniu closed 5 years ago

arniu commented 5 years ago

A DataObject without toJson:

package io.vertx.example.entity;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;

@DataObject
public class User {
  public User(JsonObject json) {
    // ...
  }
}

Fail to run ./gradlew build:

> Task :compileJava FAILED
Processed element io.vertx.example.service.impl.UserServiceImpl is in an implementation package
Could not generate model for io.vertx.example.service.UserService#find(io.vertx.core.json.JsonObject,io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.List<io.vertx.example.entity.User>>>): type io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.List<io.vertx.example.entity.User>>> is not legal for use for a parameter in code generation
io.vertx.codegen.GenException: type io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.List<io.vertx.example.entity.User>>> is not legal for use for a parameter in code generation
        at io.vertx.codegen.ClassModel.checkParamType(ClassModel.java:275)
        at io.vertx.codegen.ClassModel.getParams(ClassModel.java:1045)
        at io.vertx.codegen.ClassModel.createMethod(ClassModel.java:893)
        at io.vertx.codegen.ClassModel.lambda$traverseType$12(ClassModel.java:742)
...

While with toJson, it's OK:

@DataObject
public class User {
  public User(JsonObject json) {
    // ...
  }

+ public JsonObject toJson() {
+   return JsonObject.mapFrom(this);
+ }
}
slinkydeveloper commented 5 years ago

It's not legal to use a data object without toJson() as return type (in this case async return type): https://github.com/vert-x3/vertx-codegen#data-objects

Optionally a data object can define a public io.vertx.core.json.JsonObject toJson() method: such method makes the data object convertible to JsonObject, the data object can then be used as an Api return type.