[英]How can I make fasterXml ObjectMapper put a format property into schema for fields of the type long
How can I make an ObjectMapper
set the format
field for some properties in the generated schema of a class?如何让ObjectMapper
为 class 的生成架构中的某些属性设置format
字段?
I'm trying to generate a schema for a java class using a FasterXml
library:我正在尝试使用FasterXml
库为 java class 生成架构:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jsonSchema</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.1</version>
<scope>compile</scope>
</dependency>
Test:测试:
@SneakyThrows
@Test
public void test() {
ObjectMapper objectMapper = new ObjectMapper();
SchemaFactoryWrapper schemaVisitor = new SchemaFactoryWrapper();
objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(MyClass.class), schemaVisitor);
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schemaVisitor.finalSchema()));
}
@Data
public static class MyClass {
private int myInt;
private long myLong;
}
The result is like this:结果是这样的:
{
"type":"object",
"id":"urn:jsonschema:........:MyClass",
"properties":{
"myInt":{
"type":"integer"
},
"myLong":{
"type":"integer"
}
}
}
Both types are good, but I'd like to have "format": "int64"
set for the property myLong
.两种类型都很好,但我想为属性myLong
设置"format": "int64"
。
Json Schema does not introduce int64
type. Json Schema没有引入int64
类型。 There are only number
and integer
types.只有number
和integer
类型。 Take a look at the enum JsonFormatTypes .查看枚举JsonFormatTypes 。 JsonSchema
module uses it. JsonSchema
模块使用它。 To extend it you would have to extend a lot of classes and somehow trick internal mechanism to replace integer
with int64
which at the end would generate misleading schema which would be understandable only for you.要扩展它,您将不得不扩展很多类,并以某种方式欺骗内部机制以将integer
替换为int64
,这最终会产生误导性的模式,只有您才能理解。 All other libs and tools do not recognise it.所有其他库和工具都无法识别它。 I propose to use com.fasterxml.jackson.annotation.JsonPropertyDescription
annotation for long
property:我建议对long
属性使用com.fasterxml.jackson.annotation.JsonPropertyDescription
注释:
class MyClass {
private int myInt;
@JsonPropertyDescription("int64")
private long myLong;
}
Which will generate something like this:这将产生这样的东西:
{
"type" : "object",
"id" : "urn:jsonschema:com:example:MyClass",
"properties" : {
"myInt" : {
"type" : "integer"
},
"myLong" : {
"type" : "integer",
"description" : "int64"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.