英文:
Is there way to get required field array in draft-07 json schema through java code
问题
以下是要翻译的内容:
我想要生成带有必需字段的 draft-04 或 draft-07 的 json schema。
我对 JSON schema 还很新,所以能够使用 victools 生成 draft-07 schema:
共享相同的代码:
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON);
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema("MyClassName".class);
System.out.println(jsonSchema.toString());
我得到的输出是:
类似于 draft-07 的 Json schema 如下:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ActiveOrHistoricCurrencyAndAmount": {
"type": "object",
"properties": {
"ccy": { "type": "string" },
"value": { "type": "number" }
}
}
}
}
我想要的是:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ActiveOrHistoricCurrencyAndAmount": {
"type": "object",
"properties": {
"ccy": { "type": "string" },
"value": { "type": "number" }
},
"required": ["ccy", "value"]
}
}
}
我还想为必需字段生成必需数组,那么如何使用 Java 生成这个呢?
英文:
I want to generate json schema of draft-04 or draft-07 with required array for mandatory fields?
I am new to JSON schema, so able to generate draft-07 schema with victools:
Sharing code for same:
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON);
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema("MyClassName".class);
System.out.println(jsonSchema.toString());
The o/P i got is:
Json schema for draft-07 like this:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ActiveOrHistoricCurrencyAndAmount": {
"type": "object",
"properties": {
"ccy": { "type": "string" },
"value": { "type": "number" }
}
}
}
}
what i wanted is:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ActiveOrHistoricCurrencyAndAmount": {
"type": "object",
"properties": {
"ccy": { "type":"string" },
"value": { "type":"number" }
},
"required": ["ccy", "value"]
}
}
}
I wanted required array for mandatory fields also , so how to generate this using java?
答案1
得分: 1
victools生成器 4.17.0 目前不直接支持Jackson注解@JsonProperty(required=true)
的required
属性。这将在下一个版本中加入,并可能通过将选项JacksonOption.RESPECT_JSONPROPERTY_ORDER
设置为true
来启用。
详见victools更新日志
在那之前,您可以自定义SchemaGeneratorConfigBuilder来支持这个属性,像这样:
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON);
configBuilder.forFields()
.withRequiredCheck(field -> {
JsonProperty jsonProperty = field.getAnnotationConsideringFieldAndGetter(JsonProperty.class) ;
if ( jsonProperty == null )
return false; // 没有 @JsonProperty ? => 字段不是必需的。
else
return jsonProperty.required(); // 让我们尊重“required”属性
});
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema("MyClassName".class);
(免责声明:我最近为这个功能提交了PR)
英文:
victools generator 4.17.0 currently does not support the required
attribute of the Jackson annotation @JsonProperty(required=true)
out of the box . It is scheduled for the next version, and will probably be enabled with the option JacksonOption.RESPECT_JSONPROPERTY_ORDER
set to true
.
see victools changelog
Until then, you could customize the SchemaGeneratorConfigBuilder to support this attribute like this :
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON);
configBuilder.forFields()
.withRequiredCheck(field -> {
JsonProperty jsonProperty = field.getAnnotationConsideringFieldAndGetter(JsonProperty.class) ;
if ( jsonProperty == null )
return false; // No @JsonProperty ? => field not required.
else
return jsonProperty.required(); // let's respect what the 'required' says
});
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema("MyClassName".class);
(disclaimer: I recently submitted the PR for this feature)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论