swagger-express-middleware icon indicating copy to clipboard operation
swagger-express-middleware copied to clipboard

Example fields being used as default values

Open joetennant opened this issue 7 years ago • 6 comments

When using a json based swagger document, example values are being used a default values when default is not defined. The expectation is that example values would be ignored.

For example:

"paths": {
  "/companies": {
      "post": {
        "description": "Create a Company",
        "parameters": [
          {
            "name": "company",
            "in": "body",
            "description": "A Company",
            "required": true,
            "schema": {
              "$ref": "#/definitions/company"
            }
          }
        ],
        "responses": {
          "201": {
            "description": "Created",
            "schema": {
              "$ref": "#/definitions/company"
            }
          },
          "406": {
            "description": "Not Acceptable"
          }
        }
      }
    }
},
"definitions": {
    "company": {
      "type": "object",
      "properties": {
        "id": {
          "description": "unique identifier of a company",
          "example": "01234567-89ab-cdef-0123-456789abcdef",
          "format": "uuid",
          "maxLength": 37,
          "readOnly": true,
          "type": "string"
        },
        "name": {
          "description": "name of company",
          "example": "acme",
          "maxLength": 255,
          "readOnly": false,
          "type": "string"
        },
        "code": {
          "description": "unique code of company",
          "maxLength": 255,
          "type": "string"
        },
        "created_at": {
          "description": "when company was created",
          "example": "2001-01-01T12:00:00Z",
          "format": "date-time",
          "readOnly": true,
          "type": "string"
        },
        "updated_at": {
          "description": "when company was updated",
          "example": "2001-01-01T12:00:00Z",
          "format": "date-time",
          "readOnly": true,
          "type": "string"
        }
      },
      "required": [
        "name",
        "code"
      ]
    }
}

When sending a POST request to the /companies endpoint where the code value is defined, but the name field is empty. The result is the value acme being used as if it were a default value.

joetennant avatar May 22 '17 20:05 joetennant

Hi. This behavior is by design, as documented here. The mock middleware will use the default if one exists, or the example otherwise.

JamesMessinger avatar May 23 '17 14:05 JamesMessinger

Thanks for the reply. We are not using the mock() middleware, however. Would you expect this from the validateRequest() or the parseRequest() middleware?

joetennant avatar May 23 '17 17:05 joetennant

oh... now that's not by design. It definitely shouldn't be doing that.

JamesMessinger avatar May 23 '17 17:05 JamesMessinger

:)

Thanks

joetennant avatar May 23 '17 17:05 joetennant

This is still an issue, and a very bad bug?

https://github.com/APIDevTools/swagger-express-middleware/pull/99

jamesacres avatar May 17 '19 10:05 jamesacres

Waiting on this.

gaurang171 avatar Jun 16 '19 13:06 gaurang171