提问者:小点点

我应该如何内联获取 JSON 引用而不是获取它引用 - 不应该有其他属性 附加属性:定义


我是JsonSchema的新手,我正在尝试生成swagger(3.0) JSON。我使用的是NJsonSchema。它已经成功地为模型生成了JSON模式。唯一的问题是JSON引用了复杂类型。我在http://editor.swagger.io/测试了生成的JSON,UI生成得很好,但是我有很多结构错误。

paths./xyz/asd/abc/.schema 上的结构错误不应具有其他属性 附加属性:定义

下面是可以在上面的链接上测试的示例JSON。

{
  "openapi": "3.0.1",
  "info": {
    "title": "SomeTitle",
    "description": "someDescription",
    "version": "v1"
  },
  "paths": {
    "/Device": {
      "get": {
        "summary": "someSummary",
        "tags": [
          "Device"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "description": "deviceId",
            "required": true,
            "schema": {
              "type": "string",
              "nullable": false
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success.",
            "content": {
              "application/json": {
                "schema": {
                  "title": "SomeTitleAgain",
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "id"
                  ],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "udiCarrier": {
                      "$ref": "#/paths/~1Device/get/responses/200/content/application~1json/schema/definitions/ListOfUdiCarrierComponent"
                    }
                  },
                  "definitions": {
                    "ListOfUdiCarrierComponent": {
                      "type": "array",
                      "items": {
                        "$ref": "#/paths/~1Device/get/responses/200/content/application~1json/schema/definitions/UdiCarrierComponent"
                      }
                    },
                    "UdiCarrierComponent": {
                      "type": "object",
                      "additionalProperties": false,
                      "properties": {
                        "carrierHRF": {
                          "type": "string"
                        }}}}}}}}}}}},
  "components": {  }
}

我不认为NJsonSchema提供了摆脱模式引用处理的方法。所以生成的JSON总是有这些引用。

是否有任何方法可以处理此模式并内联获取引用?我还在网上寻找了NewtonsoftJson的IReferenceResolver示例,但无法清楚地了解如何使用它。

提前谢谢。


共1个答案

匿名用户

我已经用使用模式和$ref属性的正确方法更正了您的JSON文件:

{
  "openapi": "3.0.1",
  "info": {
    "title": "SomeTitle",
    "description": "someDescription",
    "version": "v1"
  },
  "paths": {
    "/Device": {
      "get": {
        "summary": "someSummary",
        "tags": [
          "Device"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "description": "deviceId",
            "required": true,
            "schema": {
              "type": "string",
              "nullable": false
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ListOfUdiCarrierComponent"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ListOfUdiCarrierComponent": {
        "title": "SomeTitleAgain",
        "type": "object",
        "additionalProperties": false,
        "required": [
          "id"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "udiCarrier": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/UdiCarrierComponent"
            }
          }
        }
      },
      "UdiCarrierComponent": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "carrierHRF": {
            "type": "string"
          }
        }
      }
    }
  }
}