Allow handling of single-arg constructor as property based by default
See original GitHub issueclass Foo {
private final Bar bar;
Foo(Bar bar) {
this.bar = bar;
}
}
With the parameter-names-module enabled I’d expect this class to be correctly deserialized from {"bar":{...}}
.
More than once have users hit this issue and asked why isn’t it handled this way (#1135, #8, #21).
It might also be a good idea to provide an option to change this behavior (HANDLE_SINGLE_ARG_CONSTRUCTOR_AS_PROPERTY_CREATOR).
Issue Analytics
- State:
- Created 7 years ago
- Reactions:35
- Comments:23 (18 by maintainers)
Top Results From Across the Web
How can I specify a one-argument constructor using Lombok?
@RequiredArgsConstructor generates a constructor with 1 parameter for each field that requires special handling.
Read more >Why Default or No Argument Constructor is Important in Java ...
Almost all Java developers know that compiler adds a default constructor or better known as a no-argument constructor in every Java class, but...
Read more >Default Constructor in Java – Class Constructor Example
In this article, we will talk about constructors, how to create our own constructors, and what default constructors are in Java.
Read more >Providing Constructors for Your Classes (The Java™ Tutorials ...
The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument ...
Read more >@NoArgsConstructor, @RequiredArgsConstructor ...
This set of 3 annotations generate a constructor that will accept 1 parameter for certain fields, and simply assigns this parameter to the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To anyone that can’t upgrade jackson you might want to try this module - https://github.com/infobip/infobip-jackson-extension#SingleArgumentPropertyCreatorAnnotationlessSupport. It might even work for some cases where ConstructorDetector.USE_PROPERTIES_BASED doesn’t work as it works differently:
Here’s a test that probably speaks better for itself what is supported - https://github.com/infobip/infobip-jackson-extension/blob/master/infobip-jackson-extension-module/src/test/java/com/infobip/jackson/SingleArgumentPropertiesCreatorModeAnnotationIntrospectorTest.java.
Work here progressing, and now the original case passes when mapper is constructed like so:
I realized that implementing
ConstructorSelector
for selecting across multiple alternatives would get quite complicated so will for now leave only 3 configurable pieces:SingleArgConstructor
, the most relevant here (choice ofSingleArgConstructor.PROPERTIES
)requireCtorAnnotation
: Whether to always require annotation (@JsonCreator
) or not (for those who want more not less explicit) (default: false)allowJDKTypeCtors
: Even if implicit discovery allowed in general, is it allowed for JDK types (java.*, javax.)? (default: false)Of these I implemented the bigger piece (1) and need to add (2) and (3).