deserializing type hierarchy from CosmosDB in java

Paul Gardiner 0 Reputation points
2023-05-09T17:52:47.04+00:00

I have a pojo that stores a list of other pojos, including subclasses. Everything serializes and saves properly, but when deserializing, all of the subclasses end up restored as the parent class. For example, I have the container object ( all objects have @Data annotation):

 public class Conf {
    private List<Foo> foos;
 }

 public class Foo {
    private String id;

 }

 public class Bar extends Foo {
      private String barField;`
 }

When I save "Conf" to cosmosdb, containing a list of Foo and Bar objects, everything is written out fine, and all Bar json objects have both id and barField fields. When I read from the database and deserialize back to java, "foos" contains only "Foo" objects.

I have tried various json annotations, but it doesn't change how the objects are written to Cosmos. Is there something I can do to get them to deserialize properly?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,455 questions
{count} votes