【问题标题】:Getting "Cannot read property 'parameters' of null" for swagger spec为招摇规范获取“无法读取 null 的属性‘参数’”
【发布时间】:2015-12-21 19:34:53
【问题描述】:

我有以下 JSON 是我自己从代码中生成的。我基本上是在编写代码来为 UI 生成 swagger 规范。我正在写一个新的,因为我仍然使用 play 1.3 并且为此的 swagger 模块已经有一段时间没有更新了。但是,我从我的代码生成器中得到了这个 JSON,但 UI 挂在“获取资源列表:http://localhost:9100/resources.json;请稍候”。 javascript 控制台显示错误“获取”无法读取 null 的属性“参数”以获取 swagger 规范”。我不知道该怎么做,任何帮助将不胜感激。

     {
  "swagger": "2.0",
  "info": null,
  "host": null,
  "basePath": null,
  "tags": [
    {
      "name": "payments",
      "description": null,
      "externalDocs": null
    }
  ],
  "schemes": null,
  "consumes": null,
  "produces": null,
  "paths": {
    "/": {
      "get": {
        "tags": [
          "payments"
        ],
        "summary": "/payment_methods",
        "description": "",
        "operationId": "paymentMethods",
        "schemes": null,
        "consumes": null,
        "produces": [
          "application/json"
        ],
        "parameters": [],
        "responses": {
          "default": {
            "description": "successful operation",
            "schema": null,
            "examples": null,
            "headers": null
          }
        },
        "security": null,
        "externalDocs": null,
        "deprecated": null
      },
      "head": null,
      "post": {
        "tags": [
          "payments"
        ],
        "summary": null,
        "description": null,
        "operationId": "addPaypalAccount",
        "schemes": null,
        "consumes": null,
        "produces": null,
        "parameters": [],
        "responses": {
          "default": {
            "description": "successful operation",
            "schema": null,
            "examples": null,
            "headers": null
          }
        },
        "security": null,
        "externalDocs": null,
        "deprecated": null
      },
      "put": null,
      "delete": {
        "tags": [
          "payments"
        ],
        "summary": null,
        "description": null,
        "operationId": "deletePaypalAccount",
        "schemes": null,
        "consumes": null,
        "produces": null,
        "parameters": [],
        "responses": {
          "default": {
            "description": "successful operation",
            "schema": null,
            "examples": null,
            "headers": null
          }
        },
        "security": null,
        "externalDocs": null,
        "deprecated": null
      },
      "options": null,
      "patch": null,
      "parameters": null
    }
  },
  "securityDefinitions": null,
  "definitions": null,
  "parameters": null,
  "responses": null,
  "externalDocs": null,
  "securityRequirement": null
}

【问题讨论】:

  • 您是否需要转义、使用引号或以其他方式更改您拥有路径的格式:{ /: { ?我的 Json 编辑器强迫我将其更改为“/”。
  • 哦,对不起。由于我使用了 JSON 视图扩展,引号被删除。引号在那里并且 JSON 是有效的。我现在用实际的 JSON 修改了这个问题。改变格式是什么意思?

标签: swagger swagger-ui swagger-2.0 swagger-editor


【解决方案1】:

问题在于null 值。它们需要被禁用——null 不同于“不存在”。也就是说,我相信您需要将映射器配置为不写入空值。

【讨论】:

  • 是的,我认为你是对的。我的代码现在支持 jax-rs 样式注释(因为 swagger 注释不会让我标记独立路径)并且在添加这些的过程中,我确实将映射器配置为仅写入非空值。现在似乎可以工作了。
【解决方案2】:

我遇到了这个问题,结果证明它与我的 .NET Web api 应用程序中的 JSON 格式化程序有关。我可以通过将此行添加到 BootStrap 类的 Configure 方法来解决它。

config.Formatters.Clear();

【讨论】:

  • 我使用 JsonContentNegotiator 从这里强制 JSON 响应:stackoverflow.com/a/18145616/647728 并需要将 jsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; 添加到 WebApiConfig 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多