swagger-express-middleware
swagger-express-middleware copied to clipboard
Can swagger-express-middleware mock based on example response?
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?
+1
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.
@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.
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> at ono (/appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/node_modules/ono/lib/index.js:62:17)
<br> at /appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/lib/mock/query-resource.js:34:15
<br> at doCallback (/appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/lib/data-store/index.js:303:5)
<br> at /appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/lib/data-store/index.js:49:7
<br> at Immediate.<anonymous> (/appl/applications/nodeJS/lib/node_modules/swagger-express-middleware/lib/data-store/index.js:290:5)
<br> at Immediate.immediate._onImmediate (timers.js:440:18)
<br> at processImmediate [as _immediateCallback] (timers.js:383:17)
Any idea?
Thanks!
@Fabriciu any luck on this? I'm running into the same thing.
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