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

Can swagger-express-middleware mock based on example response?

Open gauravphoenix opened this issue 8 years ago • 6 comments

First of all, I am thankful such project exists.

My response definition looks like this-

        "responses": {
          "200": {
            "description": "successful operation",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Account"
              }
            },
            "examples": {
              "application/json": [
                {
                  "id": 123,
                  "name": "dev-account"
                },
                {
                  "id": 456,
                  "name:": "prod-account"
                }
              ]
            }
          }
        }

When using swagger-express-middleware I was hoping that when I make API call I will get example response back but I get back an empty array. What can I do to make sure I get example response back?

gauravphoenix avatar May 25 '16 01:05 gauravphoenix

+1

whq731 avatar May 25 '16 06:05 whq731

The Swagger Spec allows 3 different ways to provide default/example responses. It's important to distinguish between the three:

1. responses.[code].examples Spec: here and here This object allows you to provide example responses for different MIME types. This is a new feature that was added to the Swagger Spec since the last release of Swagger Express Middleware, so it's not yet supported.

2. responses.[code].schema.example Spec: here This object allows you to provide an example of a schema. Swagger Express Middleware will automatically use your example as the response if there is no other data available, as described here

3. responses.[code].schema.default Spec: here This object allows you to provide a default value for a schema. Swagger Express Middleware treats this exactly the same as the response.[code].schema.example property, so it will use it as the response if there is no other data available.

JamesMessinger avatar May 25 '16 12:05 JamesMessinger

@BigstickCarpet thanks so much for quick response. I'll go ahead and use responses.[code].schema.example.

On a side node, I wonder what is the real value of responses.[code].examples.

gauravphoenix avatar May 25 '16 20:05 gauravphoenix

Couldn't make it work using default.

I defined my yaml. This is a part of the code:

      responses:
        "200":
          description: successful operation
          schema:
            $ref: "#/definitions/user"
        404:
          description: User not found.
          schema:
            type: object
            properties:
              type:
                type: string
                example: "error"
              status:
                type: string
                example: "404"
              code:
                type: string
                example: "-20001"
              message:
                type: string
                example: "User not found"
              responseId:
                type: string
                example: "Id-1-2-3"
            default:
              application/json:
                type: "error"
                status: "404"
                code: "-20001"
                message: "User not found"
                responseId: "Id-1-2-3"

When I call the API, the middleware returns HTTP 404 and the trace:

Error: /user Not Found
<br> &nbsp; &nbsp;at ono (/appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/node_modules/ono/lib/index.js:62:17)
<br> &nbsp; &nbsp;at /appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/lib/mock/query-resource.js:34:15
<br> &nbsp; &nbsp;at doCallback (/appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/lib/data-store/index.js:303:5)
<br> &nbsp; &nbsp;at /appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/lib/data-store/index.js:49:7
<br> &nbsp; &nbsp;at Immediate.&lt;anonymous&gt; (/appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/lib/data-store/index.js:290:5)
<br> &nbsp; &nbsp;at Immediate.immediate._onImmediate (timers.js:440:18)
<br> &nbsp; &nbsp;at processImmediate [as _immediateCallback] (timers.js:383:17)

Any idea?

Thanks!

Fabriciu avatar Aug 23 '16 22:08 Fabriciu

@Fabriciu any luck on this? I'm running into the same thing.

bmakuh avatar Dec 09 '16 16:12 bmakuh

great library! I am wondering whether taking the example from definitions.[entity].example as already implemented by swagger-ui should also be implemented?

Update: Just found out that it works in the case of returning one entity whereas it doesn't work in the case of returning an array of entities

benz-sc avatar Jan 25 '17 13:01 benz-sc