Victools JSON模式生成器为同一类创建两个定义
使用Victools的JSON模式生成器(这是基于Java类生成JSON模式),如果我将两个类与常见的Supertype相关联,并且使用 @jsontypename
。
请考虑以下代码:
@JsonTypeName("Root")
public class Root {
private String rootName;
...
private List<SuperClass1> superclass1 = new ArrayList<SuperClass1>();
...
}
@JsonTypeName("SuperClass1")
@JsonTypeInfo( use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = Sub1.class, name = "Sub1"),
@JsonSubTypes.Type(value = Sub2.class, name = "Sub2")})
public abstract class SuperClass1 {
String name;
int someThingElse;
Root root;
...
}
@JsonTypeName("Sub1")
public class Sub1 extends SuperClass1 {
String sub1;
...
Sub2 sub2;
...
}
@JsonTypeName("Sub2")
public class Sub2 extends SuperClass1{
String sub2;
...
}
生成以下JSON架构:
{
"$schema" : "http://json-schema.org/draft-07/schema#",
"definitions" : {
"Sub1" : {
"type" : "object",
"properties" : {
"root" : {
"$ref" : "#"
},
"sub1" : {
"type" : "string"
},
"sub2" : {
"$ref" : "#/definitions/Sub2-2"
}
}
},
"Sub2-1" : {
"type" : "object",
"properties" : {
"root" : {
"$ref" : "#"
},
"sub2" : {
"type" : "string"
}
}
},
"Sub2-2" : {
"allOf" : [ {
"$ref" : "#/definitions/Sub2-1"
}, {
"type" : "object",
"properties" : {
"type" : {
"const" : "json_test.Sub2"
}
},
"required" : [ "type" ]
} ]
}
},
"type" : "object",
"properties" : {
"rootName" : {
"type" : "string"
},
"superclass1" : {
"type" : "array",
"items" : {
"anyOf" : [ {
"allOf" : [ {
"$ref" : "#/definitions/Sub1"
}, {
"type" : "object",
"properties" : {
"type" : {
"const" : "json_test.Sub1"
}
},
"required" : [ "type" ]
} ]
}, {
"$ref" : "#/definitions/Sub2-2"
} ]
}
}
}
}
如果 sub1
类的属性引用 sub2
类,以及 @jsontypeinpeinfo(use = jsontypeinfo.id.class,include = jsontypeinfo.as.property,属性=“ type”)
)。
有人可以向我解释为什么它会生成 sub2-1
和 sub2-2
,而不仅仅是 sub2
在“定义”部分中?
谢谢
- Jaap
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
区别在于架构生成器的内部工作:
sub2-1
表示“原始”类型,如果您将sub2-2
代表sub2-1在杰克逊通常添加的附加“类型”属性中
,如果
sub2-1
仅引用一次,则应将其列入第二个架构,因此允许将第二个架构标记为sub2
。至少对于sub1
,似乎按预期工作。我建议您在
victools
github存储库上提供一个完整的可重复示例,作为“问题”,我可以看看这是一个很容易解决的错误还是至少由您的特定设置解释。注意:我是该库的维护者。
The difference is due to the inner workings of the schema generator:
Sub2-1
represents the “raw” type if you willSub2-2
representsSub2-1
with the additional “type” property added by JacksonNormally, if
Sub2-1
is only referenced once, it should be inlined into the second schema and therefore allow that second schema to be labelled as justSub2
. At least forSub1
that appears to be working as intended.I suggest you provide a full reproducible example as “Issue” on the
victools
GitHub repository and I can take a look, whether this is a bug that can be easily fixed or at least explained by your specific setup.Note: I’m the maintainer of that library.