node-red / node-red-node-swagger

A set of tools for generating Swagger api documentation based on the HTTP nodes deployed in a flow
Apache License 2.0
62 stars 47 forks source link

Omit otherwise empty fields #4

Closed knolleary closed 9 years ago

knolleary commented 9 years ago

At the bottom of this issues is a flow that creates a tiny subset of the standard pet store example.

Looking at the generated swagger, we are getting a lot of empty schema/properties entries. We should omit these if they are empty (as they are not required by the spec)

"responses": {
      "405": {
        "description": "Invalid Input",
        "schema": {
          "properties": {}
        }
      }
    }

[{"id":"6e85ee6e.917a1","type":"swagger-doc","summary":"Returns a single pet","description":"","tags":"","consumes":"","produces":"application/xml,application/json","parameters":[{"name":"petId","in":"header","description":"ID of pet to return","required":true,"type":"string","format":"long"}],"responses":{"200":{"schema":{"properties":{}},"code":"200"},"400":{"description":"Invalid ID supplied","schema":{"properties":{}},"code":"400"},"404":{"description":"Pet not found","schema":{"properties":{}},"code":"404"}},"deprecated":true},{"id":"ac5b4676.53a4b8","type":"swagger-doc","summary":"Add a new pet to the store","description":"","tags":"","consumes":"","produces":"","parameters":[{"name":"body","in":"body","description":"Pet object that needs to be added to the store","required":true,"schema":{"properties":{}}}],"responses":{"405":{"description":"Invalid Input","schema":{"properties":{}},"code":"405"}},"deprecated":true},{"id":"ea6bcf4e.15943","type":"http in","name":"","url":"/pet","method":"post","swaggerDoc":"ac5b4676.53a4b8","x":599,"y":413,"z":"396c2376.c693dc","wires":[[]]},{"id":"ccd7bb57.332848","type":"http in","name":"","url":"/pet/:petId","method":"get","swaggerDoc":"6e85ee6e.917a1","x":602,"y":353,"z":"396c2376.c693dc","wires":[[]]}]

codymwalker commented 9 years ago

For responses, it is valid to leave empty schemaobjects off of the swaggerdoc. However, for body parameters, like the one in post/pet, schema is a required property. It does look like the properties attribute can be removed in the object there as well though. After removing these, I see swagger that looks like this:

{
  "swagger": "2.0",
  "info": {
    "title": "My Node-RED API",
    "version": "0.0.1"
  },
  "basePath": "/",
  "paths": {
    "/pet": {
      "post": {
        "summary": "Add a new pet to the store",
        "deprecated": true,
        "parameters": [
          {
            "name": "body",
            "in": "body",
            "description": "Pet object that needs to be added to the store",
            "required": true,
            "schema": {}
          }
        ],
        "responses": {
          "405": {
            "description": "Invalid Input"
          }
        }
      }
    },
    "/pet/:petId": {
      "get": {
        "summary": "Returns a single pet",
        "produces": [
          "application/xml",
          "application/json"
        ],
        "deprecated": true,
        "parameters": [
          {
            "name": "petId",
            "in": "header",
            "description": "ID of pet to return",
            "required": true,
            "type": "string",
            "format": "long"
          }
        ],
        "responses": {
          "200": {},
          "400": {
            "description": "Invalid ID supplied"
          },
          "404": {
            "description": "Pet not found"
          }
        }
      }
    }
  }
}

Keep in mind that the actual generation of the param and response object comes at deploy time, rather than when the swagger api is called. So even though the flows didn't change, the attached swagger will when you open the editor and click save.

[{"id":"6e85ee6e.917a1","type":"swagger-doc","summary":"Returns a single pet","description":"","tags":"","consumes":"","produces":"application/xml,application/json","parameters":[{"name":"petId","in":"header","description":"ID of pet to return","required":true,"type":"string","format":"long"}],"responses":{"200":{},"400":{"description":"Invalid ID supplied"},"404":{"description":"Pet not found"}},"deprecated":true},{"id":"ac5b4676.53a4b8","type":"swagger-doc","summary":"Add a new pet to the store","description":"","tags":"","consumes":"","produces":"","parameters":[{"name":"body","in":"body","description":"Pet object that needs to be added to the store","required":true,"schema":{}}],"responses":{"405":{"description":"Invalid Input"}},"deprecated":true},{"id":"ed54f1aa.12ab1","type":"http in","name":"","url":"/pet","method":"post","swaggerDoc":"ac5b4676.53a4b8","x":747,"y":200,"z":"ef8b8f86.10747","wires":[[]]},{"id":"1353c360.ecac3d","type":"http in","name":"","url":"/pet/:petId","method":"get","swaggerDoc":"6e85ee6e.917a1","x":750,"y":140,"z":"ef8b8f86.10747","wires":[[]]}]