If I understand the case correctly, it seems like this should work. But since it doesn't it would make sense to file an issue and I or someone else could try to figure out why alias does not work.
-+ Tatu +- On Mon, Jul 12, 2021 at 8:44 AM Mantas Gridinas <[email protected]> wrote: > Yo! > > I'm trying to migrate a makeshift polymorphic custom deserializer (which i > spawned because I hadn't read the docs). The deserializer originally worked > like the following annotation combo as per Polymorphic Deserialization usage > > public class Container { > private String id; > @JsonTypeInfo(use = Id.NAME, include = As.EXTERNAL_PROPERTY, property > = "id") > @JsonSubTypes({ > @Type(name = "FOO", value = Foo.class), > @Type(name = "NAR", value = Nar.class) > }) > private Object target; > } > > Getters and setters omitted for brevity. > > Basically original implementation could set the target field value as > particular type depending on id field. But in addition to that, it could > determine which field to read from, since the input source (sadly) only has > one constant field (id) and target field's name would change depending on > id field. If I was not using typeinfo and subtypes annotation combo, the > annotation for aliases would be > > @JsonAlias({"fooValue", "narValue"}) > > On their own, the two features work fine, but when you would try to use > the two together, polymorphic deserializer is incapable of determining the > source value field via aliases. Is there any way to make polymorphism work > with field aliases? > > Below is the minimum working snippet to replicate my issue > > import com.fasterxml.jackson.annotation.JsonAlias; > import com.fasterxml.jackson.annotation.JsonSubTypes; > import com.fasterxml.jackson.annotation.JsonSubTypes.Type; > import com.fasterxml.jackson.annotation.JsonTypeInfo; > import com.fasterxml.jackson.annotation.JsonTypeInfo.As; > import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; > import com.fasterxml.jackson.core.JsonProcessingException; > import com.fasterxml.jackson.databind.ObjectMapper; > > public class Main { > > public static void main(String[] args) throws JsonProcessingException { > String json = "{ \"id\": \"NAR\", \"narValue\": {}}"; > ObjectMapper mapper = new ObjectMapper(); > Container container = mapper.readValue(json, Container.class); > System.out.println(container.getTarget()); // expected value is > Nar@[hashcode] > } > > public static class Nar { > > private String dar; > private String zar; > > public String getDar() { > return dar; > } > > public void setDar(String dar) { > this.dar = dar; > } > > public String getZar() { > return zar; > } > > public void setZar(String zar) { > this.zar = zar; > } > } > > public static class Foo { > private String bar; > private String zar; > > public String getBar() { > return bar; > } > > public void setBar(String bar) { > this.bar = bar; > } > > public String getZar() { > return zar; > } > > public void setZar(String zar) { > this.zar = zar; > } > } > > public static class Container { > > private String id; > @JsonTypeInfo(use = Id.NAME, include = As.EXTERNAL_PROPERTY, > property = "id") > @JsonSubTypes({ > @Type(name = "FOO", value = Foo.class), > @Type(name = "NAR", value = Nar.class) > }) > @JsonAlias({ > "fooValue", > "narValue" > }) > private Object target; > > public String getId() { > return id; > } > > public void setId(String id) { > this.id = id; > } > > public Object getTarget() { > return target; > } > > public void setTarget(Object target) { > this.target = target; > } > } > } > > -- > 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/2e28b484-71ad-41af-be92-ffc12ed30ae2n%40googlegroups.com > <https://groups.google.com/d/msgid/jackson-user/2e28b484-71ad-41af-be92-ffc12ed30ae2n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CAGrxA27Y1sXTCsAjZR5JXihLka4K32_ZyQj%3D_fhsz7XMnN8VZQ%40mail.gmail.com.
