dna*_*ult 7 java json jsonschema jackson
使用Jackson的JSON模式模块时,不管是否遇到某个模型类,我都希望停止完整的图形,并使用类名为另一个模式插入$ ref.你能引导我到jackson-module-jsonSchema源中的正确位置开始修补吗?
这里有一些代码来说明这个问题:
public static class Zoo {
public String name;
public List<Animal> animals;
}
public static class Animal {
public String species;
}
public static void main(String[] args) throws Exception {
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
ObjectMapper mapper = objectMapperFactory.getMapper();
mapper.acceptJsonFormatVisitor(mapper.constructType(Zoo.class), visitor);
JsonSchema jsonSchema = visitor.finalSchema();
System.out.println(mapper.writeValueAsString(jsonSchema));
}
Run Code Online (Sandbox Code Playgroud)
输出:
{
"type" : "object",
"properties" : {
"animals" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : { <---- Animal schema is inlined :-(
"species" : {
"type" : "string"
}
}
}
},
"name" : {
"type" : "string"
}
}
}
Run Code Online (Sandbox Code Playgroud)
期望的输出:
{
"type" : "object",
"properties" : {
"animals" : {
"type" : "array",
"items" : {
"$ref" : "#Animal" <---- Reference to another schema :-)
}
},
"name" : {
"type" : "string"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个解决该问题的自定义 SchemaFactoryWrapper 。不能保证,但它似乎与 Jackson 2.4.3 配合得很好。
更新:从 Jackson 2.5 开始,事情变得容易多了。现在您可以指定自定义 VisitorContext。