-
-
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
Wrong serialization of Type Ids for certain types of Enum values #4733
Comments
Interesting analysis. I think we can come up with some improvements. But sady serialization and deserialization symmetry is and always be in most cases achieved best-effort. |
Yes, there is no guarantee that serialization will contain enough information for deserialization in the general case. Here, however, an enum constant is defined by a single identifier. Since the type is known on serialization (the second argument of
Except in |
It seems to me code itself is wrong, you should not read with
since the base type is Put another way: deserialization target type generally MUST be the level at which So I am not sure this is valid bug. |
You have a point, my example was too direct. Here is a more real scenario: import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import example.Inter.A;
@JsonTypeInfo(use = Id.MINIMAL_CLASS)
@JsonSubTypes({
@JsonSubTypes.Type(value = A.class),
})
interface Inter {
default void yes() {}
enum A implements Inter {
A1,
A2 {
@Override
public void yes() {
}
},
}
}
class Ser {
@JsonProperty
Inter inter1;
@JsonProperty
Inter inter2;
// toString
}
public class Start {
public static void main(String[] args) throws JsonProcessingException {
JsonMapper MAPPER = JsonMapper.builder().build();
Ser ser = new Ser();
ser.inter1 = A.A1;
ser.inter2 = A.A2;
var string1 = MAPPER.writeValueAsString(ser);
System.out.println(string1);
Ser value1 = MAPPER.readValue(string1, Ser.class);
System.out.println(value1);
}
} My
With
but there is enough information to deserialize correctly. Similarly,
And
|
Ok, so the biggest hurdle here comes from Enum implementation (by JDK) -- Value As to name to use for inner classes, that's probably another related problem, needing to consider case of inner classes. Although for |
Yes, I don't see why not, all the info is there. For example, with
|
Actually, having said all of that; maybe the problem is reverse: sub-class of Enum I think there are couple of special type for which class used for getting Type Id is changed as part of processing. Would just need to remember where this was done. |
If it solves the problem I don't see why it wouldn't be valid. |
@nlisker Right, just writing out aloud possible ways to tackle the issue (and what the issue actually is) :) |
Search before asking
Describe the bug
This class serializes and deserializes enum constants. I assume that when serializing there should be enough info to deserialize it, and if deserialization fails, then it's a bug.
Results for
Id
values:CLASS
NONE
MINIMAL_CLASS
NAME
SIMPLE_NAME
DEDUCTION
I'm not sure what is considered correct behavior. Except for
DEDUCTION
, I would assume they should all pass.Version Information
2.18.0
2.17.2
The text was updated successfully, but these errors were encountered: