首页 > 解决方案 > Everit 架构构建器将未设置的属性包括为 null

问题描述

尝试构建和使用 Schema 对象:

        Schema rootSchema = ObjectSchema.builder()
                .additionalProperties(true)
                .build();

在我尝试将其序列化为字符串然后使用 SchemaLoader 重新加载之前,它似乎工作正常:

        String json = objectMapper.writeValueAsString(rootSchema);
        JSONObject schemaObject = new JSONObject(json);
        Schema schema = SchemaLoader.load(schemaObject);

问题是未设置的关键字被序列化为null:

{
  "title" : null,
  "description" : null,
  "id" : null,
  "propertySchemas" : { },
  "schemaOfAdditionalProperties" : null,
  "requiredProperties" : [ ],
  "minProperties" : null,
  "maxProperties" : null,
  "propertyDependencies" : { },
  "schemaDependencies" : { },
  "patternProperties" : { }
}

SchemaLoader 将这些报告为错误,例如“java.lang.RuntimeException: org.json.JSONException: JSONObject["id"] not a string”。是否可以跳过这些关键字?

另一个问题:序列化版本不包含附加属性关键字。

此外,是否可以包含 JSON Schema 规范中未指定的其他关键字?AFAIIK 规范本身并不禁止此类关键字,而 AJV 等一些库实际上使用它们?

标签: javajsonjsonschemaeverit

解决方案


推荐阅读