swagger-express-middleware
swagger-express-middleware copied to clipboard
POST to path causing corresponding GET to change response
Hi there, great library! Our team is starting to use this to mock responses for our API paths, based on a Swagger doc. It seems that for whatever reason, performing a POST operation causes the GET requests to change.
Example
GET http://localhost:4200/api/flows response:
[
{
"sample": "flow obj"
},
{
"sample2": "flow obj 2"
}
]
POST http://localhost:4200/api/flows: Body:
{
"something": "that wont save"
}
Response (correct response):
{
"new": "newest!"
}
GET http://localhost:4200/api/flows after POST:
[
null
]
Example Swagger file illustrating the problem:
{
"swagger": "2.0",
"info": {
"description": "some description",
"version": "1.0.0",
"title": "",
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"tags": [ ],
"schemes": [
"http"
],
"servers": [
{
"url": "http://localhost:4200/api",
"variables": {}
}
],
"basePath": "/api",
"paths": {
"/flows": {
"get": {
"tags": [
"flows"
],
"summary": "Returns all flows",
"description": "Returns all flows",
"operationId": "getFlows",
"produces": [
"application/json"
],
"parameters": [],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "array",
"default": [
{
"sample": "flow obj"
},
{
"sample2": "flow obj 2"
}
]
}
}
}
},
"post": {
"tags": [
"flows"
],
"summary": "Create flow",
"description": "",
"operationId": "createFlow",
"produces": [
"application/json"
],
"parameters": [
{
"name": "body",
"in": "body",
"description": "flow to create",
"required": true,
"schema": {
"type": "object"
}
}
],
"responses": {
"200": {
"type": "object",
"description": "successful operation",
"schema": {
"type": "object",
"default": {
"new": "newest!"
}
}
},
"400": {
"description": "Invalid parameter supplied"
}
}
}
}
}
}
Is there something about my Swagger doc that's causing the problem? I can't seem to figure out what the cause is.
Looks like due to the algorithm to determine resource-to-collection match, i.e. when POST request schema for the '/api/flows' does not match the response schema for your GET operation, then your POST request should be treated as a resource path that would save a resource for '/api/flows'. That does not happen. Also in your example, two objects in your default response have different schemas.
Thanks @alexsmr we tested this IRL and found that's not the case. As you can see from the above, simplified example, there are no schema definitions, so this should be straight forward for the repo owners to reproduce.
@JamesMessinger sorry to bother you, but might you have an idea on why this simple example blows away the GET results after a POST? Might you need more information on how I set up the middleware?
I simply use: app.use(middleware.mock()); with no MemoryDataStore object. My expectation was that without that, the mocked endpoints would respond with the "default" property values for each path, which happens for GETs, but POSTs/PUTs do not function this way.
Thanks for your time.