英文:
How to exclude Primitive types from required list of Json Schema (version4)
问题
我有以下提到的类:
public class Test1 {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
生成以下模式的 JsonSchema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Test 1",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"required": ["age"]
}
如您所见,默认情况下,它将基本字段(int)放在模式的必需列表中。
我正在使用以下库进行 Json Schema 生成:https://github.com/mbknor/mbknor-jackson-jsonSchema。
即使我为基本字段指定了默认值,它仍然保持在必需字段中。请问您如何将基本字段从必需列表中移除。
我正在使用以下代码生成 json schema:
ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper);
JsonNode jsonSchema = jsonSchemaGenerator.generateJsonSchema(Test1.class);
try {
System.out.print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我不认为这是可能的。我查看了库代码,他们对必需进行了以下检查:
val requiredProperty: Boolean = if (propertyType.getRaw`Class.isPrimitive || jsonPropertyRequired || validationAnnotationRequired(prop))`
英文:
I have following classes as mentioned below:
public class Test1 {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age= age;
}
}
JsonSchema generating the following schema:
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "Test 1",
"type" : "object",
"additionalProperties" : false,
"properties" : {
"name" : {
"type" : "string"
},
"age" : {
"type" : "integer"
}
},
"required" : [ "age" ]
}
As you can see by default its putting primitive fields (int) in the required list of the schema.
I am using following library for Json Schema generation: https://github.com/mbknor/mbknor-jackson-jsonSchema.
Even if I specify default value for the primitive field, it stays in required field. Could you please tell how could I remove primitive field from required list.
I am using following code to generate json schema:
ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper);
JsonNode jsonSchema = jsonSchemaGenerator.generateJsonSchema(Test1.class);
try {
System.out.print(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I don't think its possible. I went through library code, they are using following checks for required:
val requiredProperty: Boolean = if (propertyType.getRaw`Class.isPrimitive || jsonPropertyRequired || validationAnnotationRequired(prop))`
专注分享java语言的经验与见解,让所有开发者获益!
评论