Describe the bug
Given the following Entity class:
package org.acme.db;
import io.quarkus.mongodb.panache.PanacheMongoEntity;
public class TestEntity extends PanacheMongoEntity {
public String name;
}
When I use it in the following Resource:
package org.acme;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.acme.db.TestEntity;
@Path("/hello")
public class ExampleResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
TestEntity entity = new TestEntity();
entity.name = "a";
entity.persist();
return "hello";
}
}
I get the following error:
2020-10-05 11:37:48,098 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (executor-thread-1) HTTP Request to /hello failed,
error id: ba6ed126-bc40-491a-a01a-3e4b1fece1eb-1: org.jboss.resteasy.spi.UnhandledException:
java.lang.IllegalAccessError: class org.acme.ExampleResource tried to access protected field org.acme.db.Entity.name
(org.acme.ExampleResource and org.acme.db.Entity are in unnamed module of loader
io.quarkus.bootstrap.classloading.QuarkusClassLoader @6b975803)
Please note that TestEntity and ExampleResource are in different java packages.
Expected behavior
I can interact with the public fields of the entity from any other class. This is also what the panache documentation claims:
Use public fields. Get rid of dumb getter and setters. Under the hood, we will generate all getters and setters that are missing, and rewrite every access to these fields to use the accessor methods. This way you can still write useful accessors when you need them, which will be used even though your entity users still use field accesses.
— https://quarkus.io/guides/mongodb-panache#how-and-why-we-simplify-mongodb-api
Actual behavior
It crashes with an IllegalAccessError because the fields are now protected and the accessors from the other class don't seem to be rewritten. This behavior started with the 1.8.0.CR1 update and was caused by https://github.com/quarkusio/quarkus/commit/62505279fb59ce9f28db8abf7b81e9a1ae721fe0, though the root cause seems to be the not-executed rewriting of the accessors.
To Reproduce
Steps to reproduce the behavior:
quarkus-mongodb-panache extension (code.quarkus.io)Reproducable project: code-with-quarkus-illegalaccess.zip. Just execute the tests.
Environment (please complete the following information):
/cc @loicmathieu, @evanchooly
/cc @FroMage, @loicmathieu
@dhenneke can you provide a reproducer project please ?
You can attach it inside the issue or upload it somewhere.
@loicmathieu I updated the issue and added a zip-ed example project. Just run the tests and it should fail.
@loicmathieu I also ran into this issue. This is caused by extending PanacheMongoEntity. Not sure why.
I have the same issue - currently staying at Quarkus version 1.7.5 - if i upgrade it stops working and i get the access error
It works when the entity class and the resource class are in the same package. Possible workaround.
Same problem here, you just need to extend PanacheMongoEntityBase and it crashes, some like java.lang.IllegalAccessError: class UserService tried to access protected field.
@evanchooly apparently, since Quarkus 1.8 if a Mongo Entity is not in the same package as the classe that uses it and we use public fields, it fails with java.lang.IllegalAccessError: class UserService tried to access protected field.
As a lot of changes has been done on Quarkus 1.8 for the support of Kotlin, maybe something sliped through, can you check the bytecode enhancement part ?
Definitely.
Hi,
I confirm, this error is still present with the latest Quarkus version (1.10.2). Do we have some news about why this happen?
This is a blocking bug for using MongoDb in a Quarkus application. Put all classes in the same package is a workaround but we can't imagine this solution for real projects.
Thanks
@avdev4j currently working on this. i know the problem. i just have to wire it up correctly to fix it.
Most helpful comment
Hi,
I confirm, this error is still present with the latest Quarkus version (1.10.2). Do we have some news about why this happen?
This is a blocking bug for using MongoDb in a Quarkus application. Put all classes in the same package is a workaround but we can't imagine this solution for real projects.
Thanks