micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6k stars 1.04k forks source link

Micronaut 3 to 4 migration issue: request.getHaaders.getFirst (key, type.class) can no longer convert #10914

Open sdelamo opened 2 weeks ago

sdelamo commented 2 weeks ago

Discussed in https://github.com/micronaut-projects/micronaut-core/discussions/10899

Originally posted by **ethanhere** June 10, 2024 In micronaut 3 we have the following code working io.micronaut.http.HttpRequest request = ... HttpHeaders headers = request.getHeaders(); Optional optionalMyAppId = headers.getFirst("id", MyAppId.class); final MyAppId appId = optionalMyAppId.orElse(null); Where MyAppId is a pojo class: public final class MyAppId { private final String value; private MyAppId(String value) { this.value = value; } @JsonCreator public static MyAppId valueOf(@JsonProperty String value) { MyAppIdValidator.validateMyAppId(value); return new MyAppId(value); } @JsonCreator public static AccountId of(@JsonProperty String value) { return valueOf(value); } } In micronaut 3, it will get the value of "id" header and use MyAppId.of method to create a MyAppId object. This code no longer works in micronaut 4. I debugged into the code and found in https://github.com/micronaut-projects/micronaut-core/blob/0118db96962eaf7a83abbcb81c1afd73139ff777/core/src/main/java/io/micronaut/core/convert/DefaultMutableConversionService.java#L237 It will return EmptyAnnotationMetadata and not able to use of method to convert. Can you advise how to make our original code working?
sdelamo commented 2 weeks ago

reproducible as attached by user. githubrepro.zip