InvalidSchemaException using JsonSubTypes, not picking up what I defined in the base interface

≡放荡痞女 提交于 2019-12-11 15:18:21

问题


The JsonSubTypes that are being picked up are not the ones that I was expecting it to pick up.

/* ANIMAL: BASE */
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
  @Type(value = CatTopic.class, name = "CAT"),
  @Type(value = DogTopic.class, name = "DOG"),
  @Type(value = FishTopic.class, name = "FISH"),
  @Type(value = LamaTopic.class, name = "LAMA")
})
 public interface AnimalTopicDeclInterface {}

and these classes

    public abstract class AnimalTopic implements AnimalTopicDeclInterface {}

    public class CatTopic extends AnimalTopic implements CatTopicDeclInterface {}

    public class DogTopic extends AnimalTopic implements DogTopicDeclInterface {}

    public class FishTopic extends AnimalTopic implements FishTopicDeclInterface {}

    public class LamaTopic extends AnimalTopic implements LamaTopicDeclInterface {}

and this Interface, (listed one but each class above has one)

/* ANIMAL: CAT */
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
  @Type(value = DogTopic.class, name = "DOG"),
})
 public interface CatTopicDeclInterface {}

The problem that I discovered is that when I attempt to validate in a test the created Json Schema it blows up saying "InvalidSchemaException". It lists out all of the other classes as

[{
    "leve" : "error",
    "message" : "value has incorrect type (found null, expected one of [string])",
    "domain" : "syntax",
    "schema" : {
        "loadingURI" : "#",
        "pointer" : "/definitions/DogTopic"
    }
    "keyword" : "title",
    "found" : "null",
    "expected" : [ "string" ]
}] (also outputs three additional entries for DOG, FISH and LAMA)

I've been debugging this for 3 days now and am not sure how I can get the validation to see what is defined in the AnimalTopicDeclInterface which is off of the abstract AnimalTopic that CatTopic is extending.

What is happening is that when doing the validation it is only looking at what CatTopic is Implementing which only contains of of the 4 classes.

Note that even if I include in the implement line for CatTopic all of the other interfaces (excluding the base one) it will only ever see what is defined in the 1st interface defined in the implements section which in this case is the CatTopicDeclInterface.

What magical annotation am I to use so that the validation will see from CatTopic to the extended AnimalTopic to the AnimalTopicDeclInterface?

Related question I had on this: Where should the JsonSubTypes be read from?


回答1:


Finally found the way to handle the main issue in my related post. public abstract @interface JsonTypeInfo

What needs to be done is for each of the Classes defined in the JsonSubTypes you must also specify on that Class @JsonTypeName("ClassName") e.g.

@JsonTypeName("CAT")
public class CatTopic extends A implements AlpahInterface, BetaInterface, CharlieInterface {}


来源:https://stackoverflow.com/questions/51544714/invalidschemaexception-using-jsonsubtypes-not-picking-up-what-i-defined-in-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!