FasterXML / FasterXML/jackson-databind

For multiple constructors, treat a single-arg record constructor as if it were a multiple-arg constructor with `@JsonProperty` annotations

Open
#5,222 21 comments 0 reactions 0 assignees View on GitHub
3.x has-failing-test pr-welcome
Dominant language
Java
Stars
3.7k
Forks
1.5k
Avg merge
3d 6h
Merged PRs (30d)
28

Description

### Is your feature request related to a problem? Please describe.

[JacksonRecordConstructorExample.java.zip](https://github.com/user-attachments/files/21196518/JacksonRecordConstructorExample.java.zip)

I need to deserialize an object that has two constructors, a single-arg version that takes a java record, and a no-arg version. Depending on the provided JSON, I want Jackson to auto-detect which one to use.

Jackson never auto-detects the constructor that takes a record. Using `@JsonCreator` is not a good choice, because then it always uses the single-arg constructor instead of the no-arg.

### Describe the solution you'd like

Jackson does auto-detect a version that has `@JsonProperty` for each parameter, like this:
`public JacksonRecordConstructorExample( @JsonProperty("a") int a, @JsonProperty("b") int b, @JsonProperty("c") int c )`

It seems to me that it ought to treat this constructor version equivalently and auto-detect it:
`public record Triple( int a, int b, int c ) {}`

`public JacksonRecordConstructorExample( Triple triple )
`

### Usage example

See attached test case

### Additional context

I'm using Jackson 2.19.1

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.