有没有办法通过Java代码在draft-07的JSON模式中获取所需的字段数组

huangapple go评论88阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2020年10月7日 23:19:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64247177.html
匿名

发表评论

匿名网友

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

确定