-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JsonTypeInfo.As.EXTERNAL_PROPERTY
does not work with record wrappers
#3342
Comments
Has there been any investigation into this? It's a bit annoying to have to use one class file when all of the rest of my stuff is using records. I'm doing this a bit differently (using the JsonTypeInfo and JsonSubTypes annotations) so here is a test case demonstrating how using records fails while using a class works: import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class JacksonRecordTypeInfoTest {
// This test works since it's using the ParentClass class type
@Test
void testJsonSubTypesUsingClass() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
ParentClass parent = objectMapper.readValue("""
{"type": "CHILLED", "child": {}}
""", ParentClass.class);
Assertions.assertTrue(parent.child instanceof ChilledChild);
}
// This test fails since it uses the ParentRecord record type
@Test
void testJsonSubTypesUsingRecord() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
ParentRecord parent = objectMapper.readValue("""
{"type": "CHILLED", "child": {}}
""", ParentRecord.class);
Assertions.assertTrue(parent.child instanceof ChilledChild);
}
public enum ParentType {
CHILLED,
AGGRESSIVE
}
public static class ParentClass
{
ParentType type;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type"
)
@JsonSubTypes({
@JsonSubTypes.Type(value = AggressiveChild.class, name = "AGGRESSIVE"),
@JsonSubTypes.Type(value = ChilledChild.class, name = "CHILLED")
})
ChildBase child;
}
public record ParentRecord(
ParentType type,
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type"
)
@JsonSubTypes({
@JsonSubTypes.Type(value = AggressiveChild.class, name = "AGGRESSIVE"),
@JsonSubTypes.Type(value = ChilledChild.class, name = "CHILLED")
})
ChildBase child
){}
public interface ChildBase {
}
public record AggressiveChild(String someString) implements ChildBase {
}
public record ChilledChild(String someString) implements ChildBase {
}
} |
I have not had any time to work on this unfortunately. I am not sure if this is due to general issues with Records (related to introspection of Constructors wrt other properties), or specifically because I have been hoping to rewrite property introspection for Jackson 2.14, and if I ever find time to do that, it would help unravel a set of record-related issues, including this one. |
JsonTypeInfo.As.EXTERNAL_PROPERTY
does not work with record wrappers
Describe the bug
When I try to use
JsonTypeInfo.As.EXTERNAL_PROPERTY
inside a record, I getNote that it works with normal classes. Code examples below.
Version information
2.13.0
To Reproduce
Using a record as wrapping object: (Fails)
Using a class as wrapping object: (Passes)
Expected behavior
Should work with records, too.
For now, using normal class as workaround.
Additional context
(none)
The text was updated successfully, but these errors were encountered: