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

Config generated classes are not registered for reflection #22474

Closed
vsevel opened this issue Dec 22, 2021 · 5 comments · Fixed by #22499
Closed

Config generated classes are not registered for reflection #22474

vsevel opened this issue Dec 22, 2021 · 5 comments · Fixed by #22499
Assignees
Labels
area/config kind/enhancement New feature or request
Milestone

Comments

@vsevel
Copy link
Contributor

vsevel commented Dec 22, 2021

Description

In ConfigMapping we define interfaces such as com.acme.config.MyConfig. At build time this generates implementation classes such as com.acme.config.MyConfig-<hash>Impl

If you intend to use those classes through reflection, you end up having to figure out the hash that has been generated, and add @RegisterForReflection(classNames= {com.acme.config.MyConfig-1234Impl}) into your application.

In our project, we are adding the config objects into freemarker template models, and access some of the config properties from within the templates, hence the need for reflection.

Ideally those config implementation classes should be enabled for reflection, or at least (if we believe the need is rare, and/or if we fear the impact on the native exec) the application should be offered an easy way to enable those classes for reflection.

see zulip conversation

/cc @radcortez

Implementation ideas

One option would be to allow adding @RegisterForReflection on the config interfaces, which would be interpreted as: a registration on the config impl classes need to be done.

Another option would be to add a build time parameter register-config-classes-for-reflection/serialization.

@vsevel vsevel added the kind/enhancement New feature or request label Dec 22, 2021
@quarkus-bot
Copy link

quarkus-bot bot commented Dec 22, 2021

You added a link to a Zulip discussion, please make sure the description of the issue is comprehensive and doesn't require accessing Zulip.

This message is automatically generated by a bot.

@vsevel
Copy link
Contributor Author

vsevel commented Dec 22, 2021

to reproduce:

package org.acme.getting.started;

import io.smallrye.config.ConfigMapping;

@ConfigMapping(prefix = "job")
public interface JobConfig {

    String hello();
}
@Path("/hello")
public class GreetingResource {

    private static final Logger log = LoggerFactory.getLogger(GreetingResource.class);

    @Inject
    JobConfig job;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        try {
            Method helloMethod = job.getClass().getDeclaredMethod("hello");
            return (String) helloMethod.invoke(job);
        } catch (Exception e) {
            return e.toString();
        }
    }
}

application.properties:

job.hello=world
@QuarkusTest
public class GreetingResourceTest {

    @Test
    public void testHelloEndpoint() {
        given()
          .when().get("/hello")
          .then()
             .statusCode(200)
             .body(is("world"));
    }
}

Build and start in native mode, and invoke the hello endpoint (works in jvm mode):

curl http://localhost:8090/hello
java.lang.NoSuchMethodException: org.acme.getting.started.JobConfig-1625885570Impl.hello()

@radcortez
Copy link
Member

Sounds reasonable. I'll add a better way to do this.

@vsevel
Copy link
Contributor Author

vsevel commented Dec 23, 2021

let me know if I can help.

@radcortez
Copy link
Member

Thanks, no worries. It is a fairly easy change.

@quarkus-bot quarkus-bot bot added this to the 2.7 - main milestone Dec 23, 2021
@gsmet gsmet modified the milestones: 2.7 - main, 2.6.1.Final Dec 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/config kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants