You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@JsonView should allow the same property to have different typesΒ #6272
Is your feature request related to a problem? Please describe.
Let's have 2 different interfaces
public interface OneType {}
and
public interface AnotherType {}
.
This should be possible
package org.example;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
public class Serializable {
private final List<Integer> number;
@JsonView(OneType.class)
@JsonProperty("number")
int getSum() {
return number.stream().mapToInt(x->x).sum();
}
public Serializable(List<Integer> number) {this.number = number;}
@JsonView(AnotherType.class)
List<Integer> getNumber() {return number;}
}
. However, serializing such an instance fails.
Serializable serializable = new Serializable(List.of(2, 3));
ObjectMapper objectMapper = new ObjectMapper();
IO.println("one type");
IO.println(objectMapper.writerWithView(OneType.class).writeValueAsString(serializable));
IO.println("another type");
IO.println(objectMapper.writerWithView(AnotherType.class).writeValueAsString(serializable));
it types one type then fails at runtime with
Exception in thread "main" tools.jackson.databind.exc.InvalidDefinitionException: Invalid type definition for type `org.example.Serializable`: Conflicting getter definitions for property "number": org.example.Serializable#getNumber() vs org.example.Serializable#getSum()
.
Of course it succeeds if @JsonProperty("number") is changed to give the property a different name. But the point is, the @JsonView annotations mean that the property number has exactly one type.
Describe the solution you'd like
The output should be
one type
{"number":5}
another type
{"number":[2,3]}
I doubt this can or will be supported due to complexity in trying to support heterogenous per-view types in property introspection system; but will leave open as a request in case someone wants to tackle a particularly hard problem. :)
Activity
cowtowncoder commentedon Oct 11, 2026
I doubt this can or will be supported due to complexity in trying to support heterogenous per-view types in property introspection system; but will leave open as a request in case someone wants to tackle a particularly hard problem. :)
JooHyukKim commentedon Oct 11, 2026
Seems too complex honestly π€