简体   繁体   中英

how should i get the json references inline rather than getting it references - should NOT have additional properties additionalProperty: definitions

I am new to JsonSchema, I am trying to generate swagger(3.0) JSON. I am using NJsonSchema. It has successfully generated the JSON schema for models. The only problem is that the JSON has references to the complex type. I tried the generated JSON in http://editor.swagger.io/ and the UI is generated fine, but I have a lot of structural error.

Structural error at paths./xyz/asd/abc/.schema should NOT have additional properties additionalProperty: definitions

following is the sample JSON which can be tested on the above link.

{
  "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": {  }
}

I don't think NJsonSchema gives a way to get rid of schema reference handling. So JSON generated always has these references.

Is there any way to process this schema and get the references inline? I also looked for NewtonsoftJson's IReferenceResolver examples on the internet but could not get a clear picture of how can I use it.

Thanks in Advance.

I have corrected you JSON file with the correct way to use schema and $ref attribute:

{
  "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"
          }
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM