Closed yrachid closed 3 years ago
Updates
After some further testing I found out that using the @JsonProperty
annotation might solve the problem for the Pojo scenarios given that it will only work when applied to constructor parameters instead of class fields:
Just one note: although use of Jackson with Lombok should be fine as things are, all reproductions of problems need to be without use of Lombok itself -- just using annotations like Lombok pre-processor adds into byte code.
There are no known issues with Java 9 at this point (wrt 2.9.4, unit test suite) that I am aware of.
But one thing to note, wrt constructor/field/method annotations -- unless you also register Java 8 parameter names module, constructor parameter names are not available, and as such linkage can not be established. This is probably relevant here. So whereas fields have "implicit" name (name of field in bytecode) and possibly "explicit" name, override (from annotations), constructor parameters do not. This matters when combining individual accessors (ctor parameter, field, getter, setter) into single logical property, and combining information from all annotations, to avoid having to add multiple instances of same annotation.
Thank you for the details. I have a few questions:
all reproductions of problems need to be without use of Lombok itself
In the repository I sent previously there are test cases where the classes do not use Lombok at all (please see pojo.PersonAllArgs
class) but Lombok is still in the classpath of that project, do you believe that can be an issue?
just using annotations like Lombok pre-processor adds into byte code.
Do you mean that just by having Lombok in the classpath the results can be affected? Sorry for asking this kind of unrelated question, I was just wondering if you would know anything about it.
unless you also register Java 8 parameter names module, constructor parameter names are not available, and as such linkage can not be established.
In that same repository the Jdk8Module
is registered in the ObjectMapper
(see here), are you referring to that module specifically? Or is there another module to configure Java 8 parameter names
?
I have another project (using JDK 8) that uses Lombok and Jacksons' JDK8Module
and in that project the use of JsonCreator
and JsonProperty
annotations are not required, Jackson works fine without them even in conjunction with Lombok annotations like @RequiredArgsConstructor
or @AllArgsConstructor
and after trying to migrate that project to JDK 9, things stopped working. That's the reason why I believe this could be JDK 9 related.
Once again thank you very much for your attention! I will create another repository without having Lombok in the classpath to try reproducing the issue.
Update
I was able to reproduce the problem in a project without Lombok as a dependency:
Please see the project here.
What I meant was not that Lombok couldn't be somewhere, but rather that it can not be a required dependency by test(s). This because as far as I know, it is not enough to add a dependency, but additional jar has to be deployed with jdk, and I want to keep project free of any external dependencies beyond Maven. That is, when cloning git repo, all you need is Maven (and JDK for it to use), after which build should should succeed from clean slate. (I also in general try to minimize lib dependencies by core components, but in some cases new test dependencies may be added)
Thank you for reproduction!
For reference: This seems to be the related issue in the Lombok project: https://github.com/rzwitserloot/lombok/issues/1563
unless you also register Java 8 parameter names module, constructor parameter names are not available, and as such linkage can not be established.
In that same repository the
Jdk8Module
is registered in theObjectMapper
(see here), are you referring to that module specifically? Or is there another module to configureJava 8 parameter names
?
Yes, namely ParameterNamesModule.
It seems to be a problem in java 11 as well.
It seems to be a problem in java 11 as well.
I was able to fix this issue with JDK 14 by adding @ConstructorProperties annotation.
Since core jackson-databind
does not automatically find parameter names -- jackson-module-parameter-names
(from jackson-modules-java8
) is needed -- then one of following is needed:
@JsonCreator
to indicate constructor to use, along with @JsonProperty
-- or, @ConstructorProperties
jackson-modules-java8
, along with reproduction (possibly one that was linked to earlier -- but if possible, just inline minimal repro here)As to jackson-databind
, I think a better exception would make sense, if possible, to try to indicate problem better. But that may be challenging too, depending on where all information is available (wrt. potential candidates)
At this point not sure what could be done here: closing for now; may be re-filed with up-to-date reproduction with 2.11.x (or later).
unless you also register Java 8 parameter names module
I just spent so much time on this... so much time... because I are dumb (or rather spoiled by spring boot) I was missing both the databind and the parameters module, is there any way the thing that throws this error could detect that they, or at least the latter is missing and throw a better error?
@xenoterracide I am open to improvements, but it is typically quite difficult to figure out something that might be needed is missing. This is problematic for another common problem case: javac not being configured to include parameter names in bytecode -- but since that alone may be intentional (older code compiled with older jdk; JDK itself I think leaves names out on purpose), missing names is not an error condition.
For 2.13 I will probably suggest upgrade of baseline to be Java 8 (same as what 3.0/master
already requires) and if so, parameter names functionality could be included directly and not as module. That would probably reduce these issues.
maybe the error message could just be improved? like "make sure you have -parameters enabled, and have the parameters-name-module` registered. I want paragraph long error messages! (or like, links to them?)
It seems to be a problem in java 11 as well.
Adding @ConstructorProperties annotation works in java 11 as well.
Jackson-databind JDK9 issue
When used with JDK 9, Jackson's
ObjectMapper
launches acom.fasterxml.jackson.databind.exc.InvalidDefinitionException
while trying to read JSON content into a class that declares constructors with arguments. It works when the same is attempted with a class that declares a constructor with no arguments.The issue 393 shows a very similar problem. The only difference is that in such issue the problem was related to Lombok annotations whereas in the current scenario the problem happens in both Pojos and Lombok annotated classes.
At the aforementioned issue's discussions someone suggested disabling the
INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES
feature from the Mapper but that did not solve the current problem and the exception is still being thrown.Still in that discussion, the
@JsonCreator
annotation was mentioned as a possible way to solve the problem. Such annotation was added on the cases with constructor arguments but the problem still persists (with or without@JsonCreator
).Test Scenarios
This repository was created as an attempt to reproduce the error.
There are four scenarios reproduced in the given repository:
Pojos:
Lombok generated constructors:
Given scenarios were run with and without applying the
@JsonCreator
annotation to the classes.When running the tests, the following output is presented:
When
@JsonCreator
is applied:When
@JsonCreator
is absent:Reproducing
There are two ways to reproduce the error in the aforementioned repository:
Without Docker:
With Docker:
Using OpenJDK 9.0.1 image
Information
The problem was produced under the following circumstances:
Jackson versions tested:
Operating System:
JDK Version:
Gradle version:
Sorry if this is the wrong place or if this issue was already reported. Would you have any advice on that?
Thanks in advance!