-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Handling of annotations assigned to constructor parameters of the record
class
#4513
Comments
Could the example be translated into plain Java? Records support annotations just fine for general case, so details matter a lot. |
You are right about the For verification purposes, I have created the following code. import com.fasterxml.jackson.annotation.JsonProperty
@JvmRecord
data class JacksonTestKt(
@JsonProperty("propertyOne") val one: String,
@JsonProperty("propertyTwo") val two: String,
) import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.lang.annotation.Annotation;
import java.util.Arrays;
public class Temp {
record JacksonTest(
@JsonProperty("propertyOne") String one,
@JsonProperty("propertyTwo") String two
) {}
static void printCtorParamAnn(Class<?> c) {
System.out.println("- " + c.getSimpleName());
Arrays.stream(c.getDeclaredConstructors()[0].getParameters()).forEach(p -> {
var a = p.getAnnotation(JsonProperty.class);
if (a == null) { return; }
System.out.println("-- " + p.getName() + " " + a.annotationType());
});
}
static void printFieldParamAnn(Class<?> c) {
System.out.println("- " + c.getSimpleName());
Arrays.stream(c.getDeclaredFields())
.forEach(f -> {
for (Annotation a : f.getDeclaredAnnotations()) {
System.out.println("-- " + a.annotationType());
}
});
}
public static void main(String[] args) throws JsonProcessingException {
System.out.println("param ann");
printCtorParamAnn(JacksonTest.class);
printCtorParamAnn(JacksonTestKt.class);
System.out.println();
System.out.println("field ann");
printFieldParamAnn(JacksonTest.class);
printFieldParamAnn(JacksonTestKt.class);
}
} The result is as follows:
I believe that To reiterate, I wanted to ask if it is possible to parse the |
Ah. Ok, I agree: it should be possible to find annotations from Constructor parameters; annotations in Fields should NOT be required. But one challenging part is reproducing the issue on Java side, to be able to reproduce it, verify fix & guard against regression. One more thing: this almost certainly would be something to handle as part of major Bean Property Introspection rewrite (for which I just created #4515). |
You are absolutely right.
Thank you very much. I don't even think it should be fixed unless there is a way to annotate only parameters in |
@k163377 On its own maybe low priority, but I would definitely hope to be able to solve this case as part of general rework -- I do not think annotations on underlying Record fields should be required. |
The decompiled Kotlin bytecode looks so bizarre:
|
Likely caused by #3929. But I think the "root" cause is that weird "record class" generated by |
@yihtserns |
With #4515 now merged, this might be solvable more easily: detection and merging of annotations between Fields and Constructor parameters now works, even for implicitly (no annotations) detected Constructors (like canonical constructors of Record). But will need a Java unit test in some form. ... or add in Kotlin module I suppose. |
Describe your Issue
In
Kotlin
, annotations are given to constructor parameters of therecord
class when written in a natural way.On the other hand,
Jackson
seemed to ignore such annotations.For this reason, the following problems have been reported
FasterXML/jackson-module-kotlin#773
I am not familiar with the handling of the
record
class, but is it possible to handle annotations on parameters in the same way as the normal class?The text was updated successfully, but these errors were encountered: