如何在 Json Schema(版本4)的必需列表中排除原始类型。

huangapple 未分类评论50阅读模式
英文:

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))`

huangapple
  • 本文由 发表于 2020年4月10日 21:07:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61140930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定