On Thu, Jan 23, 2020 at 1:12 PM TimB <[email protected]> wrote:
> Hi there,
> maybe I have an regression issue with Jackson 2.10.. but maybe I doing
> something wrong... hope you can help me out.
>
> A simple Test shows the problem. We having a class like this:
>
> @JsonTypeInfo(
> use = JsonTypeInfo.Id.MINIMAL_CLASS,
> include = JsonTypeInfo.As.PROPERTY,
> property = "@class"
> )
> public class EnumContaintingClass <ENUM_TYPE extends Enum<ENUM_TYPE>> {
> private ENUM_TYPE selected;
> private List<ENUM_TYPE> options;
>
> public EnumContaintingClass(){
> }
>
> public EnumContaintingClass(ENUM_TYPE selected, Class<ENUM_TYPE> cls) {
> this.selected = selected;
> readOptionsFromEnum(cls);
> }
>
> private void readOptionsFromEnum(Class < ENUM_TYPE > enumClass) {
> ENUM_TYPE[] enumConstants = enumClass.getEnumConstants();
> options = Arrays.asList(enumConstants);
> }
> // getter and setter here..
> }
>
>
> And a Test for a roundtrip:
>
> public class JacksonTestCase {
> public enum TestEnum { FIRST, SECOND, THIRD; }
>
> @Test
> public void serialize() throws IOException {
> EnumContaintingClass gui = new EnumContaintingClass(TestEnum.SECOND,
> TestEnum.class);
> ObjectMapper mapper = new ObjectMapper();
> String str = mapper.writer().writeValueAsString(gui);
> Object o = mapper.readerFor(EnumContaintingClass.class).readValue(str);
> Assert.assertNotNull(o);
> }
> }
>
>
> Serializing works fine but deserzializing fails with
>
> *Cannot deserialize Class java.lang.Enum (of type enum) as a Bean*
>
>
> This testcase works fine with the latest 2.9 Jackson but with 2.10 it
> fails.
> I tried to add the TypeInfos also to the member but this didn't make it
> works. The only thing I could do is changing from <ENUM_TYPE extends Enum<
> ENUM_TYPE>> to <ENUM_TYPE>.
> But that is not what we want to have, only Enums shall be allowed there
> (to indicate this I added the readOptionsFromEnum-Method).
>
> So please could you guide me how to make this work in Jackson 2.10 or is
> this an regression issue?
>
I don't see how it should have worked ever, actually: problem is that type
information is missing: there is just type variable `ENUM_TYPE`, of some
Enum type. This will not be enough to deserialize anything back. Use of
polymorphic typing will not likely help, being orthogonal to generic types
(and typically combination does not work well).
So, instead of `EnumContaintingClass` as type (which is raw type, with
unbound type variable), you should use `EnumContaintingClass<TestEnum>` (or
whatever type you are using). That should be enough on deserialization:
however, may or may not cause problems during serialization: you might need
to force root type:
mapper.writerFor(new TypeReference<EnumContainingClass<TestEnum>>() { })
.writeValue(....)
Another thing to try that could help would be to use `@JsonTypeInfo` for
properties:
private ENUM_TYPE selected;
private List<ENUM_TYPE> options;
and NOT on class: use at class level in this case does not make much sense
actually as it will only retain type `EnumContainingClass` (which is not
polymorphic), but does nothing to type variable/binding. So it could be
dropped; it does not serve purpose as far as I can see.
This may be due to misunderstanding on role of this annotation: it does not
actually apply to members of annotated class but only to instances of that
class.
-+ Tatu +-
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/CAGrxA27Hf3%3DP_TUMp%3Dj%3DEgg9OvT-ubxLHHqW00LdH0gbURanEg%40mail.gmail.com.