How to exclude Primitive types from required list of Json Schema (version4)

ε祈祈猫儿з 提交于 2020-04-18 03:54:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!