Search before asking

  • [x] I searched in the issues and found nothing similar.

Describe the bug

As discussed with @cowtowncoder in https://github.com/spring-projects/spring-boot/issues/46659, there appears to be an increased need for @JsonCreator with Jackson 3.0.0-rc6 compared to 2.19.2.

Version Information

  • 3.0.0-rc6
  • 2.19.2

Reproduction

package com.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

import tools.jackson.databind.json.JsonMapper;

public class Example {

    public static void main(String[] args) throws Exception {
        String json = "{\"productId\":5, \"name\":\"test\", \"weight\":42}";
        JsonMapper jackson3Mapper = tools.jackson.databind.json.JsonMapper.builder().build();
        System.out.println(jackson3Mapper.readValue(json, Pojo.class).getProductId());
        ObjectMapper jackson2Mapper = com.fasterxml.jackson.databind.json.JsonMapper.builder().build();
        jackson2Mapper.registerModule(new ParameterNamesModule());
        System.out.println(jackson2Mapper.readValue(json, Pojo.class).getProductId());
    }

    public static class Pojo {

        private final int productId;

        private final String name;

        private final int weight;

        public Pojo() {
            this.productId = 0;
            this.name = null;
            this.weight = 0;
        }

        public Pojo(int productId, String name, int weight) {
            this.productId = productId;
            this.name = name;
            this.weight = weight;
        }

        public int getProductId() {
            return this.productId;
        }

        public String getName() {
            return this.name;
        }

        public int getWeight() {
            return this.weight;
        }

    }

}

The above should be compiled with -parameters. When run, it should produce the following output:

0
5

Jackson 3 uses the default constructor to create Pojo, Jackson 2 uses the three-argument constructor.

Expected behavior

I expected the non-default Pojo(int productId, String name, int weight) constructor to be used in both cases, and not just with Jackson 2. It is used with Jackson 3 when annotated with @JsonCreator or when the default constructor is removed.

Additional context

No response

Comment From: cowtowncoder

Odd: I would have expected 2.19 also work like 3 because auto-detection for parameters-taking constructor should only work if there are no other public constructors (not even no-args ("default") constructor). (2.18 and 2.19 as Creator detection was rewritten for 2.18.0).