question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow handling of single-arg constructor as property based by default

See original GitHub issue
class 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:closed
  • Created 7 years ago
  • Reactions:35
  • Comments:23 (18 by maintainers)

github_iconTop GitHub Comments

2reactions
lpandziccommented, Apr 19, 2021

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:

  • it doesn’t change default creator logic which might be wanted in some cases
  • it handles only cases where there’s only one constructor and that has 1 parameter that matches 1 field for name and type
  • for all other cases it backs off and lets jackson do the default behavior.

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.

2reactions
cowtowncodercommented, Sep 18, 2020

Work here progressing, and now the original case passes when mapper is constructed like so:

ObjectMapper mapper = JsonMapper.builder()
        .constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
        // add modules, configure etc
        .build();

I realized that implementing ConstructorSelector for selecting across multiple alternatives would get quite complicated so will for now leave only 3 configurable pieces:

  1. SingleArgConstructor, the most relevant here (choice of SingleArgConstructor.PROPERTIES)
  2. requireCtorAnnotation: Whether to always require annotation (@JsonCreator) or not (for those who want more not less explicit) (default: false)
  3. 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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found