当前位置: 首页 > 知识库问答 >
问题:

Rest保证异常-无效的JSON架构,不能继续

钱经赋
2023-03-14

我正在使用TestNG和Rest Assured。我们有json模式文件。在Json模式中,它使用$ref属性引用其他模式。

当我对Hamcrest断言使用matchesJsonSchemaInClasspath时,它会失败并出现验证错误。错误基本上是Validator无法读取嵌套的JSON模式。有人能帮我解决这个问题吗?

附加了错误和Json模式。

错误:

com.github.fge.jsonschema.core.exceptions.InvalidSchemaException: 
fatal: invalid JSON Schema, cannot continue
Syntax errors:
[ {
  "level" : "warning",
  "schema" : {
    "loadingURI" : "file:/C:/GitWorkspace/OM/service_OM/functionalTest/target/test-classes/specs/specification/v1/schema/response/response.json#",
    "pointer" : ""
  },
  "domain" : "syntax",
  "message" : "the following keywords are unknown and will be ignored: [javaType, name]",
  "ignored" : [ "javaType", "name" ]
}, {
  "level" : "error",
  "message" : "value has incorrect type (found boolean, expected one of [array])",
  "domain" : "syntax",
  "schema" : {
    "loadingURI" : "file:/C:/GitWorkspace/OM/service_OM/functionalTest/target/test-classes/specs/specification/v1/schema/response/service_header.json#",
    "pointer" : ""
  },
  "keyword" : "required",
  "found" : "boolean",
  "expected" : [ "array" ]
}, {
  "level" : "error",
  "message" : "URI \"./service_header.json\" is not normalized",
  "domain" : "syntax",
  "schema" : {
    "loadingURI" : "file:/C:/GitWorkspace/OM/service_OM/functionalTest/target/test-classes/specs/specification/v1/schema/response/service_header.json#",
    "pointer" : "/properties/service_header"
  },
  "keyword" : "$ref",
  "value" : "./service_header.json"
}, {
  "level" : "error",
  "message" : "value has incorrect type (found boolean, expected one of [array])",
  "domain" : "syntax",
  "schema" : {
    "loadingURI" : "file:/C:/GitWorkspace/OM/service_OM/functionalTest/target/test-classes/specs/specification/v1/schema/response/service_header.json#",
    "pointer" : "/properties/service_header"
  },
  "keyword" : "required",
  "found" : "boolean",
  "expected" : [ "array" ]
}, {
  "level" : "error",
  "message" : "value has incorrect type (found boolean, expected one of [array])",
  "domain" : "syntax",
  "schema" : {
    "loadingURI" : "file:/C:/GitWorkspace/OM/service_OM/functionalTest/target/test-classes/specs/specification/v1/schema/response/service_item.json#",
    "pointer" : "/properties/items"
  },
  "keyword" : "required",
  "found" : "boolean",
  "expected" : [ "array" ]
}, {
  "level" : "error",
  "message" : "URI \"./setvice_item.json\" is not normalized",
  "domain" : "syntax",
  "schema" : {
    "loadingURI" : "file:/C:/GitWorkspace/OM/service_OM/functionalTest/target/test-classes/specs/specification/v1/schema/response/service_item.json#",
    "pointer" : "/properties/items/items"
  },
  "keyword" : "$ref",
  "value" : "./service_item.json"
} ]
level: "fatal"

JSON模式:

主要的json

{ 
   "type":"object",
   "$schema":"http://json-schema.org/draft-04/hyper-schema",
   "name": "Response",
   "title": "Response",
   "javaType": "com.ruthresh.Response",
   "description": "Enables you to make a POST response.",
   "required":true,
   "properties":{
       "service_header":{
           "type":"object",
           "description": "The service header for a response.",
           "required":true,
           "$ref":"./service_header.json"
       },
       "items":{
           "type":"array",
           "description": "An array of individual items.",
           "required":true,
           "maxLength":5000,
           "minLength":1,
           "items":{
                "$ref":"./service_item.json"
            }
        }
    }
}

服务单元标题。json

{
    "type":"object",
    "$schema":"http://json-schema.org/draft-04/hyper-schema",
    "name":"service_header",
    "title":"Service response Header",
    "description":"The service header for a response.",
    "id":"service_header:v1",
    "javaType": "com.ruthresh.Header",
    "required":true,
    "properties":{
        "email_subject":{
            "type":"string",
            "description":"Email subject.",
            "maxLength":255
        },
        "email_message":{
            "type":"string",
            "description":"Email Message.",
            "maxLength":4000
        }
    }
}

serivice_item.json

{
    "type":"object",
    "$schema":"http://json-schema.org/draft-04/hyper-schema",
    "name":"serivice_item",
    "title":"Service response Header",
    "description":"The service header for a response.",
    "id":"serivice_item:v1",
    "javaType": "com.ruthresh.Item",
    "required":true,
    "properties":{
        "item_subject":{
            "type":"string",
            "description":"Item subject.",
            "maxLength":255
        },
        "item_message":{
            "type":"string",
            "description":"Item Message.",
            "maxLength":4000
        }
    }
}

共有1个答案

上官鸿晖
2023-03-14

正如错误消息所说,“required”应该是必需属性的数组,而不是布尔值。

 类似资料:
  • 我正在使用rest-assured并使用它验证JSON模式。我使用jsonschema生成了模式。net(草稿7),并将生成的模式保存在文件中,并根据GETAPI的响应匹配生成的模式。但抛出错误的代码输出如下: 我的通用模式是模式链接 api API repsonse的响应

  • 我的任务是将应用程序的当前体系结构(使用Cupboard)迁移到Room。在第一项工作中,我遇到了一些问题,即迁移数据库对象,目前用Java编写(Cupboard仅支持Java),使它们与Room一起工作。这是一个例子: 这是它在数据库中的样子: 我对此实体所做的是在Kotlin中创建一个用于Room的类,它如下所示: 通过比较这两个字段,我可以看到一些字段应该被标记为,但它们似乎被标记为,尽管K

  • 我正在学习“放心框架”。 我使用http://ziptasticapi.com免费的API为我的演习。 当我打电话: 我得到以下字符串: {“国家”:“美国”、“州”:“正义与发展党”、“城市”:“阿达克”} as响应字符串值。 当我尝试时: 我突然想到: JAVAlang.IllegalStateException:无法分析对象,因为响应中未指定支持的内容类型。内容类型为“文本/html”;字符

  • 我有一个如下的Json模式,当我在网上验证它时,比如http://jsonschemalint.com/draft4/#,它说它是有效的。我把“类型”改成了“对象”、“数组”、“字符串”,其余的结构保持不变。它仍然说是有效的模式。我的理解是当类型=对象时应该有“属性”,当类型=数组时应该有“项目”

  • 我用的是Spring2.2。我最近升级到了Spring4.1.4。 我的项目使用以下应用程序上下文:

  • 问题内容: 背景:我正在编写一个简单的UDP应用程序对我管理的beta服务器执行ping操作,以便告诉我它仍然可以正常运行(我无法对那些想知道的服务器启用ping操作)。我打算在手机上运行此命令,以在服务器不再响应时警告我。 我试图像这样使用看似简单的java.net.DatagramSocket: 我还要说一句,我已通过android清单启用了Internet权限,如果删除了use子句,则会出现