【问题标题】:Generated JSON Schema does not show properties of fields that are of type 'object'生成的 JSON 架构不显示“对象”类型的字段的属性
【发布时间】:2019-01-09 19:10:52
【问题描述】:

spring-data-rest 生成的 JSON 模式不包含“对象”类型的属性定义。

我正在尝试使用生成的架构来使用制服材料/AutoForm 自动创建表单。它需要这些对象属性的定义才能正确创建表单。

curl localhost:8080/api/profile/agentContactRecords -H 
"Accept:application/schema+json"

我希望上述架构请求的输出是

...
"Agent Business Lines" : {
  "title" : "Agent business lines",
  "type" : "array",
  "items" : {
    "type" : "object",
          "properties": {
            "Business Line": {
              "type": "string",
              "title": "The Business line Schema"
            },
            "Agent Name": {
              "type": "string",
              "title": "The Agent name Schema"
            }
          }
  }
},
"Agent" : {
  "title" : "Agent",
  "type" : "object",
          "properties": {
            "Agent Number": {
              "type": "string",
              "title": "The Agent number Schema"
            }
          }
},
...

但我得到以下内容

...
"Agent Business Lines" : {
  "title" : "Agent business lines",
  "readOnly" : false,
  "type" : "array",
  "items" : {
    "type" : "object"
  }
},
"Agent" : {
  "title" : "Agent",
  "readOnly" : false,
  "type" : "object"
},
...

【问题讨论】:

  • 好的。这听起来像是特定库的问题。我建议您在该库的相应问题跟踪器中记录问题。
  • 我投票结束这个问题,因为它是对特定库或实现的附加功能请求

标签: spring-data-rest jsonschema


【解决方案1】:

我的实体中的对象属性使用@ElementCollection(targetClass="className.class") 进行了注释。但是,targetClass 本身使用 @Entity 注释进行了注释。当我将目标类更改为纯 java 对象(无 @Entity 注释)时,字段开始出现在 json 模式中,其属性为 $ref,如下所示。

    "agentBusinessLines" : {
      "title" : "Agent business lines",
      "readOnly" : false,
       "type" : "array",
      "items" : {
        "$ref" : "#/definitions/agentBusinessLineNonEntityCopy"
      }
    },
  "definitions" : {
    "agentBusinessLineNonEntityCopy" : {
      "type" : "object",
  "properties" : {
    "businessLineAcronym" : {
      "title" : "Business line acronym",
      "readOnly" : false,
      "type" : "string"
    },
    "agentName" : {
      "title" : "Agent name",
      "readOnly" : false,
      "type" : "string"
    },
    "company" : {
      "title" : "Company",
      "readOnly" : false,
      "type" : "string"
    },
    "agentNumber" : {
      "title" : "Agent number",
      "readOnly" : false,
      "type" : "integer"
    }
  }
}

【讨论】:

    【解决方案2】:

    我在 Spring Darta Rest 中检查了 PersistentEntityToJsonSchemaConverter.java 的源代码,发现它不能确定数组属性中项目(对象类型)的属性。

    我找到了一种扩展此类并添加此功能的方法 扩展 RepositoryRestMvcConfiguration 类及其方法 jsonSchema 转换器。 不幸的是,没有办法扩展 JsonSchema 类,因为它在其构造函数中使用了包作用域对象(AbstractJsonSchemaProperty)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      相关资源
      最近更新 更多