问题
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))`
来源:https://stackoverflow.com/questions/61140930/how-to-exclude-primitive-types-from-required-list-of-json-schema-version4