jooby icon indicating copy to clipboard operation
jooby copied to clipboard

Use jooby spec with ebean enhanced classes

Open ajcamilo opened this issue 9 years ago • 7 comments

I'm building a project that uses jooby with ebean.

I have the following entity:

@Entity
public class Author {
    @Id
    public int id;
    public String name;
    public String email;
    public Author(int id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }
    public int getId() { return id; }
    public String getName() { return name; }
    public String getEmail() { return email; }
}

And the following route:

use("api/author")
  /**
    * Get list of all authors
    *
    * @return Returns list of all authors
    */
  .get(req -> {
    EbeanServer ebean = require(EbeanServer.class);
    List<AuthorImpl> authors = new LinkedList<>(new QAuthorImpl(ebean).findList());
    return authors;
  });

When I try to generate the spec with jooby:spec and I go inspect the RAML spec file, I see that my Author class has pulled all the things that ebean enhanced with.

Is the jooby:spec looking into the enhanced Author class?

ajcamilo avatar Apr 11 '17 18:04 ajcamilo

Well, yea. Guess ebean rewrite Author.class

What if you run jooby:spec in an early phase than ebean enhancer?

jknack avatar Apr 11 '17 18:04 jknack

I don't know how I can do this. Here is the build section of my pom.xml

  <build>
    <plugins>
      <!-- Build fat jar -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
      </plugin>

      <!-- api spec -->
      <plugin>
        <groupId>org.jooby</groupId>
        <artifactId>jooby-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>spec</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- data -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>add-ebeanquery-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>target/generated-sources/annotations</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>io.repaint.maven</groupId>
        <artifactId>tiles-maven-plugin</artifactId>
        <version>2.8</version>
        <extensions>true</extensions>
        <configuration>
          <tiles>
            <tile>org.avaje.tile:java-compile:1.1</tile>
            <tile>io.ebean.tile:enhancement:2.6</tile>
          </tiles>
        </configuration>
      </plugin>
    </plugins>
  </build>

ajcamilo avatar Apr 12 '17 12:04 ajcamilo

Looks good, just add the <phase>generate-sources</phase> to the jooby:spec and tiles-maven-plugin. Think that will do it.

jknack avatar Apr 12 '17 13:04 jknack

It doesn't work either, the result is the same :cry:

I also tried another approach that would make some sense, that was abstracting the Author class this way:

public interface Author {
    int getId();
    String getName();
    String getEmail();
}

Then created the AuthorImpl that was the annotated class:

@Entity
public class AuthorImpl implements Author {
    @Id
    public int id;
    public String name;
    public String email;
    public AuthorImpl(int id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }
    public int getId() { return id; }
    public String getName() { return name; }
    public String getEmail() { return email; }
}

And then, in the route had the following:

use("api/author")
  /**
    * Get list of all authors
    *
    * @return Returns list of all authors
    */
  .get(req -> {
    EbeanServer ebean = require(EbeanServer.class);
    List<Author> authors = new LinkedList<>(new QAuthorImpl(ebean).findList());
    return authors;
  });

This way the jooby:spec goal didn't look into the enhanced class. But then came the problem that the plugin does not look the the definition of the POJO methods, but only looks into the declarations. Am I right with this one?

The result was that in the spec, the Author class was empty.

ajcamilo avatar Apr 12 '17 13:04 ajcamilo

don't remember need to check, but what you did looks good and should work. so there is probably a bug here

jknack avatar Apr 12 '17 14:04 jknack

I had this solution https://github.com/ajcamilo/jooby/commit/93c4d03999448b4317538ec05f3432f69a0dcf6b

And it works, because all properties added by ebean start with _.

Is this a good solution for you? Do you want me to create a pullrequest with this?

ajcamilo avatar Apr 28 '17 17:04 ajcamilo

Pull merged it. Resolving this issue.

jknack avatar May 21 '17 15:05 jknack